diff --git a/PInvoke/CfgMgr32/CfgMgr32_3.cs b/PInvoke/CfgMgr32/CfgMgr32_3.cs index 5d71ef13..264559f2 100644 --- a/PInvoke/CfgMgr32/CfgMgr32_3.cs +++ b/PInvoke/CfgMgr32/CfgMgr32_3.cs @@ -24,7 +24,7 @@ namespace Vanara.PInvoke /// cref="Win32Error.ERROR_SUCCESS"/>. The callback should not return any other values. /// [UnmanagedFunctionPointer(CallingConvention.Winapi)] - public delegate Win32Error CM_NOTIFY_CALLBACK(HCMNOTIFICATION notify, [Optional] IntPtr context, CM_NOTIFY_ACTION action, in CM_NOTIFY_EVENT_DATA eventData, uint eventDataSize); + public delegate Win32Error CM_NOTIFY_CALLBACK(HCMNOTIFICATION notify, [Optional] IntPtr context, CM_NOTIFY_ACTION action, [In] IntPtr eventData, uint eventDataSize); /// /// A variable of ULONG type that supplies one of the following flag values that apply if the caller supplies a device instance identifier @@ -1982,7 +1982,7 @@ namespace Vanara.PInvoke // pNotifyContext ); [DllImport(Lib_Cfgmgr32, SetLastError = false, ExactSpelling = true)] [PInvokeData("cfgmgr32.h", MSDNShortId = "NF:cfgmgr32.CM_Register_Notification")] - public static extern CONFIGRET CM_Register_Notification(in CM_NOTIFY_FILTER pFilter, [In, Optional] IntPtr pContext, CM_NOTIFY_CALLBACK pCallback, out SafeHCMNOTIFICATION pNotifyContext); + public static extern CONFIGRET CM_Register_Notification(in CM_NOTIFY_FILTER pFilter, [In, Optional] IntPtr pContext, [MarshalAs(UnmanagedType.FunctionPtr)] CM_NOTIFY_CALLBACK pCallback, out SafeHCMNOTIFICATION pNotifyContext); /// /// The CM_Request_Device_Eject function prepares a local device instance for safe removal, if the device is removable. If diff --git a/UnitTests/PInvoke/CfgMgr32/CfgMgr32Tests.cs b/UnitTests/PInvoke/CfgMgr32/CfgMgr32Tests.cs index ac056a46..73ddd56e 100644 --- a/UnitTests/PInvoke/CfgMgr32/CfgMgr32Tests.cs +++ b/UnitTests/PInvoke/CfgMgr32/CfgMgr32Tests.cs @@ -507,10 +507,11 @@ namespace Vanara.PInvoke.Tests public void RegisterAllInterfacesTest() { CM_NOTIFY_CALLBACK callback = Notification; - CM_NOTIFY_FILTER allDev = CM_NOTIFY_FILTER.AllDevices; - Assert.That(CM_Register_Notification(allDev, default, callback, out SafeHCMNOTIFICATION context), Is.EqualTo(CONFIGRET.CR_SUCCESS)); - context.Dispose(); GC.KeepAlive(callback); + Assert.That(CM_Register_Notification(CM_NOTIFY_FILTER.AllDevices, default, callback, out SafeHCMNOTIFICATION context), Is.EqualTo(CONFIGRET.CR_SUCCESS)); + for (int i = 0; i < 200; i++) + System.Threading.Thread.Sleep(100); + context.Dispose(); } [Test] @@ -578,38 +579,29 @@ namespace Vanara.PInvoke.Tests } }; - private Win32Error Notification(HCMNOTIFICATION notify, IntPtr context, CM_NOTIFY_ACTION action, in CM_NOTIFY_EVENT_DATA eventData, uint eventDataSize) + private Win32Error Notification(HCMNOTIFICATION notify, IntPtr context, CM_NOTIFY_ACTION action, IntPtr ed, uint eventDataSize) { - switch (eventData.FilterType) + unsafe { - case CM_NOTIFY_FILTER_TYPE.CM_NOTIFY_FILTER_TYPE_DEVICEHANDLE: - if (action == CM_NOTIFY_ACTION.CM_NOTIFY_ACTION_DEVICECUSTOMEVENT) - Debug.WriteLine($"Custom event {eventData.u.DeviceHandle.EventGuid}."); - break; + CM_NOTIFY_EVENT_DATA* eventData = (CM_NOTIFY_EVENT_DATA*)ed; + switch (eventData->FilterType) + { + case CM_NOTIFY_FILTER_TYPE.CM_NOTIFY_FILTER_TYPE_DEVICEHANDLE: + if (action == CM_NOTIFY_ACTION.CM_NOTIFY_ACTION_DEVICECUSTOMEVENT) + Debug.WriteLine($"Custom event {eventData->u.DeviceHandle.EventGuid}."); + break; - case CM_NOTIFY_FILTER_TYPE.CM_NOTIFY_FILTER_TYPE_DEVICEINSTANCE: - unsafe - { - fixed (char* p = eventData.u.DeviceInstance.InstanceId) - { - var instanceId = new string(p); - Debug.WriteLine($"Notification for {instanceId}: {action}"); - } - } - break; + case CM_NOTIFY_FILTER_TYPE.CM_NOTIFY_FILTER_TYPE_DEVICEINSTANCE: + var instanceId = new string(eventData->u.DeviceInstance.InstanceId); + Debug.WriteLine($"Notification for {instanceId}: {action}"); + break; - case CM_NOTIFY_FILTER_TYPE.CM_NOTIFY_FILTER_TYPE_DEVICEINTERFACE: - unsafe - { - fixed (char* p = eventData.u.DeviceInterface.SymbolicLink) - { - var symbolicLink = new string(p); - Debug.WriteLine($"Notification for {eventData.u.DeviceInterface.ClassGuid} linked {symbolicLink}: {action}"); - } - } - break; + case CM_NOTIFY_FILTER_TYPE.CM_NOTIFY_FILTER_TYPE_DEVICEINTERFACE: + var symbolicLink = new string(eventData->u.DeviceInterface.SymbolicLink); + Debug.WriteLine($"Notification for {eventData->u.DeviceInterface.ClassGuid} linked {symbolicLink}: {action}"); + break; + } } - return Win32Error.ERROR_SUCCESS; } }