Breaking change: Fixed problem with CM_Register_Notification callback method (#432)

pull/436/head
David Hall 2023-10-19 21:28:32 -06:00
parent 9e37bbd1bc
commit 69f805f25c
2 changed files with 24 additions and 32 deletions

View File

@ -24,7 +24,7 @@ namespace Vanara.PInvoke
/// cref="Win32Error.ERROR_SUCCESS"/>. The callback should not return any other values.
/// </returns>
[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);
/// <summary>
/// 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);
/// <summary>
/// The <c>CM_Request_Device_Eject</c> function prepares a local device instance for safe removal, if the device is removable. If

View File

@ -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;
}
}