Why CM_Register_Notification function not working

0

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

c++
windows
driver
visual-c++-2012
asked on Stack Overflow Jan 18, 2019 by Vijay Kumbhani

1 Answer

0

CM_NOTIFY_FILTER_TYPE_DEVICEHANDLE filter type is used for notification of device removals. When the device represented by the interface receives a query remove request, the system notifies your component.

You should call CM_Register_Notification with DeviceHandle set (handle of already opened device via CreateFile call) if you want to use this filter type:

CM_NOTIFY_FILTER NotifyFilter = {0};
NotifyFilter.cbSize = sizeof(NotifyFilter);
NotifyFilter.FilterType = CM_NOTIFY_FILTER_TYPE_DEVICEHANDLE;
NotifyFilter.u.DeviceHandle.hTarget = Context->DeviceHandle;

But I guess you want to register for device interface arrival instead:

CM_NOTIFY_FILTER NotifyFilter = {0};
NotifyFilter.cbSize = sizeof(NotifyFilter);
NotifyFilter.FilterType = CM_NOTIFY_FILTER_TYPE_DEVICEINTERFACE;
NotifyFilter.u.DeviceInterface.ClassGuid = GUID_DEVINTERFACE_COMPORT;

Here is example code for CM_NOTIFY_FILTER_TYPE_DEVICEINTERFACE callback:

DWORD
WINAPI
InterfaceCallback(
    _In_ HCMNOTIFICATION       hNotify,
    _In_ PVOID                 hContext,
    _In_ CM_NOTIFY_ACTION      Action,
    _In_ PCM_NOTIFY_EVENT_DATA EventData,
    _In_ DWORD                 EventDataSize
    )
{
    switch (Action)
    {
    case CM_NOTIFY_ACTION_DEVICEINTERFACEARRIVAL:
        //
        // Register for device notifications
        //
        RegisterDeviceNotifications(EventData->u.DeviceInterface.SymbolicLink);
        break;

    case CM_NOTIFY_ACTION_DEVICEINTERFACEREMOVAL:
...
        break;

    default:
        break;
    }

    return ERROR_SUCCESS;
}

See docs from Microsoft for proper algorithm. And official example code.

answered on Stack Overflow May 15, 2021 by DJm00n • edited May 15, 2021 by DJm00n

User contributions licensed under CC BY-SA 3.0