How can I detect device Plugging on the USB port?

Source: Internet
Author: User

WM_DEVICECHANGLE message. When nEventType is DBT_DEVICEARRIVAL and dwData = 7, it is a USB flash drive connection event (if it is correct)

I want to set the GUID dbcc_classguid OF THE DEV_BROADCAST_DEVICEINTERFACE struct in RegisterDeviceNotification to the GUID of the device. What are the parameters?

typedef void (__stdcall *FUNC_TYPE_HIDGUID) (OUT   LPGUID   HidGuid);FUNC_TYPE_HIDGUID _HidD_GetHidGuid = NULL; // USB version GUIDstatic GUID GUID_CLASS_ITOKEN2 = {0x171638f7, 0x1ead, 0x4873, {0xba, 0x98, 0xc9, 0x66, 0xab, 0xcf, 0x1, 0x42}};static GUID GUID_CLASS_ITOKEN2_HID;BOOL    bRegisted  = FALSE;typedef  PVOID           HDEVNOTIFY;HDEVNOTIFY hDevNotify1 = NULL;HDEVNOTIFY hDevNotify2 = NULL;void CSample_13Dlg::OnRegister() {// TODO: Add your control notification handler code hereHWND hWnd;//HDEVNOTIFY hDevNotify = NULL;    DEV_BROADCAST_DEVICEINTERFACE dbf;BOOL bRes;if (_HidD_GetHidGuid == NULL){HINSTANCE hMod = LoadLibrary("hid.dll");_HidD_GetHidGuid = (FUNC_TYPE_HIDGUID)GetProcAddress(hMod, "HidD_GetHidGuid");ASSERT(_HidD_GetHidGuid != NULL);// Get HID device GUID _HidD_GetHidGuid(&GUID_CLASS_ITOKEN2_HID);FreeLibrary(hMod);}if (!bRegisted) // if not registered{hWnd = this->m_hWnd;// Register ITOKEN2 device notification memset(&dbf, 0x00, sizeof(dbf));dbf.dbcc_size       = sizeof(DEV_BROADCAST_DEVICEINTERFACE);dbf.dbcc_devicetype = DBT_DEVTYP_DEVICEINTERFACE;dbf.dbcc_classguid  = GUID_CLASS_ITOKEN2;hDevNotify2 = RegisterDeviceNotification( hWnd, &dbf,DEVICE_NOTIFY_WINDOW_HANDLE);ASSERT(hDevNotify2 != NULL);// Register ITOKEN2 HID device notificationmemset(&dbf, 0x00, sizeof(dbf));dbf.dbcc_size       = sizeof(DEV_BROADCAST_DEVICEINTERFACE);dbf.dbcc_devicetype = DBT_DEVTYP_DEVICEINTERFACE;dbf.dbcc_classguid  = GUID_CLASS_ITOKEN2_HID;hDevNotify1 = RegisterDeviceNotification( hWnd, &dbf,DEVICE_NOTIFY_WINDOW_HANDLE);ASSERT(hDevNotify1 != NULL);GetDlgItem(IDC_REGISTER)->SetWindowText("Unregister");bRegisted = TRUE;}else {if (hDevNotify1) // Unregister ITOKEN2 device notification {bRes = UnregisterDeviceNotification(hDevNotify1);ASSERT(bRes);}if (hDevNotify2) // Unregister ITOKEN2 HID device notification{bRes = UnregisterDeviceNotification(hDevNotify2);ASSERT(bRes);}GetDlgItem(IDC_REGISTER)->SetWindowText("register");bRegisted = FALSE;}}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.