依次使用一下函數:
1、static public extern void HidD_GetHidGuid (ref System.Guid HidGuid);
獲取HID的全局GUID
2、static public extern IntPtr SetupDiGetClassDevs(ref System.Guid ClassGuid,
string Enumerator,
int hwndParent,
int Flags);
獲取所有hid句柄
3、static public extern int SetupDiEnumDeviceInterfaces(IntPtr DeviceInfoSet,
int DeviceInfoData,
ref System.Guid InterfaceClassGuid,
int MemberIndex,
ref SP_DEVICE_INTERFACE_DATA DeviceInterfaceData);
檢測集合每個接口,成功返回true
4、 static public extern bool SetupDiGetDeviceInterfaceDetail(IntPtr DeviceInfoSet,
ref SP_DEVICE_INTERFACE_DATA DeviceInterfaceData,
IntPtr DeviceInterfaceDetailData,
int DeviceInterfaceDetailDataSize,
ref int RequiredSize,
IntPtr DeviceInfoData);
獲取接口信息,第一次失敗 但可獲得信息緩沖區大小
5、 static public extern bool SetupDiGetDeviceInterfaceDetail(IntPtr DeviceInfoSet,
ref SP_DEVICE_INTERFACE_DATA DeviceInterfaceData,
IntPtr DeviceInterfaceDetailData,
int DeviceInterfaceDetailDataSize,
ref int RequiredSize,
IntPtr DeviceInfoData);
第二次獲取
循環 3、4、5把所有的設備讀取,把所有設備路徑名存放在一個數組里devicePathName
6、 CreateFile(string lpFileName,
uint dwDesiredAccess,
uint dwShareMode,
ref SECURITY_ATTRIBUTES lpSecurityAttributes,
int dwCreationDisposition,
uint dwFlagsAndAttributes,
int hTemplateFile);
根據devicePathName鏈接文件
以上步驟成功就可以讀寫USB了