From a5f672ec734df2bbc7559e5192f6c10d273215e1 Mon Sep 17 00:00:00 2001 From: Rob Mensching Date: Mon, 29 Aug 2022 13:13:43 -0700 Subject: Use IntPtr for pointers to handles This is arguably more correct than raw uints. --- src/wix/WixToolset.Core.Native/Msi/Database.cs | 2 +- src/wix/WixToolset.Core.Native/Msi/MsiException.cs | 4 +- src/wix/WixToolset.Core.Native/Msi/MsiHandle.cs | 8 +-- src/wix/WixToolset.Core.Native/Msi/MsiInterop.cs | 60 +++++++++++----------- src/wix/WixToolset.Core.Native/Msi/Record.cs | 4 +- src/wix/WixToolset.Core.Native/Msi/Session.cs | 4 +- .../Msi/SummaryInformation.cs | 6 +-- src/wix/WixToolset.Core.Native/Msi/View.cs | 2 +- 8 files changed, 44 insertions(+), 46 deletions(-) diff --git a/src/wix/WixToolset.Core.Native/Msi/Database.cs b/src/wix/WixToolset.Core.Native/Msi/Database.cs index b9c5c35b..18e5066d 100644 --- a/src/wix/WixToolset.Core.Native/Msi/Database.cs +++ b/src/wix/WixToolset.Core.Native/Msi/Database.cs @@ -20,7 +20,7 @@ namespace WixToolset.Core.Native.Msi /// Persist mode to use when opening the database. public Database(string path, OpenDatabase type) { - var error = MsiInterop.MsiOpenDatabase(path, new IntPtr((int)type), out var handle); + var error = MsiInterop.MsiOpenDatabase(path, (IntPtr)type, out var handle); if (0 != error) { throw new MsiException(error); diff --git a/src/wix/WixToolset.Core.Native/Msi/MsiException.cs b/src/wix/WixToolset.Core.Native/Msi/MsiException.cs index 07c83d81..218d2a7c 100644 --- a/src/wix/WixToolset.Core.Native/Msi/MsiException.cs +++ b/src/wix/WixToolset.Core.Native/Msi/MsiException.cs @@ -17,8 +17,8 @@ namespace WixToolset.Core.Native.Msi /// The error code from the MsiXxx() function call. public MsiException(int error) : base(error) { - uint handle = MsiInterop.MsiGetLastErrorRecord(); - if (0 != handle) + IntPtr handle = MsiInterop.MsiGetLastErrorRecord(); + if (IntPtr.Zero != handle) { using (Record record = new Record(handle)) { diff --git a/src/wix/WixToolset.Core.Native/Msi/MsiHandle.cs b/src/wix/WixToolset.Core.Native/Msi/MsiHandle.cs index dc2ce605..0bfd555c 100644 --- a/src/wix/WixToolset.Core.Native/Msi/MsiHandle.cs +++ b/src/wix/WixToolset.Core.Native/Msi/MsiHandle.cs @@ -15,7 +15,7 @@ namespace WixToolset.Core.Native.Msi public abstract class MsiHandle : IDisposable { private bool disposed; - private uint handle; + private IntPtr handle; private int owningThread; #if DEBUG private string creationStack; @@ -33,7 +33,7 @@ namespace WixToolset.Core.Native.Msi /// Gets or sets the MSI handle. /// /// The MSI handle. - internal uint Handle + internal IntPtr Handle { get { @@ -85,7 +85,7 @@ namespace WixToolset.Core.Native.Msi { if (!this.disposed) { - if (0 != this.handle) + if (IntPtr.Zero != this.handle) { if (Thread.CurrentThread.ManagedThreadId == this.owningThread) { @@ -94,7 +94,7 @@ namespace WixToolset.Core.Native.Msi { throw new Win32Exception(error); } - this.handle = 0; + this.handle = IntPtr.Zero; } else { diff --git a/src/wix/WixToolset.Core.Native/Msi/MsiInterop.cs b/src/wix/WixToolset.Core.Native/Msi/MsiInterop.cs index 11ac4094..4df28ed6 100644 --- a/src/wix/WixToolset.Core.Native/Msi/MsiInterop.cs +++ b/src/wix/WixToolset.Core.Native/Msi/MsiInterop.cs @@ -56,7 +56,7 @@ namespace WixToolset.Core.Native.Msi /// Handle to a database. /// Error code. [DllImport("msi.dll", EntryPoint = "MsiCloseHandle", CharSet = CharSet.Unicode, ExactSpelling = true)] - internal static extern int MsiCloseHandle(uint database); + internal static extern int MsiCloseHandle(IntPtr database); /// /// PInvoke of MsiCreateRecord @@ -64,7 +64,7 @@ namespace WixToolset.Core.Native.Msi /// Count of columns in the record. /// Handle referencing the record. [DllImport("msi.dll", EntryPoint = "MsiCreateRecord", CharSet = CharSet.Unicode, ExactSpelling = true)] - internal static extern uint MsiCreateRecord(int parameters); + internal static extern IntPtr MsiCreateRecord(int parameters); /// /// Creates summary information of an existing transform to include validation and error conditions. @@ -76,7 +76,7 @@ namespace WixToolset.Core.Native.Msi /// Specifies the properties to be validated to verify that the transform can be applied to the database. /// Error code. [DllImport("msi.dll", EntryPoint = "MsiCreateTransformSummaryInfoW", CharSet = CharSet.Unicode, ExactSpelling = true)] - internal static extern int MsiCreateTransformSummaryInfo(uint database, uint referenceDatabase, string transformFile, TransformErrorConditions errorConditions, TransformValidations validations); + internal static extern int MsiCreateTransformSummaryInfo(IntPtr database, IntPtr referenceDatabase, string transformFile, TransformErrorConditions errorConditions, TransformValidations validations); /// /// Applies a transform to a database. @@ -86,7 +86,7 @@ namespace WixToolset.Core.Native.Msi /// Error conditions that should be suppressed. /// Error code. [DllImport("msi.dll", EntryPoint = "MsiDatabaseApplyTransformW", CharSet = CharSet.Unicode, ExactSpelling = true)] - internal static extern int MsiDatabaseApplyTransform(uint database, string transformFile, TransformErrorConditions errorConditions); + internal static extern int MsiDatabaseApplyTransform(IntPtr database, string transformFile, TransformErrorConditions errorConditions); /// /// PInvoke of MsiDatabaseCommit. @@ -94,7 +94,7 @@ namespace WixToolset.Core.Native.Msi /// Handle to a databse. /// Error code. [DllImport("msi.dll", EntryPoint = "MsiDatabaseCommit", CharSet = CharSet.Unicode, ExactSpelling = true)] - internal static extern int MsiDatabaseCommit(uint database); + internal static extern int MsiDatabaseCommit(IntPtr database); /// /// PInvoke of MsiDatabaseExportW. @@ -105,7 +105,7 @@ namespace WixToolset.Core.Native.Msi /// File name. /// Error code. [DllImport("msi.dll", EntryPoint = "MsiDatabaseExportW", CharSet = CharSet.Unicode, ExactSpelling = true)] - internal static extern int MsiDatabaseExport(uint database, string tableName, string folderPath, string fileName); + internal static extern int MsiDatabaseExport(IntPtr database, string tableName, string folderPath, string fileName); /// /// Generates a transform file of differences between two databases. @@ -120,7 +120,7 @@ namespace WixToolset.Core.Native.Msi /// This is a reserved argument and must be set to 0. /// Error code. [DllImport("msi.dll", EntryPoint = "MsiDatabaseGenerateTransformW", CharSet = CharSet.Unicode, ExactSpelling = true)] - internal static extern int MsiDatabaseGenerateTransform(uint database, uint databaseReference, string transformFile, int reserved1, int reserved2); + internal static extern int MsiDatabaseGenerateTransform(IntPtr database, IntPtr databaseReference, string transformFile, int reserved1, int reserved2); /// /// PInvoke of MsiDatabaseImportW. @@ -130,7 +130,7 @@ namespace WixToolset.Core.Native.Msi /// File name. /// Error code. [DllImport("msi.dll", EntryPoint = "MsiDatabaseImportW", CharSet = CharSet.Unicode, ExactSpelling = true)] - internal static extern int MsiDatabaseImport(uint database, string folderPath, string fileName); + internal static extern int MsiDatabaseImport(IntPtr database, string folderPath, string fileName); /// /// PInvoke of MsiDatabaseMergeW. @@ -140,7 +140,7 @@ namespace WixToolset.Core.Native.Msi /// The name of the table to receive merge conflict information. /// Error code. [DllImport("msi.dll", EntryPoint = "MsiDatabaseMergeW", CharSet = CharSet.Unicode, ExactSpelling = true)] - internal static extern int MsiDatabaseMerge(uint database, uint databaseMerge, string tableName); + internal static extern int MsiDatabaseMerge(IntPtr database, IntPtr databaseMerge, string tableName); /// /// PInvoke of MsiDatabaseOpenViewW. @@ -150,7 +150,7 @@ namespace WixToolset.Core.Native.Msi /// View handle. /// Error code. [DllImport("msi.dll", EntryPoint = "MsiDatabaseOpenViewW", CharSet = CharSet.Unicode, ExactSpelling = true)] - internal static extern int MsiDatabaseOpenView(uint database, string query, out uint view); + internal static extern int MsiDatabaseOpenView(IntPtr database, string query, out IntPtr view); /// /// PInvoke of MsiExtractPatchXMLDataW. @@ -190,7 +190,7 @@ namespace WixToolset.Core.Native.Msi /// /// Handle to error record if one exists. [DllImport("msi.dll", EntryPoint = "MsiGetLastErrorRecord", CharSet = CharSet.Unicode, ExactSpelling = true)] - internal static extern uint MsiGetLastErrorRecord(); + internal static extern IntPtr MsiGetLastErrorRecord(); /// /// PInvoke of MsiDatabaseGetPrimaryKeysW. @@ -200,7 +200,7 @@ namespace WixToolset.Core.Native.Msi /// Handle to receive resulting record. /// Error code. [DllImport("msi.dll", EntryPoint = "MsiDatabaseGetPrimaryKeysW", CharSet = CharSet.Unicode, ExactSpelling = true)] - internal static extern int MsiDatabaseGetPrimaryKeys(uint database, string tableName, out uint record); + internal static extern int MsiDatabaseGetPrimaryKeys(IntPtr database, string tableName, out IntPtr record); /// /// PInvoke of MsiDoActionW. @@ -210,7 +210,7 @@ namespace WixToolset.Core.Native.Msi /// Specifies the action to execute. /// Error code. [DllImport("msi.dll", EntryPoint = "MsiDoActionW", CharSet = CharSet.Unicode, ExactSpelling = true)] - internal static extern int MsiDoAction(uint product, string action); + internal static extern int MsiDoAction(IntPtr product, string action); /// /// PInvoke of MsiGetSummaryInformationW. Can use either database handle or database path as input. @@ -221,7 +221,7 @@ namespace WixToolset.Core.Native.Msi /// Handle to summary information. /// Error code. [DllImport("msi.dll", EntryPoint = "MsiGetSummaryInformationW", CharSet = CharSet.Unicode, ExactSpelling = true)] - internal static extern int MsiGetSummaryInformation(uint database, string databasePath, uint updateCount, ref uint summaryInfo); + internal static extern int MsiGetSummaryInformation(IntPtr database, string databasePath, uint updateCount, ref IntPtr summaryInfo); /// /// PInvoke of MsiDatabaseIsTablePersitentW. @@ -230,7 +230,7 @@ namespace WixToolset.Core.Native.Msi /// Table name. /// MSICONDITION [DllImport("msi.dll", EntryPoint = "MsiDatabaseIsTablePersistentW", CharSet = CharSet.Unicode, ExactSpelling = true)] - internal static extern int MsiDatabaseIsTablePersistent(uint database, string tableName); + internal static extern int MsiDatabaseIsTablePersistent(IntPtr database, string tableName); /// /// PInvoke of MsiOpenDatabaseW. @@ -240,7 +240,7 @@ namespace WixToolset.Core.Native.Msi /// Handle to database. /// Error code. [DllImport("msi.dll", EntryPoint = "MsiOpenDatabaseW", CharSet = CharSet.Unicode, ExactSpelling = true)] - internal static extern int MsiOpenDatabase(string databasePath, IntPtr persist, out uint database); + internal static extern int MsiOpenDatabase(string databasePath, IntPtr persist, out IntPtr database); /// /// PInvoke of MsiOpenPackageW. @@ -249,7 +249,7 @@ namespace WixToolset.Core.Native.Msi /// A pointer to a variable that receives the product handle. /// Error code. [DllImport("msi.dll", EntryPoint = "MsiOpenPackageW", CharSet = CharSet.Unicode, ExactSpelling = true)] - internal static extern int MsiOpenPackage(string packagePath, out uint product); + internal static extern int MsiOpenPackage(string packagePath, out IntPtr product); /// /// PInvoke of MsiRecordIsNull. @@ -258,7 +258,7 @@ namespace WixToolset.Core.Native.Msi /// Index of field to check for null value. /// true if the field is null, false if not, and an error code for any error. [DllImport("msi.dll", EntryPoint = "MsiRecordIsNull", CharSet = CharSet.Unicode, ExactSpelling = true)] - internal static extern int MsiRecordIsNull(uint record, int field); + internal static extern int MsiRecordIsNull(IntPtr record, int field); /// /// PInvoke of MsiRecordGetInteger. @@ -267,7 +267,7 @@ namespace WixToolset.Core.Native.Msi /// Index of field to retrieve integer from. /// Integer value. [DllImport("msi.dll", EntryPoint = "MsiRecordGetInteger", CharSet = CharSet.Unicode, ExactSpelling = true)] - internal static extern int MsiRecordGetInteger(uint record, int field); + internal static extern int MsiRecordGetInteger(IntPtr record, int field); /// /// PInvoke of MsiRectordSetInteger. @@ -277,7 +277,7 @@ namespace WixToolset.Core.Native.Msi /// Value to set field to. /// Error code. [DllImport("msi.dll", EntryPoint = "MsiRecordSetInteger", CharSet = CharSet.Unicode, ExactSpelling = true)] - internal static extern int MsiRecordSetInteger(uint record, int field, int value); + internal static extern int MsiRecordSetInteger(IntPtr record, int field, int value); /// /// PInvoke of MsiRecordGetStringW. @@ -288,7 +288,7 @@ namespace WixToolset.Core.Native.Msi /// Size of buffer. /// Error code. [DllImport("msi.dll", EntryPoint = "MsiRecordGetStringW", CharSet = CharSet.Unicode, ExactSpelling = true)] - internal static extern int MsiRecordGetString(uint record, int field, StringBuilder valueBuf, ref int valueBufSize); + internal static extern int MsiRecordGetString(IntPtr record, int field, StringBuilder valueBuf, ref int valueBufSize); /// /// PInvoke of MsiRecordSetStringW. @@ -298,7 +298,7 @@ namespace WixToolset.Core.Native.Msi /// String value. /// Error code. [DllImport("msi.dll", EntryPoint = "MsiRecordSetStringW", CharSet = CharSet.Unicode, ExactSpelling = true)] - internal static extern int MsiRecordSetString(uint record, int field, string value); + internal static extern int MsiRecordSetString(IntPtr record, int field, string value); /// /// PInvoke of MsiRecordSetStreamW. @@ -308,7 +308,7 @@ namespace WixToolset.Core.Native.Msi /// Path to file to set stream value to. /// Error code. [DllImport("msi.dll", EntryPoint = "MsiRecordSetStreamW", CharSet = CharSet.Unicode, ExactSpelling = true)] - internal static extern int MsiRecordSetStream(uint record, int field, string filePath); + internal static extern int MsiRecordSetStream(IntPtr record, int field, string filePath); /// /// PInvoke of MsiRecordReadStreamW. @@ -319,7 +319,7 @@ namespace WixToolset.Core.Native.Msi /// Size of data buffer. /// Error code. [DllImport("msi.dll", EntryPoint = "MsiRecordReadStream", CharSet = CharSet.Unicode, ExactSpelling = true)] - internal static extern int MsiRecordReadStream(uint record, int field, byte[] dataBuf, ref int dataBufSize); + internal static extern int MsiRecordReadStream(IntPtr record, int field, byte[] dataBuf, ref int dataBufSize); /// /// PInvoke of MsiRecordGetFieldCount. @@ -327,7 +327,7 @@ namespace WixToolset.Core.Native.Msi /// MSI Record handle. /// Count of fields in the record. [DllImport("msi.dll", EntryPoint = "MsiRecordGetFieldCount", CharSet = CharSet.Unicode, ExactSpelling = true)] - internal static extern int MsiRecordGetFieldCount(uint record); + internal static extern int MsiRecordGetFieldCount(IntPtr record); /// /// PInvoke of MsiSetExternalUIW. @@ -365,7 +365,7 @@ namespace WixToolset.Core.Native.Msi /// Size of string buffer. /// Error code. [DllImport("msi.dll", EntryPoint = "MsiSummaryInfoGetPropertyW", CharSet = CharSet.Unicode, ExactSpelling = true)] - 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); + 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); /// /// PInvoke of MsiViewGetColumnInfo. @@ -375,7 +375,7 @@ namespace WixToolset.Core.Native.Msi /// Handle for returned record. /// Error code. [DllImport("msi.dll", EntryPoint = "MsiViewGetColumnInfo", CharSet = CharSet.Unicode, ExactSpelling = true)] - internal static extern int MsiViewGetColumnInfo(uint view, int columnInfo, out uint record); + internal static extern int MsiViewGetColumnInfo(IntPtr view, int columnInfo, out IntPtr record); /// /// PInvoke of MsiViewExecute. @@ -384,7 +384,7 @@ namespace WixToolset.Core.Native.Msi /// Handle to a record that supplies the parameters for the view. /// Error code. [DllImport("msi.dll", EntryPoint = "MsiViewExecute", CharSet = CharSet.Unicode, ExactSpelling = true)] - internal static extern int MsiViewExecute(uint view, uint record); + internal static extern int MsiViewExecute(IntPtr view, IntPtr record); /// /// PInvoke of MsiViewFetch. @@ -393,7 +393,7 @@ namespace WixToolset.Core.Native.Msi /// Handle to receive record info. /// Error code. [DllImport("msi.dll", EntryPoint = "MsiViewFetch", CharSet = CharSet.Unicode, ExactSpelling = true)] - internal static extern int MsiViewFetch(uint view, out uint record); + internal static extern int MsiViewFetch(IntPtr view, out IntPtr record); /// /// PInvoke of MsiViewModify. @@ -403,6 +403,6 @@ namespace WixToolset.Core.Native.Msi /// Handle of record. /// Error code. [DllImport("msi.dll", EntryPoint = "MsiViewModify", CharSet = CharSet.Unicode, ExactSpelling = true)] - internal static extern int MsiViewModify(uint view, int modifyMode, uint record); + internal static extern int MsiViewModify(IntPtr view, int modifyMode, IntPtr record); } } diff --git a/src/wix/WixToolset.Core.Native/Msi/Record.cs b/src/wix/WixToolset.Core.Native/Msi/Record.cs index c25e76e2..9a201fa6 100644 --- a/src/wix/WixToolset.Core.Native/Msi/Record.cs +++ b/src/wix/WixToolset.Core.Native/Msi/Record.cs @@ -18,7 +18,7 @@ namespace WixToolset.Core.Native.Msi public Record(int fieldCount) { this.Handle = MsiInterop.MsiCreateRecord(fieldCount); - if (0 == this.Handle) + if (IntPtr.Zero == this.Handle) { throw new OutOfMemoryException(); } @@ -28,7 +28,7 @@ namespace WixToolset.Core.Native.Msi /// Creates a record from a handle. /// /// Handle to create record from. - internal Record(uint handle) + internal Record(IntPtr handle) { this.Handle = handle; } diff --git a/src/wix/WixToolset.Core.Native/Msi/Session.cs b/src/wix/WixToolset.Core.Native/Msi/Session.cs index 743fb2be..ae9cb0ba 100644 --- a/src/wix/WixToolset.Core.Native/Msi/Session.cs +++ b/src/wix/WixToolset.Core.Native/Msi/Session.cs @@ -2,8 +2,6 @@ namespace WixToolset.Core.Native.Msi { - using System.Globalization; - /// /// Controls the installation process. /// @@ -15,7 +13,7 @@ namespace WixToolset.Core.Native.Msi /// The database to open. public Session(Database database) { - var packagePath = "#" + database.Handle.ToString(CultureInfo.InvariantCulture); + var packagePath = "#" + database.Handle; var error = MsiInterop.MsiOpenPackage(packagePath, out var handle); if (0 != error) diff --git a/src/wix/WixToolset.Core.Native/Msi/SummaryInformation.cs b/src/wix/WixToolset.Core.Native/Msi/SummaryInformation.cs index da629df2..3b3dea0f 100644 --- a/src/wix/WixToolset.Core.Native/Msi/SummaryInformation.cs +++ b/src/wix/WixToolset.Core.Native/Msi/SummaryInformation.cs @@ -200,7 +200,7 @@ namespace WixToolset.Core.Native.Msi throw new ArgumentNullException(nameof(db)); } - uint handle = 0; + var handle = IntPtr.Zero; var error = MsiInterop.MsiGetSummaryInformation(db.Handle, null, 0, ref handle); if (0 != error) { @@ -220,8 +220,8 @@ namespace WixToolset.Core.Native.Msi throw new ArgumentNullException(nameof(databaseFile)); } - uint handle = 0; - var error = MsiInterop.MsiGetSummaryInformation(0, databaseFile, 0, ref handle); + var handle = IntPtr.Zero; + var error = MsiInterop.MsiGetSummaryInformation(IntPtr.Zero, databaseFile, 0, ref handle); if (0 != error) { throw new MsiException(error); diff --git a/src/wix/WixToolset.Core.Native/Msi/View.cs b/src/wix/WixToolset.Core.Native/Msi/View.cs index 6305a9de..f255190c 100644 --- a/src/wix/WixToolset.Core.Native/Msi/View.cs +++ b/src/wix/WixToolset.Core.Native/Msi/View.cs @@ -58,7 +58,7 @@ namespace WixToolset.Core.Native.Msi /// Record containing parameters to be substituded into the view. public void Execute(Record record) { - var error = MsiInterop.MsiViewExecute(this.Handle, null == record ? 0 : record.Handle); + var error = MsiInterop.MsiViewExecute(this.Handle, null == record ? IntPtr.Zero : record.Handle); if (0 != error) { throw new MsiException(error); -- cgit v1.2.3-55-g6feb