c# - Getting USB Storage Device Instance ID (unique ID) programmatically -


Whenever a user plugs a USB mass storage device, how does the device instance ID (unique ID) of that specific device get receive? Hold WM_DEVICECHANGE from any window handle by registering for device change notifications.

  DEV_BROADCAST_DEVICEINTERFACE dbd = {sizeof (dbd)}; Dbd.dbcc_devicetype = DBT_DEVTYP_DEVICEINTERFACE; Dbd.dbcc_classguid = GUID_DEVINTERFACE_USB_DEVICE; RegisterDevice Notification (HWND, and DBD, DEVICE_NOTIFY_WINDOW_HANDLE);  

The lParam of WM_DEVICECHANGE can be inserted on the DB 07DEVTYP_DEVICEINTERFACE. Note - When plug in a device you can get multiple WM_DEVICECHANGE notifications, just filter and duplicate ignore at the arrival event

  LRESULT WndProc (HWND hWnd, uint uMsg, wParam wParam, lParam lParam) {switch (Hwnd) {case WM_DEVICE_CHANGE:. {PDEV_BROADCAST_HDR pHdr = zero; PDEV_BROADCAST_DEVICEINTERFACE pDev = NULL; PHDR = (PDEV_BROADCAST_HDR) lParam; Bool fDeviceArrival = (wParam == DB T_DEVICEARRIVAL); If (fDeviceArrival) {if (pHdr & amp; amp; pHdr-> dbch_devicetype == DBT_DEVTYP_DEVICEINTERFACE)) {pDev = (PDEV_BROADCAST_DEVICEINTERFACE) lParam; } If (pDev & amp; amp; (pDev-> GUID_DEVINTERFACE_USB_DEVICE)) {// Device is just removing PNP string dbcc_name in OutputDebugString (pDev-> dbcc_name); OutputDebugString ("\ r \ n"); }} ....  

Comments

Popular posts from this blog

windows - Heroku throws SQLITE3 Read only exception -

lex - Building a lexical Analyzer in Java -

python - rename keys in a dictionary -