[RegisterDeviceNotification] Handle connection/disconnection of a USB HID device
Hello,
I'm currently trying to handle USB HID device notifications using HID library from Windows Development Kit.
I'm using a HID device (SpacePoint Fusion, for those who are interested) and I can detect it, open it and read data from it without any issue.
My problem is concerning RegisterDeviceNotification function, which enables to get noticed when an event has occured concerning the device (eg. connection/disconnection).
Everything works fine for a MFC application, but not for a Win32 console application.
The first parameter of RegisterDeviceNotification method is the handle of the window that will process the events. So I'm using the handle of the window console, the return of the function (HDEVNOTIFY type) seems to notice me that everything went fine since GetLastError() returns 0.
The problem is that the message processing function (WindowProc) is never called, so I don't have any chance to see a WM_DEVICECHANGE event happen.
Re: [RegisterDeviceNotification] Handle connection/disconnection of a USB HID device
You have to have 'normal' window in a separate thread to catch notifications. The window is not obliged to be visible anyhow, and it can be a message-only window alright.
The main problem with console window is: the window procedure is not controlled/monitored by your application like this is done in case of MFC window.
Last edited by Igor Vartanov; April 28th, 2011 at 10:34 AM.
Re: [RegisterDeviceNotification] Handle connection/disconnection of a USB HID device
Thanks to both of you.
Actually, I followed Codeplug recommendation by creating a plain non-visible window.
This works fine, I've just added some code to make sure the device which has been attached/removed is the one I'm interested in.
Now, everything's working well and I'll implement it in my DLL.
Bookmarks