1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
|
// 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.Text;
using System.Collections.Generic;
using System.Globalization;
using System.Diagnostics.CodeAnalysis;
/// <summary>
/// The Patch object represents a unique instance of a patch that has been
/// registered or applied.
/// </summary>
public class PatchInstallation : Installation
{
/// <summary>
/// Enumerates all patch installations on the system.
/// </summary>
/// <returns>Enumeration of patch objects.</returns>
/// <remarks><p>
/// Win32 MSI API:
/// <a href="http://msdn.microsoft.com/library/en-us/msi/setup/msienumpatches.asp">MsiEnumPatches</a>
/// </p></remarks>
public static IEnumerable<PatchInstallation> AllPatches
{
get
{
return PatchInstallation.GetPatches(null, null, null, UserContexts.All, PatchStates.All);
}
}
/// <summary>
/// Enumerates patch installations based on certain criteria.
/// </summary>
/// <param name="patchCode">PatchCode (GUID) of the patch to be enumerated. Only
/// instances of patches within the scope of the context specified by the
/// <paramref name="userSid"/> and <paramref name="context"/> parameters will be
/// enumerated. This parameter may be set to null to enumerate all patches in the specified
/// context.</param>
/// <param name="targetProductCode">ProductCode (GUID) product whose patches are to be
/// enumerated. If non-null, patch enumeration is restricted to instances of this product
/// within the specified context. If null, the patches for all products under the specified
/// context are enumerated.</param>
/// <param name="userSid">Specifies a security identifier (SID) that restricts the context
/// of enumeration. A SID value other than s-1-1-0 is considered a user SID and restricts
/// enumeration to the current user or any user in the system. The special SID string
/// s-1-1-0 (Everyone) specifies enumeration across all users in the system. This parameter
/// can be set to null to restrict the enumeration scope to the current user. When
/// <paramref name="userSid"/> must be null.</param>
/// <param name="context">Specifies the user context.</param>
/// <param name="states">The <see cref="PatchStates"/> of patches to return.</param>
/// <remarks><p>
/// Win32 MSI APIs:
/// <a href="http://msdn.microsoft.com/library/en-us/msi/setup/msienumpatchesex.asp">MsiEnumPatchesEx</a>
/// </p></remarks>
public static IEnumerable<PatchInstallation> GetPatches(
string patchCode,
string targetProductCode,
string userSid,
UserContexts context,
PatchStates states)
{
StringBuilder buf = new StringBuilder(40);
StringBuilder targetProductBuf = new StringBuilder(40);
UserContexts targetContext;
StringBuilder targetSidBuf = new StringBuilder(40);
for (uint i = 0; ; i++)
{
uint targetSidBufSize = (uint) targetSidBuf.Capacity;
uint ret = NativeMethods.MsiEnumPatchesEx(
targetProductCode,
userSid,
context,
(uint) states,
i,
buf,
targetProductBuf,
out targetContext,
targetSidBuf,
ref targetSidBufSize);
if (ret == (uint) NativeMethods.Error.MORE_DATA)
{
targetSidBuf.Capacity = (int) ++targetSidBufSize;
ret = NativeMethods.MsiEnumPatchesEx(
targetProductCode,
userSid,
context,
(uint) states,
i,
buf,
targetProductBuf,
out targetContext,
targetSidBuf,
ref targetSidBufSize);
}
if (ret == (uint) NativeMethods.Error.NO_MORE_ITEMS)
{
break;
}
if (ret != 0)
{
throw InstallerException.ExceptionFromReturnCode(ret);
}
string thisPatchCode = buf.ToString();
if (patchCode == null || patchCode == thisPatchCode)
{
yield return new PatchInstallation(
buf.ToString(),
targetProductBuf.ToString(),
targetSidBuf.ToString(),
targetContext);
}
}
}
private string productCode;
/// <summary>
/// Creates a new object for accessing information about a patch installation on the current system.
/// </summary>
/// <param name="patchCode">Patch code (GUID) of the patch.</param>
/// <param name="productCode">ProductCode (GUID) the patch has been applied to.
/// This parameter may be null for patches that are registered only and not yet
/// applied to any product.</param>
/// <remarks><p>
/// All available user contexts will be queried.
/// </p></remarks>
public PatchInstallation(string patchCode, string productCode)
: this(patchCode, productCode, null, UserContexts.All)
{
}
/// <summary>
/// Creates a new object for accessing information about a patch installation on the current system.
/// </summary>
/// <param name="patchCode">Registered patch code (GUID) of the patch.</param>
/// <param name="productCode">ProductCode (GUID) the patch has been applied to.
/// This parameter may be null for patches that are registered only and not yet
/// applied to any product.</param>
/// <param name="userSid">The specific user, when working in a user context. This
/// parameter may be null to indicate the current user. The parameter must be null
/// when working in a machine context.</param>
/// <param name="context">The user context. The calling process must have administrative
/// privileges to get information for a product installed for a user other than the
/// current user.</param>
/// <remarks><p>
/// If the <paramref name="productCode"/> is null, the Patch object may
/// only be used to read and update the patch's SourceList information.
/// </p></remarks>
public PatchInstallation(string patchCode, string productCode, string userSid, UserContexts context)
: base(patchCode, userSid, context)
{
if (String.IsNullOrEmpty(patchCode))
{
throw new ArgumentNullException("patchCode");
}
this.productCode = productCode;
}
/// <summary>
/// Gets the patch code (GUID) of the patch.
/// </summary>
public string PatchCode
{
get
{
return this.InstallationCode;
}
}
/// <summary>
/// Gets the ProductCode (GUID) of the product.
/// </summary>
public string ProductCode
{
get
{
return this.productCode;
}
}
/// <summary>
/// Gets a value indicating whether this patch is currently installed.
/// </summary>
public override bool IsInstalled
{
get
{
return (this.State & PatchStates.Applied) != 0;
}
}
/// <summary>
/// Gets a value indicating whether this patch is marked as obsolte.
/// </summary>
[SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly", MessageId = "Obsoleted")]
public bool IsObsoleted
{
get
{
return (this.State & PatchStates.Obsoleted) != 0;
}
}
/// <summary>
/// Gets a value indicating whether this patch is present but has been
/// superseded by a more recent installed patch.
/// </summary>
public bool IsSuperseded
{
get
{
return (this.State & PatchStates.Superseded) != 0;
}
}
internal override int InstallationType
{
get
{
const int MSICODE_PATCH = 0x40000000;
return MSICODE_PATCH;
}
}
/// <summary>
/// Gets the installation state of this instance of the patch.
/// </summary>
/// <exception cref="ArgumentException">An unknown patch was requested</exception>
/// <exception cref="InstallerException">The installer configuration data is corrupt</exception>
public PatchStates State
{
get
{
string stateString = this["State"];
return (PatchStates) Int32.Parse(stateString, CultureInfo.InvariantCulture.NumberFormat);
}
}
/// <summary>
/// Gets the cached patch file that the product uses.
/// </summary>
public string LocalPackage
{
get
{
return this["LocalPackage"];
}
}
/// <summary>
/// Gets the set of patch transforms that the last patch
/// installation applied to the product.
/// </summary>
/// <remarks><p>
/// This value may not be available for per-user, non-managed applications
/// if the user is not logged on.
/// </p></remarks>
public string Transforms
{
get
{
// TODO: convert to IList<string>?
return this["Transforms"];
}
}
/// <summary>
/// Gets the date and time when the patch is applied to the product.
/// </summary>
public DateTime InstallDate
{
get
{
try
{
return DateTime.ParseExact(
this["InstallDate"], "yyyyMMdd", CultureInfo.InvariantCulture);
}
catch (FormatException)
{
return DateTime.MinValue;
}
}
}
/// <summary>
/// True patch is marked as possible to uninstall from the product.
/// </summary>
/// <remarks><p>
/// Even if this property is true, the installer can still block the
/// uninstallation if this patch is required by another patch that
/// cannot be uninstalled.
/// </p></remarks>
[SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly", MessageId = "Uninstallable")]
public bool Uninstallable
{
get
{
return this["Uninstallable"] == "1";
}
}
/// <summary>
/// Get the registered display name for the patch.
/// </summary>
public string DisplayName
{
get
{
return this["DisplayName"];
}
}
/// <summary>
/// Gets the registered support information URL for the patch.
/// </summary>
public Uri MoreInfoUrl
{
get
{
string value = this["MoreInfoURL"];
if (!String.IsNullOrEmpty(value))
{
try
{
return new Uri(value);
}
catch (UriFormatException) { }
}
return null;
}
}
/// <summary>
/// Gets information about a specific patch installation.
/// </summary>
/// <param name="propertyName">The property being retrieved; see remarks for valid properties.</param>
/// <returns>The property value, or an empty string if the property is not set for the patch.</returns>
/// <exception cref="ArgumentOutOfRangeException">An unknown patch or property was requested</exception>
/// <exception cref="InstallerException">The installer configuration data is corrupt</exception>
/// <remarks><p>
/// Win32 MSI APIs:
/// <a href="http://msdn.microsoft.com/library/en-us/msi/setup/msigetpatchinfo.asp">MsiGetPatchInfo</a>,
/// <a href="http://msdn.microsoft.com/library/en-us/msi/setup/msigetpatchinfoex.asp">MsiGetPatchInfoEx</a>
/// </p></remarks>
public override string this[string propertyName]
{
get
{
StringBuilder buf = new StringBuilder("");
uint bufSize = 0;
uint ret;
if (this.Context == UserContexts.UserManaged ||
this.Context == UserContexts.UserUnmanaged ||
this.Context == UserContexts.Machine)
{
ret = NativeMethods.MsiGetPatchInfoEx(
this.PatchCode,
this.ProductCode,
this.UserSid,
this.Context,
propertyName,
buf,
ref bufSize);
if (ret == (uint) NativeMethods.Error.MORE_DATA)
{
buf.Capacity = (int) ++bufSize;
ret = NativeMethods.MsiGetPatchInfoEx(
this.PatchCode,
this.ProductCode,
this.UserSid,
this.Context,
propertyName,
buf,
ref bufSize);
}
}
else
{
ret = NativeMethods.MsiGetPatchInfo(
this.PatchCode,
propertyName,
buf,
ref bufSize);
if (ret == (uint) NativeMethods.Error.MORE_DATA)
{
buf.Capacity = (int) ++bufSize;
ret = NativeMethods.MsiGetPatchInfo(
this.PatchCode,
propertyName,
buf,
ref bufSize);
}
}
if (ret != 0)
{
return null;
}
return buf.ToString();
}
}
}
}
|