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 } }
static void check(this MsiError result, string errorContext = "") { if (result != MsiError.NoError) { throw new ApplicationException("Error: EmbedTransform.Embed->" + errorContext); } }
public static void Invoke(Func <MsiError> action) { MsiError res = action(); if (res != MsiError.NoError) { throw new Exception(res.ToString()); } }
/// <summary> /// Determines whether the specified product code is installed. /// </summary> /// <param name="productCode">The product code.</param> /// <returns>Returns <c>true</c> if the product is installed. Otherwise returns <c>false</c>.</returns> public static bool IsInstalled(string productCode) { StringBuilder sb = new StringBuilder(2048); uint size = 2048; MsiError err = MsiInterop.MsiGetProductInfo(productCode, "InstallDate", sb, ref size); if (err == MsiError.UnknownProduct) { return(false); } else if (err == MsiError.NoError) { return(true); } else { throw new Exception(err.ToString()); } }
private static void InnerCheck(MsiError actualResult, MsiError expectedResult) { if (actualResult != expectedResult) { StackTrace stackTrace = new StackTrace(); string msiMethodName = "Msi" + stackTrace.GetFrame(2).GetMethod().Name; IntPtr recordHandle = MsiGetLastErrorRecord(); if (recordHandle != IntPtr.Zero) { uint resultSize = 0; MsiError formatRecordResult = MsiFormatRecord(IntPtr.Zero, recordHandle, String.Empty, ref resultSize); if (formatRecordResult == MsiError.MoreData) { resultSize++; StringBuilder sb = new StringBuilder((int)resultSize); MsiFormatRecord(IntPtr.Zero, recordHandle, sb, ref resultSize); throw new InvalidOperationException(String.Format("Call to Windows Installer function {0} failed with error code {1:d} (0x{1:x} = {1}): {2}", msiMethodName, actualResult, sb)); } } throw new InvalidOperationException(String.Format("Call to Windows Installer function {0} failed with error code {1:d} (0x{1:x} = {1}).", msiMethodName, actualResult)); } }
/// <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); } } }
private static void Check(MsiError actualResult, MsiError expectedResult) { InnerCheck(actualResult, expectedResult); }
private static void Check(MsiError actualResult) { InnerCheck(actualResult, MsiError.Success); }
/// <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); } } }