|
4 | 4 | using System;
|
5 | 5 | using System.Diagnostics;
|
6 | 6 | using System.IO;
|
| 7 | +using System.Text; |
7 | 8 | using System.Xml;
|
8 | 9 | using System.Xml.XPath;
|
9 | 10 | using Microsoft.Deployment.WindowsInstaller;
|
| 11 | +using Microsoft.Win32; |
10 | 12 |
|
11 | 13 | namespace InstallerActions
|
12 | 14 | {
|
@@ -184,6 +186,115 @@ public static ActionResult CloseStatusMonitor(Session session)
|
184 | 186 | return ActionResult.Success;
|
185 | 187 | }
|
186 | 188 |
|
| 189 | + [CustomAction] |
| 190 | + public static ActionResult CheckBitness(Session session) |
| 191 | + { |
| 192 | + try |
| 193 | + { |
| 194 | + var uninstallPaths = new string[] { |
| 195 | + "Software\\Microsoft\\Windows\\CurrentVersion\\Uninstall", |
| 196 | + "SOFTWARE\\Wow6432Node\\Microsoft\\Windows\\CurrentVersion\\Uninstall" }; |
| 197 | + |
| 198 | + // check 64-bit first and then 32-bit if needed. |
| 199 | + string displayName = null; |
| 200 | + foreach (var path in uninstallPaths) |
| 201 | + { |
| 202 | + if (string.IsNullOrEmpty(displayName)) |
| 203 | + { |
| 204 | + displayName = GetDisplayName(path); |
| 205 | + } |
| 206 | + } |
| 207 | + |
| 208 | + // didn't find NR so not installed. |
| 209 | + if (displayName == null) |
| 210 | + { |
| 211 | + return ActionResult.Success; |
| 212 | + } |
| 213 | + |
| 214 | + var productName = session["ProductName"]; |
| 215 | + if (displayName == productName) |
| 216 | + { |
| 217 | + return ActionResult.Success; |
| 218 | + } |
| 219 | + |
| 220 | + session.Message(InstallMessage.FatalExit, BuildBitnessErrorRecord(productName)); |
| 221 | + return ActionResult.Failure; |
| 222 | + } |
| 223 | + catch(Exception exception) |
| 224 | + { |
| 225 | + session.Log("Exception thrown checking bitness:\n{0}", exception); |
| 226 | + return ActionResult.Failure; |
| 227 | + } |
| 228 | + } |
| 229 | + |
| 230 | + private static string GetDisplayName(string path) |
| 231 | + { |
| 232 | + try |
| 233 | + { |
| 234 | + var regKey = Registry.LocalMachine.OpenSubKey(path); |
| 235 | + foreach (var subkeyName in regKey.GetSubKeyNames()) |
| 236 | + { |
| 237 | + // Our key a a guid so skip anything that is not. |
| 238 | + if (!subkeyName.StartsWith("{")) |
| 239 | + { |
| 240 | + continue; |
| 241 | + } |
| 242 | + |
| 243 | + // Some entries are missing the DisplayName value. |
| 244 | + var subkey = regKey.OpenSubKey(subkeyName); |
| 245 | + if (!HasDisplayName(subkey.GetValueNames())) |
| 246 | + { |
| 247 | + continue; |
| 248 | + } |
| 249 | + |
| 250 | + var displayName = subkey.GetValue("DisplayName").ToString(); |
| 251 | + if (string.IsNullOrEmpty(displayName) || !displayName.StartsWith("New Relic .NET Agent")) |
| 252 | + { |
| 253 | + continue; |
| 254 | + } |
| 255 | + |
| 256 | + // we have a new relic displayname here. |
| 257 | + return displayName; |
| 258 | + } |
| 259 | + } |
| 260 | + catch |
| 261 | + {} |
| 262 | + |
| 263 | + return null; |
| 264 | + } |
| 265 | + |
| 266 | + private static Record BuildBitnessErrorRecord(string productName) |
| 267 | + { |
| 268 | + var builder = new StringBuilder(); |
| 269 | + if (productName == "New Relic .NET Agent (64-bit)") |
| 270 | + { |
| 271 | + builder.AppendLine("The installed x86 (32-bit) version of the New Relic .NET Agent is not compatible with this 64-bit installer."); |
| 272 | + builder.AppendLine(); |
| 273 | + builder.AppendLine("Either remove the existing installation or use the x86 (32-bit) installer."); |
| 274 | + } |
| 275 | + else |
| 276 | + { |
| 277 | + builder.AppendLine("The installed 64-bit version of the New Relic .NET Agent is not compatible with this x86 (32-bit) installer."); |
| 278 | + builder.AppendLine(); |
| 279 | + builder.AppendLine("Either remove the existing installation or use the 64-bit installer."); |
| 280 | + } |
| 281 | + |
| 282 | + return new Record { FormatString = builder.ToString() }; |
| 283 | + } |
| 284 | + |
| 285 | + private static bool HasDisplayName(string[] values) |
| 286 | + { |
| 287 | + for (int i = 0; i < values.Length; i++) |
| 288 | + { |
| 289 | + if (values[i] == "DisplayName") |
| 290 | + { |
| 291 | + return true; |
| 292 | + } |
| 293 | + } |
| 294 | + |
| 295 | + return false; |
| 296 | + } |
| 297 | + |
187 | 298 | public static void Log(Session session, string message, params object[] arguments)
|
188 | 299 | {
|
189 | 300 | session["LogHack"] = String.Format(message, arguments);
|
|
0 commit comments