Skip to content

Commit

Permalink
Breaking change: Fixed problem with CM_Register_Notification callback…
Browse files Browse the repository at this point in the history
… method (dahall#432)
  • Loading branch information
dahall committed Oct 20, 2023
1 parent 9e37bbd commit 69f805f
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 34 deletions.
4 changes: 2 additions & 2 deletions PInvoke/CfgMgr32/CfgMgr32_3.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public static partial class CfgMgr32
/// 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
Expand Down Expand Up @@ -1982,7 +1982,7 @@ public static SafeCONFLICT_LIST CM_Query_Resource_Conflict_List<T>(uint dnDevIns
// 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
Expand Down
56 changes: 24 additions & 32 deletions UnitTests/PInvoke/CfgMgr32/CfgMgr32Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -507,10 +507,11 @@ public void CM_Request_Eject_PCTest()
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]
Expand Down Expand Up @@ -578,38 +579,29 @@ private static uint LocateNode(string devId = null)
}
};

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;

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_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;
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:
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:
var symbolicLink = new string(eventData->u.DeviceInterface.SymbolicLink);
Debug.WriteLine($"Notification for {eventData->u.DeviceInterface.ClassGuid} linked {symbolicLink}: {action}");
break;
}
}

return Win32Error.ERROR_SUCCESS;
}
}
Expand Down

0 comments on commit 69f805f

Please sign in to comment.