USB设备的插入检测
又被坑了,总算解决了,要睡了长话短说。首先,着重强调 Device Class GUID, Device Interface Class GUID, Interface GUID是有区别的,另外奉上两篇MSDN文章:
http://msdn.microsoft.com/en-us/library/windows/hardware/ff553412(v=vs.85).aspx
http://msdn.microsoft.com/en-us/library/windows/hardware/ff553428(v=vs.85).aspx
Device Class GUID 是INF文件中Verison Section中指名的设备类GUID,他标志着此设备图标,此设备出现在哪一栏,电压电流等各种信息。
Interface GUID是指驱动程序中的GUID, 用于使用API打开此设备进行一系列读写操作,API代码如下(FINDSTR_USB_DEVICE即为我驱动程序中使用的GUID):
1 //得到设备的句柄 2 HDEVINFO info=SetupDiGetClassDevs((LPGUID)&FINDSTR_USB_DEVICE,NULL,NULL,DIGCF_PRESENT|DIGCF_INTERFACEDEVICE); 3 if(info==INVALID_HANDLE_VALUE) 4 { 5 AfxMessageBox("No HDEVINFO avaliable for this GUID\n"); 6 return ; 7 } 8 9 SP_INTERFACE_DEVICE_DATA ifdata; 10 ifdata.cbSize=sizeof(ifdata); 11 DWORD instance=0; 12 if(!SetupDiEnumDeviceInterfaces(info,NULL,(LPGUID)&FINDSTR_USB_DEVICE,instance,&ifdata)) 13 { 14 AfxMessageBox("No SP_INTERFACE_DEVICE_DATA available for this GUID instance"); 15 SetupDiDestroyDeviceInfoList(info); 16 return ; 17 } 18 19 //得到符号链接名 20 DWORD ReqLen; 21 SetupDiGetDeviceInterfaceDetail(info,&ifdata,NULL,0,&ReqLen,NULL); 22 PSP_INTERFACE_DEVICE_DETAIL_DATA ifDetail=(PSP_INTERFACE_DEVICE_DETAIL_DATA)new char[ReqLen]; 23 if(ifDetail==NULL) 24 { 25 AfxMessageBox("Error 1"); 26 SetupDiDestroyDeviceInfoList(info); 27 return ; 28 } 29 ifDetail->cbSize=sizeof(SP_INTERFACE_DEVICE_DETAIL_DATA); 30 if(!SetupDiGetDeviceInterfaceDetail(info,&ifdata,ifDetail,ReqLen,NULL,NULL)) 31 { 32 AfxMessageBox("Error 2"); 33 SetupDiDestroyDeviceInfoList(info); 34 delete ifDetail; 35 return ; 36 } 37 char buff[256]={0}; 38 char * format="Symbolic link is %s\n"; 39 sprintf(buff,format,ifDetail->DevicePath); 40 HANDLE rv=CreateFile(ifDetail->DevicePath, 41 GENERIC_READ|GENERIC_WRITE, 42 FILE_SHARE_READ|FILE_SHARE_WRITE, 43 NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL|FILE_FLAG_OVERLAPPED,NULL); 44 //end of 得到设备句柄 45 delete ifDetail; 46 SetupDiDestroyDeviceInfoList(info); 47 48 hDevice=rv; 49 DWORD ThreadId; 50 hThread=CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)UpDate,this,0,&ThreadId); 51 m_EchoLed1.EnableWindow(TRUE); 52 m_EchoLed2.EnableWindow(TRUE); 53 m_OpenDevice.SetWindowText("关闭设备");
Device Interface Class GUID是Microsoft定义用来调用RegisterDeviceNotification时所使用的一类GUID,关于Microsoft所定义的所有Devcie Interface Class GUID 可以参看上面给出的网址,下面给出这类GUID的使用方法(用于动态检测某类USB设备的插入与拔出):
1 //注册 某个USB设备接口类 事件 2 GUID zz = {0xA5DCBF10, 0x6530, 0x11D2, 0x90, 0x1F, 0x00, 0xC0, 0x4F, 0xB9, 3 0x51, 0xED}; 4 DEV_BROADCAST_DEVICEINTERFACE DevBroadcastDeviceInterface; 5 6 DevBroadcastDeviceInterface.dbcc_size = sizeof(DevBroadcastDeviceInterface); 7 DevBroadcastDeviceInterface.dbcc_devicetype = DBT_DEVTYP_DEVICEINTERFACE; 8 DevBroadcastDeviceInterface.dbcc_classguid = zz; 9 RegisterDeviceNotification(m_hWnd, &DevBroadcastDeviceInterface, 10 DEVICE_NOTIFY_WINDOW_HANDLE);
注:上述三种GUID没有任何实质上的关联,不能混为一谈。
GUID(A5DCBF10-6530-11D2-901F-00C04FB951ED)写成代码为
static const GUID x_guid = { 0xA5DCBF10, 0x6530, 0x11D2, 0x90, 0x1F, 0x00, 0xC0, 0x4F, 0xB9, 0x51, 0xED };