C# 訪問USB(HID)設備方法 (轉)


依次使用一下函數:

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了


免責聲明!

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



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