Beispiel #1
0
        /// <summary>
        /// Opens an installer package for an installed product using the product code.
        /// </summary>
        /// <param name="productCode">Product code of the installed product</param>
        /// <returns>A Session object allowing access to the product database and install engine,
        /// or null if the specified product is not installed.</returns>
        /// <exception cref="ArgumentException">An unknown product was requested</exception>
        /// <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. OpenProduct cannot be
        /// used in a custom action because the active installation is the only session allowed.
        /// </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 API:
        /// <a href="http://msdn.microsoft.com/library/en-us/msi/setup/msiopenproduct.asp">MsiOpenProduct</a>
        /// </p></remarks>
        public static Session OpenProduct(string productCode)
        {
            int  sessionHandle;
            uint ret = NativeMethods.MsiOpenProduct(productCode, out sessionHandle);

            if (ret != 0)
            {
                if (ret == (uint)NativeMethods.Error.UNKNOWN_PRODUCT)
                {
                    return(null);
                }
                else
                {
                    throw InstallerException.ExceptionFromReturnCode(ret);
                }
            }
            return(new Session((IntPtr)sessionHandle, true));
        }