diff options
Diffstat (limited to 'src/WixToolset.Core.Native/Msi/Installer.cs')
-rw-r--r-- | src/WixToolset.Core.Native/Msi/Installer.cs | 113 |
1 files changed, 113 insertions, 0 deletions
diff --git a/src/WixToolset.Core.Native/Msi/Installer.cs b/src/WixToolset.Core.Native/Msi/Installer.cs new file mode 100644 index 00000000..2bb41078 --- /dev/null +++ b/src/WixToolset.Core.Native/Msi/Installer.cs | |||
@@ -0,0 +1,113 @@ | |||
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.Core.Native.Msi | ||
4 | { | ||
5 | using System; | ||
6 | using System.Diagnostics; | ||
7 | using System.Text; | ||
8 | |||
9 | /// <summary> | ||
10 | /// A callback function that the installer calls for progress notification and error messages. | ||
11 | /// </summary> | ||
12 | /// <param name="context">Pointer to an application context. | ||
13 | /// This parameter can be used for error checking.</param> | ||
14 | /// <param name="messageType">Specifies a combination of one message box style, | ||
15 | /// one message box icon type, one default button, and one installation message type.</param> | ||
16 | /// <param name="message">Specifies the message text.</param> | ||
17 | /// <returns>-1 for an error, 0 if no action was taken, 1 if OK, 3 to abort.</returns> | ||
18 | public delegate int InstallUIHandler(IntPtr context, uint messageType, string message); | ||
19 | |||
20 | /// <summary> | ||
21 | /// Represents the Windows Installer, provides wrappers to | ||
22 | /// create the top-level objects and access their methods. | ||
23 | /// </summary> | ||
24 | public static class Installer | ||
25 | { | ||
26 | /// <summary> | ||
27 | /// Takes the path to a file and returns a 128-bit hash of that file. | ||
28 | /// </summary> | ||
29 | /// <param name="filePath">Path to file that is to be hashed.</param> | ||
30 | /// <param name="options">The value in this column must be 0. This parameter is reserved for future use.</param> | ||
31 | /// <param name="hash">Int array that receives the returned file hash information.</param> | ||
32 | public static void GetFileHash(string filePath, int options, out int[] hash) | ||
33 | { | ||
34 | var hashInterop = new MSIFILEHASHINFO(); | ||
35 | hashInterop.FileHashInfoSize = 20; | ||
36 | |||
37 | var error = MsiInterop.MsiGetFileHash(filePath, Convert.ToUInt32(options), hashInterop); | ||
38 | if (0 != error) | ||
39 | { | ||
40 | throw new MsiException(error); | ||
41 | } | ||
42 | |||
43 | Debug.Assert(20 == hashInterop.FileHashInfoSize); | ||
44 | |||
45 | hash = new int[4]; | ||
46 | hash[0] = hashInterop.Data0; | ||
47 | hash[1] = hashInterop.Data1; | ||
48 | hash[2] = hashInterop.Data2; | ||
49 | hash[3] = hashInterop.Data3; | ||
50 | } | ||
51 | |||
52 | /// <summary> | ||
53 | /// Returns the version string and language string in the format that the installer | ||
54 | /// expects to find them in the database. If you just want version information, set | ||
55 | /// lpLangBuf and pcchLangBuf to zero. If you just want language information, set | ||
56 | /// lpVersionBuf and pcchVersionBuf to zero. | ||
57 | /// </summary> | ||
58 | /// <param name="filePath">Specifies the path to the file.</param> | ||
59 | /// <param name="version">Returns the file version. Set to 0 for language information only.</param> | ||
60 | /// <param name="language">Returns the file language. Set to 0 for version information only.</param> | ||
61 | public static void GetFileVersion(string filePath, out string version, out string language) | ||
62 | { | ||
63 | var versionLength = 20; | ||
64 | var languageLength = 20; | ||
65 | var versionBuffer = new StringBuilder(versionLength); | ||
66 | var languageBuffer = new StringBuilder(languageLength); | ||
67 | |||
68 | var error = MsiInterop.MsiGetFileVersion(filePath, versionBuffer, ref versionLength, languageBuffer, ref languageLength); | ||
69 | if (234 == error) | ||
70 | { | ||
71 | versionBuffer.EnsureCapacity(++versionLength); | ||
72 | languageBuffer.EnsureCapacity(++languageLength); | ||
73 | error = MsiInterop.MsiGetFileVersion(filePath, versionBuffer, ref versionLength, languageBuffer, ref languageLength); | ||
74 | } | ||
75 | else if (1006 == error) | ||
76 | { | ||
77 | // file has no version or language, so no error | ||
78 | error = 0; | ||
79 | } | ||
80 | |||
81 | if (0 != error) | ||
82 | { | ||
83 | throw new MsiException(error); | ||
84 | } | ||
85 | |||
86 | version = versionBuffer.ToString(); | ||
87 | language = languageBuffer.ToString(); | ||
88 | } | ||
89 | |||
90 | /// <summary> | ||
91 | /// Enables an external user-interface handler. | ||
92 | /// </summary> | ||
93 | /// <param name="installUIHandler">Specifies a callback function.</param> | ||
94 | /// <param name="messageFilter">Specifies which messages to handle using the external message handler.</param> | ||
95 | /// <param name="context">Pointer to an application context that is passed to the callback function.</param> | ||
96 | /// <returns>The return value is the previously set external handler, or null if there was no previously set handler.</returns> | ||
97 | public static InstallUIHandler SetExternalUI(InstallUIHandler installUIHandler, int messageFilter, IntPtr context) | ||
98 | { | ||
99 | return MsiInterop.MsiSetExternalUI(installUIHandler, messageFilter, context); | ||
100 | } | ||
101 | |||
102 | /// <summary> | ||
103 | /// Enables the installer's internal user interface. | ||
104 | /// </summary> | ||
105 | /// <param name="uiLevel">Specifies the level of complexity of the user interface.</param> | ||
106 | /// <param name="hwnd">Pointer to a window. This window becomes the owner of any user interface created.</param> | ||
107 | /// <returns>The previous user interface level is returned. If an invalid dwUILevel is passed, then INSTALLUILEVEL_NOCHANGE is returned.</returns> | ||
108 | public static int SetInternalUI(int uiLevel, ref IntPtr hwnd) | ||
109 | { | ||
110 | return MsiInterop.MsiSetInternalUI(uiLevel, ref hwnd); | ||
111 | } | ||
112 | } | ||
113 | } | ||