| 1 | // 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. |
| 2 | |
| 3 | namespace WixToolset.Harvesters |
| 4 | { |
| 5 | using System; |
| 6 | using System.Globalization; |
| 7 | using System.Runtime.InteropServices; |
| 8 | using Wix = WixToolset.Harvesters.Serialize; |
| 9 | |
| 10 | /// <summary> |
| 11 | /// Harvest WiX authoring from a type library file. |
| 12 | /// </summary> |
| 13 | public sealed class TypeLibraryHarvester |
| 14 | { |
| 15 | /// <summary> |
| 16 | /// Harvest the registry values written by RegisterTypeLib. |
| 17 | /// </summary> |
| 18 | /// <param name="path">The file to harvest registry values from.</param> |
| 19 | /// <returns>The harvested registry values.</returns> |
| 20 | public Wix.RegistryValue[] HarvestRegistryValues(string path) |
| 21 | { |
| 22 | using (RegistryHarvester registryHarvester = new RegistryHarvester(true)) |
| 23 | { |
| 24 | NativeMethods.RegisterTypeLibrary(path); |
| 25 | |
| 26 | return registryHarvester.HarvestRegistry(); |
| 27 | } |
| 28 | } |
| 29 | |
| 30 | /// <summary> |
| 31 | /// Parses a hexadecimal version string into a Version object. |
| 32 | /// </summary> |
| 33 | /// <param name="versionString">Hexadecimal version string, for example "1.A.3C.F241"</param> |
| 34 | /// <returns>Version object, or null if versionString is not a valid hex version.</returns> |
| 35 | public static Version ParseHexVersion(string versionString) |
| 36 | { |
| 37 | if (String.IsNullOrEmpty(versionString)) |
| 38 | { |
| 39 | return null; |
| 40 | } |
| 41 | |
| 42 | int[] versionNumbers = new int[4]; |
| 43 | string[] versionNumberStrings = versionString.Split('.'); |
| 44 | |
| 45 | for (int i = 0; i < versionNumbers.Length && i < versionNumberStrings.Length; i++) |
| 46 | { |
| 47 | if (!Int32.TryParse(versionNumberStrings[i], NumberStyles.HexNumber, CultureInfo.InvariantCulture, out versionNumbers[i])) |
| 48 | { |
| 49 | return null; |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | return new Version(versionNumbers[0], versionNumbers[1], versionNumbers[2], versionNumbers[3]); |
| 54 | } |
| 55 | |
| 56 | /// <summary> |
| 57 | /// Native methods for registering type libraries. |
| 58 | /// </summary> |
| 59 | private sealed class NativeMethods |
| 60 | { |
| 61 | /// <summary> |
| 62 | /// Registers a type library. |
| 63 | /// </summary> |
| 64 | /// <param name="typeLibraryFile">The type library file to register.</param> |
| 65 | internal static void RegisterTypeLibrary(string typeLibraryFile) |
| 66 | { |
| 67 | IntPtr ptlib; |
| 68 | |
| 69 | LoadTypeLib(typeLibraryFile, out ptlib); |
| 70 | |
| 71 | RegisterTypeLib(ptlib, typeLibraryFile, null); |
| 72 | } |
| 73 | |
| 74 | /// <summary> |
| 75 | /// Loads and registers a type library. |
| 76 | /// </summary> |
| 77 | /// <param name="szFile">Contains the name of the file from which LoadTypeLib should attempt to load a type library.</param> |
| 78 | /// <param name="pptlib">On return, contains a pointer to a pointer to the loaded type library.</param> |
| 79 | /// <remarks>LoadTypeLib will not register the type library if the path of the type library is specified.</remarks> |
| 80 | [DllImport("oleaut32.dll", PreserveSig = false)] |
| 81 | private static extern void LoadTypeLib([MarshalAs(UnmanagedType.BStr)] string szFile, out IntPtr pptlib); |
| 82 | |
| 83 | /// <summary> |
| 84 | /// Adds information about a type library to the system registry. |
| 85 | /// </summary> |
| 86 | /// <param name="ptlib">Pointer to the type library being registered.</param> |
| 87 | /// <param name="szFullPath">Fully qualified path specification for the type library being registered.</param> |
| 88 | /// <param name="szHelpDir">Directory in which the Help file for the library being registered can be found. Can be Null.</param> |
| 89 | [DllImport("oleaut32.dll", PreserveSig = false)] |
| 90 | private static extern void RegisterTypeLib(IntPtr ptlib, [MarshalAs(UnmanagedType.BStr)] string szFullPath, [MarshalAs(UnmanagedType.BStr)] string szHelpDir); |
| 91 | } |
| 92 | } |
| 93 | } |