VC自动侦测USB参考

关于USB设备的自动侦测,该如何解决

http://m.myexception.cn/vc-mfc/114413.html

关于USB设备的自动侦测
不知道为什么,U盘能侦测到插入和移除, USB 摄像头就是不行,谁给帮忙解答一下


GUID CAMERA = { 0x6BDD1FC6, 0x810F, 0x11D0, 0xBE, 0xC7, 0x08, 0x00, 0x2B, 0xE2, 0x09, 0x2F };


DEV_BROADCAST_DEVICEINTERFACE NotificationFilter;
ZeroMemory( &NotificationFilter, sizeof(NotificationFilter) );
NotificationFilter.dbcc_size = sizeof(NotificationFilter); 
NotificationFilter.dbcc_devicetype=DBT_DEVTYP_DEVICEINTERFACE;
NotificationFilter.dbcc_classguid=CAMERA;
RegisterDeviceNotification(hWnd,&NotificationFilter, DEVICE_NOTIFY_WINDOW_HANDLE);



侦测:
if( message == WM_DEVICECHANGE )
{
if ( wParam==DBT_DEVNODES_CHANGED )
int kk=1;
         else if ( wParam == DBT_DEVICEARRIVAL) // device arrival
{
MessageBox(NULL,"arrival","test",MB_OK);
return 1;
}
else if (wParam == DBT_DEVICEREMOVECOMPLETE) // device remove
{
MessageBox(NULL,"remove","test",MB_OK);
return -1;
}
}

------解决方案--------------------
你确定你的GUID 对吗? 试一下这几个GUID

C/C++ code
static const GUID GUID_DEVINTERFACE_LIST[] = 
{
    // GUID_DEVINTERFACE_USB_DEVICE
    { 0xA5DCBF10, 0x6530, 0x11D2, { 0x90, 0x1F, 0x00, 0xC0, 0x4F, 0xB9, 0x51, 0xED } },

    // GUID_DEVINTERFACE_DISK
    { 0x53f56307, 0xb6bf, 0x11d0, { 0x94, 0xf2, 0x00, 0xa0, 0xc9, 0x1e, 0xfb, 0x8b } },

    // GUID_DEVINTERFACE_HID, 
    { 0x4D1E55B2, 0xF16F, 0x11CF, { 0x88, 0xCB, 0x00, 0x11, 0x11, 0x00, 0x00, 0x30 } },

    // GUID_NDIS_LAN_CLASS
    { 0xad498944, 0x762f, 0x11d0, { 0x8d, 0xcb, 0x00, 0xc0, 0x4f, 0xc3, 0x35, 0x8c } }

     GUID_DEVINTERFACE_COMPORT
    //{ 0x86e0d1e0, 0x8089, 0x11d0, { 0x9c, 0xe4, 0x08, 0x00, 0x3e, 0x30, 0x1f, 0x73 } },

     GUID_DEVINTERFACE_SERENUM_BUS_ENUMERATOR
    //{ 0x4D36E978, 0xE325, 0x11CE, { 0xBF, 0xC1, 0x08, 0x00, 0x2B, 0xE1, 0x03, 0x18 } },

     GUID_DEVINTERFACE_PARALLEL
    //{ 0x97F76EF0, 0xF883, 0x11D0, { 0xAF, 0x1F, 0x00, 0x00, 0xF8, 0x00, 0x84, 0x5C } },

     GUID_DEVINTERFACE_PARCLASS
    //{ 0x811FC6A5, 0xF728, 0x11D0, { 0xA5, 0x37, 0x00, 0x00, 0xF8, 0x75, 0x3E, 0xD1 } }
};


//  我是把这几个GUID都注册了一遍,没注册过摄像头的,你着重确定GUID是否正确吧
for(int i=0; i

 

 

vc检测USB设备插入或移除

http://blog.hehehehehe.cn/a/412.htm

1.添加消息映射

消息映射
BEGIN_MESSAGE_MAP(CONSTNETCNDlg, CDialog)
//{{AFX_MSG_MAP(CONSTNETCNDlg)
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
...
ON_WM_DEVICECHANGE()
//}}AFX_MSG_MAP

声明消息响应函数:
afx_msg BOOL OnDeviceChange(UINT nEventType, DWORD dwData);

定义函数内容:


BOOL CONSTNETCNDlg::OnDeviceChange(UINT nEventType,DWORD dwData)
{
   DEV_BROADCAST_DEVICEINTERFACE* dbd = (DEV_BROADCAST_DEVICEINTERFACE*) dwData;

   //这里进行信息匹配,比如guid等

//针对各个事件进行处理.
switch (nEventType)
{
case DBT_DEVICEREMOVECOMPLETE:
   MessageBox("移除USB");
   break;
case DBT_DEVICEARRIVAL:
   MessageBox("发现USB");
   break;
default:
   break;
}

return TRUE;

}

2.注册设备(添加到OnInitDialog中即可)

DEV_BROADCAST_DEVICEINTERFACE DevInt;
memset(&DevInt,0,sizeof(DEV_BROADCAST_DEVICEINTERFACE));
DevInt.dbcc_size = sizeof(DEV_BROADCAST_DEVICEINTERFACE);
DevInt.dbcc_devicetype = DBT_DEVTYP_DEVICEINTERFACE;

DevInt.dbcc_classguid = { 0xA5DCBF10, 0x6530, 0x11D2, { 0x90, 0x1F, 0x00, 0xC0, 0x4F, 0xB9, 0x51, 0xED } };
RegisterDeviceNotificationthis->GetSafeHwnd(),&DevInt,DEVICE_NOTIFY_WINDOW_HANDLE);

PS:只有注册了该设备,OnDeviceChange才能获得详细的信息,否则收到的nEventType参数都是0007,dwData无数据。

你可能感兴趣的:(USB)