/// <summary> /// Opens an installer package for use with functions that access the product database and install engine, /// returning an Session object. /// </summary> /// <param name="packagePath">Path to the package</param> /// <param name="ignoreMachineState">Specifies whether or not the create a Session object that ignores the /// computer state and that is incapable of changing the current computer state. A value of false yields /// the normal behavior. A value of true creates a "safe" Session object that cannot change of the current /// machine state.</param> /// <returns>A Session object allowing access to the product database and install engine</returns> /// <exception cref="InstallerException">The product could not be opened</exception> /// <exception cref="InstallerException">The installer configuration data is corrupt</exception> /// <remarks><p> /// Note that only one Session object can be opened by a single process. OpenPackage cannot be used in a /// custom action because the active installation is the only session allowed. /// </p><p> /// A "safe" Session object ignores the current computer state when opening the package and prevents /// changes to the current computer state. /// </p><p> /// The Session object should be <see cref="InstallerHandle.Close"/>d after use. /// It is best that the handle be closed manually as soon as it is no longer /// needed, as leaving lots of unused handles open can degrade performance. /// </p><p> /// Win32 MSI APIs: /// <a href="http://msdn.microsoft.com/library/en-us/msi/setup/msiopenpackage.asp">MsiOpenPackage</a>, /// <a href="http://msdn.microsoft.com/library/en-us/msi/setup/msiopenpackageex.asp">MsiOpenPackageEx</a> /// </p></remarks> public static Session OpenPackage(string packagePath, bool ignoreMachineState) { int sessionHandle; uint ret = NativeMethods.MsiOpenPackageEx(packagePath, ignoreMachineState ? (uint)1 : 0, out sessionHandle); if (ret != 0) { throw InstallerException.ExceptionFromReturnCode(ret); } return(new Session((IntPtr)sessionHandle, true)); }