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 };


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM