using System; using System.Diagnostics; using Microsoft.Win32; using System.Windows.Forms; using RegAddProductCode.Properties; namespace RegAddProductCode { class AddProductCode { private static EventLog log = new EventLog(); static int Main(string[] args) { var architectureVersion = string.Empty; for (var i = 0; i < args.Length; i++) { switch (i) { case 0: architectureVersion = args[i]; break; } } log.Source = "DataPROInstaller"; log.WriteEntry("ArchitectureVersion is " + architectureVersion); log.WriteEntry(!Environment.Is64BitProcess ? "This is NOT a 64-bit process" : "This IS a 64-bit process"); log.WriteEntry(Environment.Is64BitOperatingSystem ? "This IS a 64-bit operation system" : "This is NOT a 64-bit operating system"); switch (architectureVersion) { case "x86": if (Environment.Is64BitOperatingSystem) { //Return false so the installer will terminate due to running the 32-bit installer //on a 64-bit operating system. This would be OK, except that if the 32-bit 2014 SqlLocalDb //installer needs to be installed as a prerequisite, it will fail silently and DataPRO //will fail upon initiation due to a database with a higher version than what 2012 SqlLocalDb //can handle. log.WriteEntry("Displaying 32-bit error message box and returning 1"); MessageBox.Show("32-bit DataPRO is not allowed to be installed on 64-bit operating system"); return 1; } break; case "x64": if (!Environment.Is64BitOperatingSystem) { //Return false so the installer will gracefully terminate instead of less gracefully later. log.WriteEntry("Displaying 64-bit error message box and returning 1"); MessageBox.Show("64-bit DataPRO is not allowed to be installed on 32-bit operating system"); return 1; } break; } log.WriteEntry("Calling AddCodeToRegistry"); var result = AddCodeToRegistry(); log.WriteEntry("Result of AddCodeToRegistry is " + result); //if (!string.IsNullOrWhiteSpace(result)) //{ // MessageBox.Show(result); //} log.WriteEntry("Returning 0"); return 0; } /// /// Ensures that the following is in the Registry: /// Computer\HKEY_LOCAL_MACHINE\SOFTWARE\Policies\Microsoft\Windows\Installer /// and that it contains the following: Name: SecureRepairPolicy; Type: REG_DWORD; Data 0x00000002 (2) /// Computer\HKEY_LOCAL_MACHINE\SOFTWARE\Policies\Microsoft\Windows\Installer\SecureRepairWhitelist /// and that it contains the following: Name: {C4889149-0CAF-44C1-B226-8F6E73684DF4}; Type: REG_DWORD; Data 0x00000000 (0) /// so that the DataPRO.exe (or its dependents) may be installed. /// /// private static string AddCodeToRegistry() { var result = string.Empty; try { var rk = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64); var sk1 = rk.OpenSubKey(Settings.Default.InstallerKey, RegistryKeyPermissionCheck.ReadWriteSubTree, System.Security.AccessControl.RegistryRights.FullControl); if (sk1 == null) return string.Format(Settings.Default.MissingKey, Settings.Default.InstallerKey); sk1.SetValue(Settings.Default.SecureRepairPolicy, 2, RegistryValueKind.DWord); sk1 = rk.OpenSubKey(Settings.Default.SecureRepairWhitelistKey, RegistryKeyPermissionCheck.ReadWriteSubTree, System.Security.AccessControl.RegistryRights.FullControl) ?? Registry.LocalMachine.CreateSubKey(Settings.Default.SecureRepairWhitelistKey); if (sk1 == null) return string.Format(Settings.Default.MissingKey, Settings.Default.SecureRepairWhitelistKey); //Add this Product Code sk1.SetValue(Settings.Default.ProductCode, 0); } catch (Exception ex) { result = ex.Message; } if (!string.IsNullOrWhiteSpace(result)) { MessageBox.Show(result); } return result; } } }