怎么侦测USB端口上有设备插拔?

WM_DEVICECHANGLE消息,nEventType为DBT_DEVICEARRIVAL且dwData  = 7时就是U盘连接事件(没弄错的话)

我想RegisterDeviceNotification中参数DEV_BROADCAST_DEVICEINTERFACE结构体的GUID dbcc_classguid;为设备的GUID,填什么参数啊?

 

typedef void (__stdcall *FUNC_TYPE_HIDGUID) (
OUT   LPGUID   HidGuid
);

FUNC_TYPE_HIDGUID _HidD_GetHidGuid = NULL;

 // USB version GUID
static 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 here
HWND 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 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_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;
}
}
 

你可能感兴趣的:(怎么侦测USB端口上有设备插拔?)