码迷,mamicode.com
首页 > Windows程序 > 详细

C# 访问USB(HID)设备方法 (转)

时间:2017-11-27 15:23:04      阅读:352      评论:0      收藏:0      [点我收藏+]

标签:oid   mem   函数   device   detail   获取   string   ssg   get   

依次使用一下函数:

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了

C# 访问USB(HID)设备方法 (转)

标签:oid   mem   函数   device   detail   获取   string   ssg   get   

原文地址:http://www.cnblogs.com/gr816/p/7903796.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!