1. 程式人生 > >如何註冊USB裝置的WM_DEVICECHANGE訊息?

如何註冊USB裝置的WM_DEVICECHANGE訊息?

HDEVNOTIFY      gNotifyDevHandle = NULL;
HDEVNOTIFY      gNotifyHubHandle = NULL;

  DEV_BROADCAST_DEVICEINTERFACE broadcastInterface = {0};

  // Register to receive notification when a USB device is plugged in.
  broadcastInterface.dbcc_size = sizeof(DEV_BROADCAST_DEVICEINTERFACE);
  broadcastInterface.dbcc_devicetype = DBT_DEVTYP_DEVICEINTERFACE;
  memcpy( &(broadcastInterface.dbcc_classguid),
  &(GUID_CLASS_USB_DEVICE), sizeof(struct _GUID));
  gNotifyDevHandle = RegisterDeviceNotification(hWnd,
  &broadcastInterface, DEVICE_NOTIFY_WINDOW_HANDLE);

  // Now register for USBHub notifications.
  memcpy( &(broadcastInterface.dbcc_classguid),
  &(GUID_CLASS_USBHUB), sizeof(struct _GUID));
  gNotifyHubHandle = RegisterDeviceNotification(hWnd,
  &broadcastInterface, DEVICE_NOTIFY_WINDOW_HANDLE);
 
 //unregister USB & USBHub notifications
  UnregisterDeviceNotification(gNotifyDevHandle);
  UnregisterDeviceNotification(gNotifyHubHandle);
  
  
HHOOK gMsgHook = NULL;
LRESULT CALLBACK HookMsgProc(int nCode, WPARAM wParam, LPARAM lParam);
gMsgHook = SetWindowsHookEx(WH_CALLWNDPROC, HookMsgProc,,,);
UnhookWindowsHookEx(gMsgHook);