Applied fix for #432

nullableenabled
David Hall 2023-10-19 21:36:09 -06:00
parent 5dad85375d
commit 361c214f07
2 changed files with 23 additions and 31 deletions

View File

@ -20,7 +20,7 @@ public static partial class CfgMgr32
/// cref="Win32Error.ERROR_SUCCESS"/>. The callback should not return any other values. /// cref="Win32Error.ERROR_SUCCESS"/>. The callback should not return any other values.
/// </returns> /// </returns>
[UnmanagedFunctionPointer(CallingConvention.Winapi)] [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> /// <summary>
/// A variable of ULONG type that supplies one of the following flag values that apply if the caller supplies a device instance identifier /// A variable of ULONG type that supplies one of the following flag values that apply if the caller supplies a device instance identifier

View File

@ -503,10 +503,11 @@ public class CfgMgr32Tests
public void RegisterAllInterfacesTest() public void RegisterAllInterfacesTest()
{ {
CM_NOTIFY_CALLBACK callback = Notification; 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); 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] [Test]
@ -574,38 +575,29 @@ public class CfgMgr32Tests
} }
}; };
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: CM_NOTIFY_EVENT_DATA* eventData = (CM_NOTIFY_EVENT_DATA*)ed;
if (action == CM_NOTIFY_ACTION.CM_NOTIFY_ACTION_DEVICECUSTOMEVENT) switch (eventData->FilterType)
Debug.WriteLine($"Custom event {eventData.u.DeviceHandle.EventGuid}."); {
break; 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: case CM_NOTIFY_FILTER_TYPE.CM_NOTIFY_FILTER_TYPE_DEVICEINSTANCE:
unsafe var instanceId = new string(eventData->u.DeviceInstance.InstanceId);
{ Debug.WriteLine($"Notification for {instanceId}: {action}");
fixed (char* p = eventData.u.DeviceInstance.InstanceId) break;
{
var instanceId = new string(p);
Debug.WriteLine($"Notification for {instanceId}: {action}");
}
}
break;
case CM_NOTIFY_FILTER_TYPE.CM_NOTIFY_FILTER_TYPE_DEVICEINTERFACE: case CM_NOTIFY_FILTER_TYPE.CM_NOTIFY_FILTER_TYPE_DEVICEINTERFACE:
unsafe var symbolicLink = new string(eventData->u.DeviceInterface.SymbolicLink);
{ Debug.WriteLine($"Notification for {eventData->u.DeviceInterface.ClassGuid} linked {symbolicLink}: {action}");
fixed (char* p = eventData.u.DeviceInterface.SymbolicLink) break;
{ }
var symbolicLink = new string(p);
Debug.WriteLine($"Notification for {eventData.u.DeviceInterface.ClassGuid} linked {symbolicLink}: {action}");
}
}
break;
} }
return Win32Error.ERROR_SUCCESS; return Win32Error.ERROR_SUCCESS;
} }
} }