// Copyright (c) .NET Foundation and contributors. All rights reserved. Licensed under the Microsoft Reciprocal License. See LICENSE.TXT file in the project root for full license information. namespace WixToolset.Dtf.WindowsInstaller { using System; using System.IO; using System.Text; using System.Reflection; using System.Globalization; using System.Collections.Generic; using System.Diagnostics.CodeAnalysis; public static partial class Installer { /// /// Advertises a product to the local computer. /// /// Path to the package of the product being advertised /// True if the product is user-assigned; false if it is machine-assigned. /// Semi-colon delimited list of transforms to be applied. This parameter may be null. /// The language to use if the source supports multiple languages /// the specified package file does not exist /// ///

/// Win32 MSI APIs: /// MsiAdvertiseProduct, /// MsiAdvertiseProductEx ///

public static void AdvertiseProduct(string packagePath, bool perUser, string transforms, int locale) { if (String.IsNullOrEmpty(packagePath)) { throw new ArgumentNullException("packagePath"); } if (!File.Exists(packagePath)) { throw new FileNotFoundException(null, packagePath); } uint ret = NativeMethods.MsiAdvertiseProduct(packagePath, new IntPtr(perUser ? 1 : 0), transforms, (ushort) locale); if (ret != 0) { throw InstallerException.ExceptionFromReturnCode(ret); } } /// /// Generates an advertise script. The method enables the installer to write to a /// script the registry and shortcut information used to assign or publish a product. /// /// Path to the package of the product being advertised /// path to script file to be created with the advertise information /// Semi-colon delimited list of transforms to be applied. This parameter may be null. /// The language to use if the source supports multiple languages /// the specified package file does not exist /// ///

/// Win32 MSI APIs: /// MsiAdvertiseProduct, /// MsiAdvertiseProductEx ///

public static void GenerateAdvertiseScript(string packagePath, string scriptFilePath, string transforms, int locale) { if (String.IsNullOrEmpty(packagePath)) { throw new ArgumentNullException("packagePath"); } if (!File.Exists(packagePath)) { throw new FileNotFoundException(null, packagePath); } uint ret = NativeMethods.MsiAdvertiseProduct(packagePath, scriptFilePath, transforms, (ushort) locale); if (ret != 0) { throw InstallerException.ExceptionFromReturnCode(ret); } } /// /// Generates an advertise script. The method enables the installer to write to a /// script the registry and shortcut information used to assign or publish a product. /// /// Path to the package of the product being advertised /// path to script file to be created with the advertise information /// Semi-colon delimited list of transforms to be applied. This parameter may be null. /// The language to use if the source supports multiple languages /// Targeted processor architecture. /// True to install multiple instances through product code changing transform. /// Advertises a new instance of the product. Requires that the parameter /// includes the instance transform that changes the product code. /// ///

/// Win32 MSI APIs: /// MsiAdvertiseProduct, /// MsiAdvertiseProductEx ///

public static void GenerateAdvertiseScript( string packagePath, string scriptFilePath, string transforms, int locale, ProcessorArchitecture processor, bool instance) { if (String.IsNullOrEmpty(packagePath)) { throw new ArgumentNullException("packagePath"); } if (String.IsNullOrEmpty(scriptFilePath)) { throw new ArgumentNullException("scriptFilePath"); } if (!File.Exists(packagePath)) { throw new FileNotFoundException(null, packagePath); } uint platform = 0; switch (processor) { case ProcessorArchitecture.X86: platform = (uint) 1; break; case ProcessorArchitecture.IA64: platform = (uint) 2; break; case ProcessorArchitecture.Amd64: platform = (uint) 4; break; } uint ret = NativeMethods.MsiAdvertiseProductEx( packagePath, scriptFilePath, transforms, (ushort) locale, platform, instance ? (uint) 1 : 0); if (ret != 0) { throw InstallerException.ExceptionFromReturnCode(ret); } } /// /// Copies an advertise script file to the local computer. /// /// Path to a script file generated by /// /// Flags controlling advertisement /// True if specified items are to be removed instead of being created ///

/// The process calling this function must be running under the LocalSystem account. To advertise an /// application for per-user installation to a targeted user, the thread that calls this function must /// impersonate the targeted user. If the thread calling this function is not impersonating a targeted /// user, the application is advertised to all users for installation with elevated privileges. ///

[SuppressMessage("Microsoft.Naming", "CA1726:UsePreferredTerms", MessageId = "flags")] public static void AdvertiseScript(string scriptFile, int flags, bool removeItems) { uint ret = NativeMethods.MsiAdvertiseScript(scriptFile, (uint) flags, IntPtr.Zero, removeItems); if (ret != 0) { throw InstallerException.ExceptionFromReturnCode(ret); } } /// /// Processes an advertise script file into the specified locations. /// /// Path to a script file generated by /// /// An optional path to a folder in which advertised icon files and transform /// files are located. If this parameter is null, no icon or transform files are written. /// True if shortcuts should be created /// True if specified items are to be removed instead of created ///

/// The process calling this function must be running under the LocalSystem account. To advertise an /// application for per-user installation to a targeted user, the thread that calls this function must /// impersonate the targeted user. If the thread calling this function is not impersonating a targeted /// user, the application is advertised to all users for installation with elevated privileges. ///

/// Win32 MSI API: /// MsiProcessAdvertiseScript ///

public static void ProcessAdvertiseScript(string scriptFile, string iconFolder, bool shortcuts, bool removeItems) { uint ret = NativeMethods.MsiProcessAdvertiseScript(scriptFile, iconFolder, IntPtr.Zero, shortcuts, removeItems); if (ret != 0) { throw InstallerException.ExceptionFromReturnCode(ret); } } /// /// Gets product information for an installer script file. /// /// Path to a script file generated by /// /// ProductInstallation stub with advertise-related properties filled in. /// An invalid product property was requested ///

/// Only the following properties will be filled in in the returned object:

    ///
  • ///
  • ///
  • ///
  • ///
  • ///
Other properties will be null. ///

/// Win32 MSI API: /// MsiGetProductInfoFromScript ///

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 props = new Dictionary(); 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); } } }