Use IntPtr for pointers to handles
This is arguably more correct than raw uints.
Rob Mensching committed
Aug 29, 2022 at 13:13 UTC
a5f672ec734df2bbc7559e5192f6c10d273215e1
8 files changed
+44
-46
src/wix/WixToolset.Core.Native/Msi/Database.cs
+1
-1
@@ -20,7 +20,7 @@ namespace WixToolset.Core.Native.Msi
20
/// <param name="type">Persist mode to use when opening the database.</param>
21
public Database(string path, OpenDatabase type)
22
{
23
- var error = MsiInterop.MsiOpenDatabase(path, new IntPtr((int)type), out var handle);
23
+ var error = MsiInterop.MsiOpenDatabase(path, (IntPtr)type, out var handle);
24
if (0 != error)
25
{
26
throw new MsiException(error);
src/wix/WixToolset.Core.Native/Msi/MsiException.cs
+2
-2
@@ -17,8 +17,8 @@ namespace WixToolset.Core.Native.Msi
17
/// <param name="error">The error code from the MsiXxx() function call.</param>
18
public MsiException(int error) : base(error)
19
{
20
- uint handle = MsiInterop.MsiGetLastErrorRecord();
21
- if (0 != handle)
20
+ IntPtr handle = MsiInterop.MsiGetLastErrorRecord();
21
+ if (IntPtr.Zero != handle)
22
{
23
using (Record record = new Record(handle))
24
{
src/wix/WixToolset.Core.Native/Msi/MsiHandle.cs
+4
-4
@@ -15,7 +15,7 @@ namespace WixToolset.Core.Native.Msi
15
public abstract class MsiHandle : IDisposable
16
{
17
private bool disposed;
18
- private uint handle;
18
+ private IntPtr handle;
19
private int owningThread;
20
#if DEBUG
21
private string creationStack;
@@ -33,7 +33,7 @@ namespace WixToolset.Core.Native.Msi
33
/// Gets or sets the MSI handle.
34
/// </summary>
35
/// <value>The MSI handle.</value>
36
- internal uint Handle
36
+ internal IntPtr Handle
37
{
38
get
39
{
@@ -85,7 +85,7 @@ namespace WixToolset.Core.Native.Msi
85
{
86
if (!this.disposed)
87
{
88
- if (0 != this.handle)
88
+ if (IntPtr.Zero != this.handle)
89
{
90
if (Thread.CurrentThread.ManagedThreadId == this.owningThread)
91
{
@@ -94,7 +94,7 @@ namespace WixToolset.Core.Native.Msi
94
{
95
throw new Win32Exception(error);
96
}
97
- this.handle = 0;
97
+ this.handle = IntPtr.Zero;
98
}
99
else
100
{
src/wix/WixToolset.Core.Native/Msi/MsiInterop.cs
+30
-30
@@ -56,7 +56,7 @@ namespace WixToolset.Core.Native.Msi
56
/// <param name="database">Handle to a database.</param>
57
/// <returns>Error code.</returns>
58
[DllImport("msi.dll", EntryPoint = "MsiCloseHandle", CharSet = CharSet.Unicode, ExactSpelling = true)]
59
- internal static extern int MsiCloseHandle(uint database);
59
+ internal static extern int MsiCloseHandle(IntPtr database);
60
61
/// <summary>
62
/// PInvoke of MsiCreateRecord
@@ -64,7 +64,7 @@ namespace WixToolset.Core.Native.Msi
64
/// <param name="parameters">Count of columns in the record.</param>
65
/// <returns>Handle referencing the record.</returns>
66
[DllImport("msi.dll", EntryPoint = "MsiCreateRecord", CharSet = CharSet.Unicode, ExactSpelling = true)]
67
- internal static extern uint MsiCreateRecord(int parameters);
67
+ internal static extern IntPtr MsiCreateRecord(int parameters);
68
69
/// <summary>
70
/// Creates summary information of an existing transform to include validation and error conditions.
@@ -76,7 +76,7 @@ namespace WixToolset.Core.Native.Msi
76
/// <param name="validations">Specifies the properties to be validated to verify that the transform can be applied to the database.</param>
77
/// <returns>Error code.</returns>
78
[DllImport("msi.dll", EntryPoint = "MsiCreateTransformSummaryInfoW", CharSet = CharSet.Unicode, ExactSpelling = true)]
79
- internal static extern int MsiCreateTransformSummaryInfo(uint database, uint referenceDatabase, string transformFile, TransformErrorConditions errorConditions, TransformValidations validations);
79
+ internal static extern int MsiCreateTransformSummaryInfo(IntPtr database, IntPtr referenceDatabase, string transformFile, TransformErrorConditions errorConditions, TransformValidations validations);
80
81
/// <summary>
82
/// Applies a transform to a database.
@@ -86,7 +86,7 @@ namespace WixToolset.Core.Native.Msi
86
/// <param name="errorConditions">Error conditions that should be suppressed.</param>
87
/// <returns>Error code.</returns>
88
[DllImport("msi.dll", EntryPoint = "MsiDatabaseApplyTransformW", CharSet = CharSet.Unicode, ExactSpelling = true)]
89
- internal static extern int MsiDatabaseApplyTransform(uint database, string transformFile, TransformErrorConditions errorConditions);
89
+ internal static extern int MsiDatabaseApplyTransform(IntPtr database, string transformFile, TransformErrorConditions errorConditions);
90
91
/// <summary>
92
/// PInvoke of MsiDatabaseCommit.
@@ -94,7 +94,7 @@ namespace WixToolset.Core.Native.Msi
94
/// <param name="database">Handle to a databse.</param>
95
/// <returns>Error code.</returns>
96
[DllImport("msi.dll", EntryPoint = "MsiDatabaseCommit", CharSet = CharSet.Unicode, ExactSpelling = true)]
97
- internal static extern int MsiDatabaseCommit(uint database);
97
+ internal static extern int MsiDatabaseCommit(IntPtr database);
98
99
/// <summary>
100
/// PInvoke of MsiDatabaseExportW.
@@ -105,7 +105,7 @@ namespace WixToolset.Core.Native.Msi
105
/// <param name="fileName">File name.</param>
106
/// <returns>Error code.</returns>
107
[DllImport("msi.dll", EntryPoint = "MsiDatabaseExportW", CharSet = CharSet.Unicode, ExactSpelling = true)]
108
- internal static extern int MsiDatabaseExport(uint database, string tableName, string folderPath, string fileName);
108
+ internal static extern int MsiDatabaseExport(IntPtr database, string tableName, string folderPath, string fileName);
109
110
/// <summary>
111
/// Generates a transform file of differences between two databases.
@@ -120,7 +120,7 @@ namespace WixToolset.Core.Native.Msi
120
/// <param name="reserved2">This is a reserved argument and must be set to 0.</param>
121
/// <returns>Error code.</returns>
122
[DllImport("msi.dll", EntryPoint = "MsiDatabaseGenerateTransformW", CharSet = CharSet.Unicode, ExactSpelling = true)]
123
- internal static extern int MsiDatabaseGenerateTransform(uint database, uint databaseReference, string transformFile, int reserved1, int reserved2);
123
+ internal static extern int MsiDatabaseGenerateTransform(IntPtr database, IntPtr databaseReference, string transformFile, int reserved1, int reserved2);
124
125
/// <summary>
126
/// PInvoke of MsiDatabaseImportW.
@@ -130,7 +130,7 @@ namespace WixToolset.Core.Native.Msi
130
/// <param name="fileName">File name.</param>
131
/// <returns>Error code.</returns>
132
[DllImport("msi.dll", EntryPoint = "MsiDatabaseImportW", CharSet = CharSet.Unicode, ExactSpelling = true)]
133
- internal static extern int MsiDatabaseImport(uint database, string folderPath, string fileName);
133
+ internal static extern int MsiDatabaseImport(IntPtr database, string folderPath, string fileName);
134
135
/// <summary>
136
/// PInvoke of MsiDatabaseMergeW.
@@ -140,7 +140,7 @@ namespace WixToolset.Core.Native.Msi
140
/// <param name="tableName">The name of the table to receive merge conflict information.</param>
141
/// <returns>Error code.</returns>
142
[DllImport("msi.dll", EntryPoint = "MsiDatabaseMergeW", CharSet = CharSet.Unicode, ExactSpelling = true)]
143
- internal static extern int MsiDatabaseMerge(uint database, uint databaseMerge, string tableName);
143
+ internal static extern int MsiDatabaseMerge(IntPtr database, IntPtr databaseMerge, string tableName);
144
145
/// <summary>
146
/// PInvoke of MsiDatabaseOpenViewW.
@@ -150,7 +150,7 @@ namespace WixToolset.Core.Native.Msi
150
/// <param name="view">View handle.</param>
151
/// <returns>Error code.</returns>
152
[DllImport("msi.dll", EntryPoint = "MsiDatabaseOpenViewW", CharSet = CharSet.Unicode, ExactSpelling = true)]
153
- internal static extern int MsiDatabaseOpenView(uint database, string query, out uint view);
153
+ internal static extern int MsiDatabaseOpenView(IntPtr database, string query, out IntPtr view);
154
155
/// <summary>
156
/// PInvoke of MsiExtractPatchXMLDataW.
@@ -190,7 +190,7 @@ namespace WixToolset.Core.Native.Msi
190
/// </summary>
191
/// <returns>Handle to error record if one exists.</returns>
192
[DllImport("msi.dll", EntryPoint = "MsiGetLastErrorRecord", CharSet = CharSet.Unicode, ExactSpelling = true)]
193
- internal static extern uint MsiGetLastErrorRecord();
193
+ internal static extern IntPtr MsiGetLastErrorRecord();
194
195
/// <summary>
196
/// PInvoke of MsiDatabaseGetPrimaryKeysW.
@@ -200,7 +200,7 @@ namespace WixToolset.Core.Native.Msi
200
/// <param name="record">Handle to receive resulting record.</param>
201
/// <returns>Error code.</returns>
202
[DllImport("msi.dll", EntryPoint = "MsiDatabaseGetPrimaryKeysW", CharSet = CharSet.Unicode, ExactSpelling = true)]
203
- internal static extern int MsiDatabaseGetPrimaryKeys(uint database, string tableName, out uint record);
203
+ internal static extern int MsiDatabaseGetPrimaryKeys(IntPtr database, string tableName, out IntPtr record);
204
205
/// <summary>
206
/// PInvoke of MsiDoActionW.
@@ -210,7 +210,7 @@ namespace WixToolset.Core.Native.Msi
210
/// <param name="action">Specifies the action to execute.</param>
211
/// <returns>Error code.</returns>
212
[DllImport("msi.dll", EntryPoint = "MsiDoActionW", CharSet = CharSet.Unicode, ExactSpelling = true)]
213
- internal static extern int MsiDoAction(uint product, string action);
213
+ internal static extern int MsiDoAction(IntPtr product, string action);
214
215
/// <summary>
216
/// PInvoke of MsiGetSummaryInformationW. Can use either database handle or database path as input.
@@ -221,7 +221,7 @@ namespace WixToolset.Core.Native.Msi
221
/// <param name="summaryInfo">Handle to summary information.</param>
222
/// <returns>Error code.</returns>
223
[DllImport("msi.dll", EntryPoint = "MsiGetSummaryInformationW", CharSet = CharSet.Unicode, ExactSpelling = true)]
224
- internal static extern int MsiGetSummaryInformation(uint database, string databasePath, uint updateCount, ref uint summaryInfo);
224
+ internal static extern int MsiGetSummaryInformation(IntPtr database, string databasePath, uint updateCount, ref IntPtr summaryInfo);
225
226
/// <summary>
227
/// PInvoke of MsiDatabaseIsTablePersitentW.
@@ -230,7 +230,7 @@ namespace WixToolset.Core.Native.Msi
230
/// <param name="tableName">Table name.</param>
231
/// <returns>MSICONDITION</returns>
232
[DllImport("msi.dll", EntryPoint = "MsiDatabaseIsTablePersistentW", CharSet = CharSet.Unicode, ExactSpelling = true)]
233
- internal static extern int MsiDatabaseIsTablePersistent(uint database, string tableName);
233
+ internal static extern int MsiDatabaseIsTablePersistent(IntPtr database, string tableName);
234
235
/// <summary>
236
/// PInvoke of MsiOpenDatabaseW.
@@ -240,7 +240,7 @@ namespace WixToolset.Core.Native.Msi
240
/// <param name="database">Handle to database.</param>
241
/// <returns>Error code.</returns>
242
[DllImport("msi.dll", EntryPoint = "MsiOpenDatabaseW", CharSet = CharSet.Unicode, ExactSpelling = true)]
243
- internal static extern int MsiOpenDatabase(string databasePath, IntPtr persist, out uint database);
243
+ internal static extern int MsiOpenDatabase(string databasePath, IntPtr persist, out IntPtr database);
244
245
/// <summary>
246
/// PInvoke of MsiOpenPackageW.
@@ -249,7 +249,7 @@ namespace WixToolset.Core.Native.Msi
249
/// <param name="product">A pointer to a variable that receives the product handle.</param>
250
/// <returns>Error code.</returns>
251
[DllImport("msi.dll", EntryPoint = "MsiOpenPackageW", CharSet = CharSet.Unicode, ExactSpelling = true)]
252
- internal static extern int MsiOpenPackage(string packagePath, out uint product);
252
+ internal static extern int MsiOpenPackage(string packagePath, out IntPtr product);
253
254
/// <summary>
255
/// PInvoke of MsiRecordIsNull.
@@ -258,7 +258,7 @@ namespace WixToolset.Core.Native.Msi
258
/// <param name="field">Index of field to check for null value.</param>
259
/// <returns>true if the field is null, false if not, and an error code for any error.</returns>
260
[DllImport("msi.dll", EntryPoint = "MsiRecordIsNull", CharSet = CharSet.Unicode, ExactSpelling = true)]
261
- internal static extern int MsiRecordIsNull(uint record, int field);
261
+ internal static extern int MsiRecordIsNull(IntPtr record, int field);
262
263
/// <summary>
264
/// PInvoke of MsiRecordGetInteger.
@@ -267,7 +267,7 @@ namespace WixToolset.Core.Native.Msi
267
/// <param name="field">Index of field to retrieve integer from.</param>
268
/// <returns>Integer value.</returns>
269
[DllImport("msi.dll", EntryPoint = "MsiRecordGetInteger", CharSet = CharSet.Unicode, ExactSpelling = true)]
270
- internal static extern int MsiRecordGetInteger(uint record, int field);
270
+ internal static extern int MsiRecordGetInteger(IntPtr record, int field);
271
272
/// <summary>
273
/// PInvoke of MsiRectordSetInteger.
@@ -277,7 +277,7 @@ namespace WixToolset.Core.Native.Msi
277
/// <param name="value">Value to set field to.</param>
278
/// <returns>Error code.</returns>
279
[DllImport("msi.dll", EntryPoint = "MsiRecordSetInteger", CharSet = CharSet.Unicode, ExactSpelling = true)]
280
- internal static extern int MsiRecordSetInteger(uint record, int field, int value);
280
+ internal static extern int MsiRecordSetInteger(IntPtr record, int field, int value);
281
282
/// <summary>
283
/// PInvoke of MsiRecordGetStringW.
@@ -288,7 +288,7 @@ namespace WixToolset.Core.Native.Msi
288
/// <param name="valueBufSize">Size of buffer.</param>
289
/// <returns>Error code.</returns>
290
[DllImport("msi.dll", EntryPoint = "MsiRecordGetStringW", CharSet = CharSet.Unicode, ExactSpelling = true)]
291
- internal static extern int MsiRecordGetString(uint record, int field, StringBuilder valueBuf, ref int valueBufSize);
291
+ internal static extern int MsiRecordGetString(IntPtr record, int field, StringBuilder valueBuf, ref int valueBufSize);
292
293
/// <summary>
294
/// PInvoke of MsiRecordSetStringW.
@@ -298,7 +298,7 @@ namespace WixToolset.Core.Native.Msi
298
/// <param name="value">String value.</param>
299
/// <returns>Error code.</returns>
300
[DllImport("msi.dll", EntryPoint = "MsiRecordSetStringW", CharSet = CharSet.Unicode, ExactSpelling = true)]
301
- internal static extern int MsiRecordSetString(uint record, int field, string value);
301
+ internal static extern int MsiRecordSetString(IntPtr record, int field, string value);
302
303
/// <summary>
304
/// PInvoke of MsiRecordSetStreamW.
@@ -308,7 +308,7 @@ namespace WixToolset.Core.Native.Msi
308
/// <param name="filePath">Path to file to set stream value to.</param>
309
/// <returns>Error code.</returns>
310
[DllImport("msi.dll", EntryPoint = "MsiRecordSetStreamW", CharSet = CharSet.Unicode, ExactSpelling = true)]
311
- internal static extern int MsiRecordSetStream(uint record, int field, string filePath);
311
+ internal static extern int MsiRecordSetStream(IntPtr record, int field, string filePath);
312
313
/// <summary>
314
/// PInvoke of MsiRecordReadStreamW.
@@ -319,7 +319,7 @@ namespace WixToolset.Core.Native.Msi
319
/// <param name="dataBufSize">Size of data buffer.</param>
320
/// <returns>Error code.</returns>
321
[DllImport("msi.dll", EntryPoint = "MsiRecordReadStream", CharSet = CharSet.Unicode, ExactSpelling = true)]
322
- internal static extern int MsiRecordReadStream(uint record, int field, byte[] dataBuf, ref int dataBufSize);
322
+ internal static extern int MsiRecordReadStream(IntPtr record, int field, byte[] dataBuf, ref int dataBufSize);
323
324
/// <summary>
325
/// PInvoke of MsiRecordGetFieldCount.
@@ -327,7 +327,7 @@ namespace WixToolset.Core.Native.Msi
327
/// <param name="record">MSI Record handle.</param>
328
/// <returns>Count of fields in the record.</returns>
329
[DllImport("msi.dll", EntryPoint = "MsiRecordGetFieldCount", CharSet = CharSet.Unicode, ExactSpelling = true)]
330
- internal static extern int MsiRecordGetFieldCount(uint record);
330
+ internal static extern int MsiRecordGetFieldCount(IntPtr record);
331
332
/// <summary>
333
/// PInvoke of MsiSetExternalUIW.
@@ -365,7 +365,7 @@ namespace WixToolset.Core.Native.Msi
365
/// <param name="stringValueBufSize">Size of string buffer.</param>
366
/// <returns>Error code.</returns>
367
[DllImport("msi.dll", EntryPoint = "MsiSummaryInfoGetPropertyW", CharSet = CharSet.Unicode, ExactSpelling = true)]
368
- internal static extern int MsiSummaryInfoGetProperty(uint summaryInfo, int property, out uint dataType, out int integerValue, ref System.Runtime.InteropServices.ComTypes.FILETIME fileTimeValue, StringBuilder stringValueBuf, ref int stringValueBufSize);
368
+ internal static extern int MsiSummaryInfoGetProperty(IntPtr summaryInfo, int property, out uint dataType, out int integerValue, ref System.Runtime.InteropServices.ComTypes.FILETIME fileTimeValue, StringBuilder stringValueBuf, ref int stringValueBufSize);
369
370
/// <summary>
371
/// PInvoke of MsiViewGetColumnInfo.
@@ -375,7 +375,7 @@ namespace WixToolset.Core.Native.Msi
375
/// <param name="record">Handle for returned record.</param>
376
/// <returns>Error code.</returns>
377
[DllImport("msi.dll", EntryPoint = "MsiViewGetColumnInfo", CharSet = CharSet.Unicode, ExactSpelling = true)]
378
- internal static extern int MsiViewGetColumnInfo(uint view, int columnInfo, out uint record);
378
+ internal static extern int MsiViewGetColumnInfo(IntPtr view, int columnInfo, out IntPtr record);
379
380
/// <summary>
381
/// PInvoke of MsiViewExecute.
@@ -384,7 +384,7 @@ namespace WixToolset.Core.Native.Msi
384
/// <param name="record">Handle to a record that supplies the parameters for the view.</param>
385
/// <returns>Error code.</returns>
386
[DllImport("msi.dll", EntryPoint = "MsiViewExecute", CharSet = CharSet.Unicode, ExactSpelling = true)]
387
- internal static extern int MsiViewExecute(uint view, uint record);
387
+ internal static extern int MsiViewExecute(IntPtr view, IntPtr record);
388
389
/// <summary>
390
/// PInvoke of MsiViewFetch.
@@ -393,7 +393,7 @@ namespace WixToolset.Core.Native.Msi
393
/// <param name="record">Handle to receive record info.</param>
394
/// <returns>Error code.</returns>
395
[DllImport("msi.dll", EntryPoint = "MsiViewFetch", CharSet = CharSet.Unicode, ExactSpelling = true)]
396
- internal static extern int MsiViewFetch(uint view, out uint record);
396
+ internal static extern int MsiViewFetch(IntPtr view, out IntPtr record);
397
398
/// <summary>
399
/// PInvoke of MsiViewModify.
@@ -403,6 +403,6 @@ namespace WixToolset.Core.Native.Msi
403
/// <param name="record">Handle of record.</param>
404
/// <returns>Error code.</returns>
405
[DllImport("msi.dll", EntryPoint = "MsiViewModify", CharSet = CharSet.Unicode, ExactSpelling = true)]
406
- internal static extern int MsiViewModify(uint view, int modifyMode, uint record);
406
+ internal static extern int MsiViewModify(IntPtr view, int modifyMode, IntPtr record);
407
}
408
}
src/wix/WixToolset.Core.Native/Msi/Record.cs
+2
-2
@@ -18,7 +18,7 @@ namespace WixToolset.Core.Native.Msi
18
public Record(int fieldCount)
19
{
20
this.Handle = MsiInterop.MsiCreateRecord(fieldCount);
21
- if (0 == this.Handle)
21
+ if (IntPtr.Zero == this.Handle)
22
{
23
throw new OutOfMemoryException();
24
}
@@ -28,7 +28,7 @@ namespace WixToolset.Core.Native.Msi
28
/// Creates a record from a handle.
29
/// </summary>
30
/// <param name="handle">Handle to create record from.</param>
31
- internal Record(uint handle)
31
+ internal Record(IntPtr handle)
32
{
33
this.Handle = handle;
34
}
src/wix/WixToolset.Core.Native/Msi/Session.cs
+1
-3
@@ -2,8 +2,6 @@
2
3
namespace WixToolset.Core.Native.Msi
4
{
5
- using System.Globalization;
6
-
5
/// <summary>
6
/// Controls the installation process.
7
/// </summary>
@@ -15,7 +13,7 @@ namespace WixToolset.Core.Native.Msi
13
/// <param name="database">The database to open.</param>
14
public Session(Database database)
15
{
18
- var packagePath = "#" + database.Handle.ToString(CultureInfo.InvariantCulture);
16
+ var packagePath = "#" + database.Handle;
17
18
var error = MsiInterop.MsiOpenPackage(packagePath, out var handle);
19
if (0 != error)
src/wix/WixToolset.Core.Native/Msi/SummaryInformation.cs
+3
-3
@@ -200,7 +200,7 @@ namespace WixToolset.Core.Native.Msi
200
throw new ArgumentNullException(nameof(db));
201
}
202
203
- uint handle = 0;
203
+ var handle = IntPtr.Zero;
204
var error = MsiInterop.MsiGetSummaryInformation(db.Handle, null, 0, ref handle);
205
if (0 != error)
206
{
@@ -220,8 +220,8 @@ namespace WixToolset.Core.Native.Msi
220
throw new ArgumentNullException(nameof(databaseFile));
221
}
222
223
- uint handle = 0;
224
- var error = MsiInterop.MsiGetSummaryInformation(0, databaseFile, 0, ref handle);
223
+ var handle = IntPtr.Zero;
224
+ var error = MsiInterop.MsiGetSummaryInformation(IntPtr.Zero, databaseFile, 0, ref handle);
225
if (0 != error)
226
{
227
throw new MsiException(error);
src/wix/WixToolset.Core.Native/Msi/View.cs
+1
-1
@@ -58,7 +58,7 @@ namespace WixToolset.Core.Native.Msi
58
/// <param name="record">Record containing parameters to be substituded into the view.</param>
59
public void Execute(Record record)
60
{
61
- var error = MsiInterop.MsiViewExecute(this.Handle, null == record ? 0 : record.Handle);
61
+ var error = MsiInterop.MsiViewExecute(this.Handle, null == record ? IntPtr.Zero : record.Handle);
62
if (0 != error)
63
{
64
throw new MsiException(error);