private void StartInstallation(object args) { IntPtr parent = IntPtr.Zero; MsiInstallUILevel oldLevel = MsiInterop.MsiSetInternalUI(MsiInstallUILevel.None | MsiInstallUILevel.SourceResOnly, ref parent); MsiInstallUIHandler oldHandler = null; try { MsiInstallUIHandler uiHandler = new MsiInstallUIHandler(OnExternalUI); oldHandler = MsiInterop.MsiSetExternalUI(uiHandler, MsiInstallLogMode.ExternalUI, IntPtr.Zero); Application.DoEvents(); MsiError ret = MsiInterop.MsiInstallProduct(msiFile, (string)args); InstallCompleteMessage installCompleteMessage = new InstallCompleteMessage(); installCompleteMessage.SetSuccessStatus(ret == MsiError.Success); SendInstallerMessageReceived(installCompleteMessage); } catch (Exception exc) { WriteLogMessage("Unknown error occured: " + exc.ToString()); } finally { if (oldHandler != null) { MsiInterop.MsiSetExternalUI(oldHandler, MsiInstallLogMode.None, IntPtr.Zero); } MsiInterop.MsiSetInternalUI(oldLevel, ref parent); #if NotLicensed FinishForm finishForm = new FinishForm(); finishForm.ShowDialog(); #endif } }
/// <summary> /// Executes the MSI file with the specified MSI parameters. /// </summary> /// <param name="msiFile">The MSI file.</param> /// <param name="msiParams">The MSI parameters.</param> /// <exception cref="System.ApplicationException"></exception> public void Execute(string msiFile, string msiParams) { MsiInstallUIHandler uiHandler = null; IntPtr parent = IntPtr.Zero; MsiInstallUILevel oldLevel = MsiInterop.MsiSetInternalUI(MsiInstallUILevel.None | MsiInstallUILevel.SourceResOnly, ref parent); MsiInstallUIHandler oldHandler = null; try { uiHandler = new MsiInstallUIHandler(OnExternalUI); //must be kept alive until the end of the MsiInstallProduct call if (SetupStarted != null) { InUiThread(SetupStarted); } oldHandler = MsiInterop.MsiSetExternalUI(uiHandler, MsiInstallLogMode.ExternalUI, IntPtr.Zero); MsiError ret = MsiInterop.MsiInstallProduct(msiFile, msiParams); CurrentActionName = ""; this.MsiErrorCode = (int)ret; if (ret != MsiError.NoError) { Console.WriteLine(string.Format("Failed to install -- {0}", ret)); //(ret==ProductVersion) Another version of this product is already installed throw new ApplicationException(string.Format("Failed to install -- {0}", ret)); } } catch (Exception e) { OnError("Application initialization error: " + e.ToString(), false); CurrentActionName = ""; // do something meaningful throw; } finally { if (oldHandler != null) { MsiInterop.MsiSetExternalUI(oldHandler, MsiInstallLogMode.None, IntPtr.Zero); oldHandler = null; } //It is important to reference uiHandler here to keep it alive till the end. //The debug build is more forgiving and referencing uiHandler is not essential as the code is not optimized if (uiHandler != null) { //see https://wixsharp.codeplex.com/discussions/647701 for details Environment.SetEnvironmentVariable("ReasonForThis", "IHadToDoSomethingThatJITWouldNotOptimiseThis"); uiHandler = null; } MsiInterop.MsiSetInternalUI(oldLevel, ref parent); if (SetupComplete != null) { InUiThread(SetupComplete); } } }
extern static public MsiInstallUIHandler MsiSetExternalUI([MarshalAs(UnmanagedType.FunctionPtr)] MsiInstallUIHandler handler, MsiInstallLogMode filter, IntPtr context);
/// <summary> /// Executes the MSI file with the specified MSI parameters. /// </summary> /// <param name="msiFile">The MSI file.</param> /// <param name="msiParams">The MSI parameters.</param> /// <exception cref="System.ApplicationException"></exception> public void Execute(string msiFile, string msiParams) { MsiInstallUIHandler uiHandler = null; IntPtr parent = IntPtr.Zero; MsiInstallUILevel oldLevel = MsiInterop.MsiSetInternalUI(MsiInstallUILevel.None | MsiInstallUILevel.SourceResOnly, ref parent); MsiInstallUIHandler oldHandler = null; try { uiHandler = new MsiInstallUIHandler(OnExternalUI); //must be kept alive until the end of the MsiInstallProduct call if (SetupStarted != null) InUiThread(SetupStarted); oldHandler = MsiInterop.MsiSetExternalUI(uiHandler, MsiInstallLogMode.ExternalUI, IntPtr.Zero); MsiError ret = MsiInterop.MsiInstallProduct(msiFile, msiParams); CurrentActionName = ""; if (ret != MsiError.Success) { Console.WriteLine(string.Format("Failed to install -- {0}", ret)); //(ret==ProductVersion) Another version of this product is already installed throw new ApplicationException(string.Format("Failed to install -- {0}", ret)); } } catch (Exception e) { OnError("Application initialization error: " + e.ToString(), false); CurrentActionName = ""; throw; // do something meaningful } finally { if (oldHandler != null) { MsiInterop.MsiSetExternalUI(oldHandler, MsiInstallLogMode.None, IntPtr.Zero); oldHandler = null; } //It is important to reference uiHandler here to keep it alive till the end. //The debug build is more forgiving and referencing uiHandler is not essential as the code is not optimized if (uiHandler != null) uiHandler = null; MsiInterop.MsiSetInternalUI(oldLevel, ref parent); if (SetupComplete != null) InUiThread(SetupComplete); } }
/// <summary> /// Executes the MSI file with the specified MSI parameters. /// </summary> /// <param name="msiFile">The MSI file.</param> /// <param name="msiParams">The MSI parameters.</param> /// <exception cref="System.ApplicationException"></exception> public void Execute(string msiFile, string msiParams) { MsiInstallUIHandler uiHandler = null; IntPtr parent = IntPtr.Zero; MsiInstallUILevel oldLevel = MsiInterop.MsiSetInternalUI(MsiInstallUILevel.None | MsiInstallUILevel.SourceResOnly, ref parent); MsiInstallUIHandler oldHandler = null; try { uiHandler = new MsiInstallUIHandler(OnExternalUI); //must be kept alive until the end of the MsiInstallProduct call if (SetupStarted != null) { InUiThread(SetupStarted); } oldHandler = MsiInterop.MsiSetExternalUI(uiHandler, MsiInstallLogMode.ExternalUI, IntPtr.Zero); MsiError ret = MsiInterop.MsiInstallProduct(msiFile, msiParams); CurrentActionName = ""; if (ret != MsiError.Success) { Console.WriteLine(string.Format("Failed to install -- {0}", ret)); //(ret==ProductVersion) Another version of this product is already installed throw new ApplicationException(string.Format("Failed to install -- {0}", ret)); } } catch (Exception e) { OnError("Application initialization error: " + e.ToString(), false); CurrentActionName = ""; throw; // do something meaningful } finally { if (oldHandler != null) { MsiInterop.MsiSetExternalUI(oldHandler, MsiInstallLogMode.None, IntPtr.Zero); oldHandler = null; } //It is important to reference uiHandler here to keep it alive till the end. //The debug build is more forgiving and referencing uiHandler is not essential as the code is not optimized if (uiHandler != null) { uiHandler = null; } MsiInterop.MsiSetInternalUI(oldLevel, ref parent); if (SetupComplete != null) { InUiThread(SetupComplete); } } }