I'm using RegisterDeviceNotification to watch for changes to a USB device, using the WM_DEVICECHANGE event. However, when I call RegisterDeviceNotification() it returns NULL for the notification handle, which should indicate that it failed. But GetLastError() returns ERROR_SUCCESS and the notifications actually go through.
This causes real trouble when I, later on, need to UnRegister for notifications and then re-register. Since the handle it returned was NULL, I can't actually re-register. If I try, RegisterDeviceNotification() just hangs.
My code is as follows:
DEV_BROADCAST_HANDLE devBrHdr;
::ZeroMemory( &devBrHdr,sizeof( DEV_BROADCAST_HANDLE ) );
devBrHdr.dbch_size = sizeof( DEV_BROADCAST_HANDLE );
devBrHdr.dbch_devicetype = DBT_DEVTYP_HANDLE;
devBrHdr.dbch_handle = m_hDriver;
m_hDevNotify = ::RegisterDeviceNotification( hWnd,&devBrHdr,DEVICE_NOTIFY_WINDOW_HANDLE );
m_hDriver is the Handle to the driver, which I opened earlier, upon connecting to the device (a USB barcode scanner).
Any ideas what I'm doing wrong here?
Make sure you are not making another Win32 API call between RegisterDeviceNotification
and GetLastError
.
Check the value of devBrHdr.dbch_hdevnotify
. It should contain the same handle returned by RegisterDeviceNotification
.
Was the m_hDriver
value obtained from a call to CreateFile
?