Using CM_Register_Notification function for receiving device change event.
returning #define CR_INVALID_DATA (0x0000001F)
I do not want to use RegisterDeviceNotification function for receiving device change event. So please do not suggest the above function.
int main()
{
CM_NOTIFY_FILTER cmNotifyFilter = { 0 };
cmNotifyFilter.cbSize = sizeof(cmNotifyFilter);
cmNotifyFilter.Flags = CM_NOTIFY_FILTER_FLAG_ALL_DEVICE_INSTANCES;
cmNotifyFilter.FilterType = CM_NOTIFY_FILTER_TYPE_DEVICEHANDLE;
cmNotifyFilter.u.DeviceInterface.ClassGuid = GUID_DEVINTERFACE_COMPORT;
HCMNOTIFICATION hcm;
char *test = new char[1024]();
CONFIGRET configRet = ::CM_Register_Notification(&cmNotifyFilter, (PVOID)test, (PCM_NOTIFY_CALLBACK)&MyCMInterfaceNotification, &hcm);
if (configRet != CR_SUCCESS) {
printf("CM_Register_Notification failed, error %d\n", (DWORD)configRet);
}
return 0;
}
DWORD MyCMInterfaceNotification( HCMNOTIFICATION hNotify, PVOID Context, CM_NOTIFY_ACTION Action, PCM_NOTIFY_EVENT_DATA EventData,DWORD EventDataSize )
{
switch (Action) {
case CM_NOTIFY_ACTION_DEVICEINTERFACEARRIVAL:
wprintf(("MyCmInterfaceNotification: Arrival of %S\n",
EventData->u.DeviceInterface.SymbolicLink));
//
// Enqueue a work item to open target
//
break;
case CM_NOTIFY_ACTION_DEVICEINTERFACEREMOVAL:
wprintf(("MyCmInterfaceNotification: removal of %S\n",
EventData->u.DeviceInterface.SymbolicLink));
break;
default:
printf(("MyCmInterfaceNotification: Arrival unknown action\n"));
break;
}
}
Thanks in advance
CM_NOTIFY_FILTER_TYPE_DEVICEHANDLE
filter type is used for notification of device removals. When the device represented by the interface receives a remove query request, the system notifies your component.You should call CM_Register_Notification with
DeviceHandle
set (handle of already opened device viaCreateFile
call) if you want to use this filter type:But I guess you want to register for device interface arrival instead:
Here is example code for
CM_NOTIFY_FILTER_TYPE_DEVICEINTERFACE
callback:See docs from Microsoft for proper algorithm. And official example code.