Tutorial :RegisterDeviceNotification Returns NULL but notifications still recieved



Question:

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?


Solution:1

  1. Make sure you are not making another Win32 API call between RegisterDeviceNotification and GetLastError.

  2. Check the value of devBrHdr.dbch_hdevnotify. It should contain the same handle returned by RegisterDeviceNotification.

  3. Was the m_hDriver value obtained from a call to CreateFile?


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »