简体   繁体   English

USB隐藏设备插入/删除检测WinAPI

[英]Usb hid device insert/removal detection winapi

I have created winapi application witch uses other .exe via createprocess to get/set reports. 我创建了winapi应用程序,女巫通过createprocess使用其他.exe来获取/设置报告。 And now I need some kind of way to detect that this USB HID device was plugged/unplugged from computer when application is running. 现在,我需要某种方法来检测该USB HID设备在应用程序运行时是否已从计算机插入/拔出。 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. 最难的是,在该应用程序中,我只知道VID和PID,而对该USB HID设备却没有任何处理。 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. 我想在插入和拔出设备时禁用/启用我的应用程序的控件。

Windows sends to all top-level windows the WM_DEVICECHANGE message when new devices or media becomes availables. 新设备或媒体可用时,Windows会向所有顶层窗口发送WM_DEVICECHANGE消息。 Checks for the event DBT_DEVICEARRIVAL in wParam . wParam检查事件DBT_DEVICEARRIVAL With the event DBT_DEVICEARRIVAL lParam can be converted to a DEV_BROADCAST_HDR structure. 通过事件DBT_DEVICEARRIVAL可以将lParam转换为DEV_BROADCAST_HDR结构。 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_HANDLE or DEV_BROADCAST_VOLUME , I'm not sure to remember which one. 当做到这一点,你检查dbch_devicetypeDEV_BROADCAST_HDR ,并转换lParam再次DEV_BROADCAST_HANDLE ,或DEV_BROADCAST_VOLUME如果dbch_devicetype等于DBT_DEVTYP_HANDLEDEV_BROADCAST_VOLUME ,我不知道要记住哪一个。

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! 首先,您必须注册自己的窗口,以便通过RegisterDeviceNotificationGUID_DEVINTERFACE_USB_DEVICE接收带有DBT_DEVICEARRIVALDBT_DEVICEREMOVECOMPLETE WM_DEVICECHANGE消息- GUID_DEVINTERFACE_USB_DEVICE RegisterDeviceNotification ,窗口将不会发送此通知!

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 我只知道VID和PID

you can search for L"#VID_????&PID_????#" in dbcc_name (where in place ? your actual vid and pid values) 你可以搜索L"#VID_????&PID_????#"dbcc_name (其中地方?你的实际vidpid值)

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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM