Ejemplo n.º 1
0
        /// <summary>
        /// Gets product information for an installer script file.
        /// </summary>
        /// <param name="scriptFile">Path to a script file generated by
        /// <see cref="GenerateAdvertiseScript(string,string,string,int,ProcessorArchitecture,bool)"/></param>
        /// <returns>ProductInstallation stub with advertise-related properties filled in.</returns>
        /// <exception cref="ArgumentOutOfRangeException">An invalid product property was requested</exception>
        /// <remarks><p>
        /// Only the following properties will be filled in in the returned object:<ul>
        /// <li><see cref="ProductInstallation.ProductCode"/></li>
        /// <li><see cref="ProductInstallation.AdvertisedLanguage"/></li>
        /// <li><see cref="ProductInstallation.AdvertisedVersion"/></li>
        /// <li><see cref="ProductInstallation.AdvertisedProductName"/></li>
        /// <li><see cref="ProductInstallation.AdvertisedPackageName"/></li>
        /// </ul>Other properties will be null.
        /// </p><p>
        /// Win32 MSI API:
        /// <a href="http://msdn.microsoft.com/library/en-us/msi/setup/msigetproductinfofromscript.asp">MsiGetProductInfoFromScript</a>
        /// </p></remarks>
        public static ProductInstallation GetProductInfoFromScript(string scriptFile)
        {
            if (String.IsNullOrEmpty(scriptFile))
            {
                throw new ArgumentNullException("scriptFile");
            }
            StringBuilder productCodeBuf = new StringBuilder(40);
            ushort        lang;
            uint          ver;
            StringBuilder productNameBuf     = new StringBuilder(100);
            StringBuilder packageNameBuf     = new StringBuilder(40);
            uint          productCodeBufSize = (uint)productCodeBuf.Capacity;
            uint          productNameBufSize = (uint)productNameBuf.Capacity;
            uint          packageNameBufSize = (uint)packageNameBuf.Capacity;
            uint          ret = NativeMethods.MsiGetProductInfoFromScript(
                scriptFile,
                productCodeBuf,
                out lang,
                out ver,
                productNameBuf,
                ref productNameBufSize,
                packageNameBuf,
                ref packageNameBufSize);

            if (ret == (uint)NativeMethods.Error.MORE_DATA)
            {
                productCodeBuf.Capacity = (int)++productCodeBufSize;
                productNameBuf.Capacity = (int)++productNameBufSize;
                packageNameBuf.Capacity = (int)++packageNameBufSize;
                ret = NativeMethods.MsiGetProductInfoFromScript(
                    scriptFile,
                    productCodeBuf,
                    out lang,
                    out ver,
                    productNameBuf,
                    ref productNameBufSize,
                    packageNameBuf,
                    ref packageNameBufSize);
            }

            if (ret != 0)
            {
                throw InstallerException.ExceptionFromReturnCode(ret);
            }
            uint    verPart1 = ver >> 24;
            uint    verPart2 = (ver & 0x00FFFFFF) >> 16;
            uint    verPart3 = ver & 0x0000FFFF;
            Version version  = new Version((int)verPart1, (int)verPart2, (int)verPart3);

            IDictionary <string, string> props = new Dictionary <string, string>();

            props["ProductCode"] = productCodeBuf.ToString();
            props["Language"]    = lang.ToString(CultureInfo.InvariantCulture);
            props["Version"]     = version.ToString();
            props["ProductName"] = productNameBuf.ToString();
            props["PackageName"] = packageNameBuf.ToString();
            return(new ProductInstallation(props));
        }