1

I have created winapi application witch uses other .exe via createprocess to get/set reports. And now I need some kind of way to detect that this USB HID device was plugged/unplugged from computer when application is running. Hardest part of it is that in that app I know just VID and PID and I don't have any handles to that USB HID device. is there any way to solve this problem or I first need handle of the device?

Edit

If anyone is interested why I need it. I want to disable/enable controls of my app when i plug and unplug device.

asked Jan 13, 2017 at 12:58

2 Answers 2

4

at first you must register own window for receive WM_DEVICECHANGE message with DBT_DEVICEARRIVAL and DBT_DEVICEREMOVECOMPLETE for GUID_DEVINTERFACE_USB_DEVICE with RegisterDeviceNotification - windows will be not send this notification without registration!

case WM_CREATE:
 DEV_BROADCAST_DEVICEINTERFACE NotificationFilter = { 
 sizeof(DEV_BROADCAST_DEVICEINTERFACE), 
 DBT_DEVTYP_DEVICEINTERFACE,
 0,
 GUID_DEVINTERFACE_USB_DEVICE
 };
 if (!(_Handle = RegisterDeviceNotification(hwnd, &NotificationFilter, DEVICE_NOTIFY_WINDOW_HANDLE)))
 {
 return -1;
 }
 break;

and unregister on destroy:

case WM_DESTROY:
 if (_Handle) UnregisterDeviceNotification(_Handle);
 break;

after this you will be receive notification. if

I know just VID and PID

you can search for L"#VID_????&PID_????#" in dbcc_name (where in place ? your actual vid and pidvalues)

case WM_DEVICECHANGE:
 switch (wParam)
 {
 case DBT_DEVICEREMOVECOMPLETE:
 case DBT_DEVICEARRIVAL:
 {
 PDEV_BROADCAST_DEVICEINTERFACE p = (PDEV_BROADCAST_DEVICEINTERFACE)lParam;
 if (p->dbcc_devicetype == DBT_DEVTYP_DEVICEINTERFACE &&
 p->dbcc_classguid == GUID_DEVINTERFACE_USB_DEVICE)
 {
 DbgPrint("%S\n", p->dbcc_name);
 if (wcsstr(p->dbcc_name, L"#VID_****&PID_****#"))
 {
 DbgPrint("%s\n", wParam == DBT_DEVICEARRIVAL ? "arrival" : "removal");
 }
 }
 }
 break;
 }
 break;
answered Jan 13, 2017 at 14:48
Sign up to request clarification or add additional context in comments.

11 Comments

_Handle is a reserved symbol in C and C++, and you should not be using it. If you do you'll risk colliding with symbols defined by the language implementation. It's their namespace after all.
@IInspectable "_Handle is a reserved symbol in C and C++" - hmm, first time listen about this keyword. here and here it not listed. and main - if I will be use reserved language keyword - compiler give me error (or warning how minimum) but CL 19.00.24210 not give me this warning. (I use _Handle as window class member if so) - and this is all "errors" which you found here ? nothing more ?
A reserved symbol and a keyword are different things. A compiler won't issue an error or warning when using a reserved symbol. This is something you need to know about identifiers.
@IInspectable - and where _Handle is exactly marked as reserved ? and maximum what be if this happens - compiler error - and ctrl+h for replace _Handle for say _hNotification
From the link I posted in my previous comment: "[I]dentifiers that begin with an underscore followed by an uppercase letter are reserved". Take your time to work through the document. You might even learn a thing or two. Then again, you're not a big fan of contracts anyway, so you might as well skip it.
|
3

Windows sends to all top-level windows the WM_DEVICECHANGE message when new devices or media becomes availables. Checks for the event DBT_DEVICEARRIVAL in wParam. With the event DBT_DEVICEARRIVAL lParam can be converted to a DEV_BROADCAST_HDR structure. When this is done, you check dbch_devicetype from DEV_BROADCAST_HDR, and convert lParam again to DEV_BROADCAST_HANDLE, or DEV_BROADCAST_VOLUME if dbch_devicetype is equal to DBT_DEVTYP_HANDLEor DEV_BROADCAST_VOLUME, I'm not sure to remember which one.

answered Jan 13, 2017 at 13:37

1 Comment

at first you not got notify for usb device without RegisterDeviceNotification with GUID_DEVINTERFACE_USB_DEVICE, at second we need DEV_BROADCAST_DEVICEINTERFACE here

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.