已阅读5页,还剩2页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Text;using System.Windows.Forms;using System.Runtime.InteropServices;using System.Threading;using System.Collections;using System.IO;/以下是调用windows的API的函数 /获得GUID DllImport(hid.dll) public static extern void HidD_GetHidGuid(ref Guid HidGuid); Guid guidHID = Guid.Empty; /过滤设备,获取需要的设备 DllImport(setupapi.dll, SetLastError = true) public static extern IntPtr SetupDiGetClassDevs(ref Guid ClassGuid, uint Enumerator, IntPtr HwndParent, DIGCF Flags); IntPtr hDevInfo; /获取设备,true获取到 DllImport(setupapi.dll, CharSet = CharSet.Auto, SetLastError = true) public static extern Boolean SetupDiEnumDeviceInterfaces(IntPtr hDevInfo, IntPtr devInfo, ref Guid interfaceClassGuid, UInt32 memberIndex, ref SP_DEVICE_INTERFACE_DATA deviceInterfaceData); public struct SP_DEVICE_INTERFACE_DATA public int cbSize ; public Guid interfaceClassGuid; public int flags; public int reserved; / 获取接口的详细信息 必须调用两次 第1次返回长度 第2次获取数据 DllImport(setupapi.dll, SetLastError = true, CharSet = CharSet.Auto) private static extern bool SetupDiGetDeviceInterfaceDetail(IntPtr deviceInfoSet, ref SP_DEVICE_INTERFACE_DATA deviceInterfaceData, IntPtr deviceInterfaceDetailData, int deviceInterfaceDetailDataSize, ref int requiredSize, SP_DEVINFO_DATA deviceInfoData); StructLayout(LayoutKind.Sequential) public class SP_DEVINFO_DATA public int cbSize = Marshal.SizeOf(typeof(SP_DEVINFO_DATA); public Guid classGuid = Guid.Empty; / temp public int devInst = 0; / dumy public int reserved = 0; StructLayout(LayoutKind.Sequential, Pack = 2) internal struct SP_DEVICE_INTERFACE_DETAIL_DATA internal int cbSize; internal short devicePath; public enum DIGCF DIGCF_DEFAULT = 0x1, DIGCF_PRESENT = 0x2, DIGCF_ALLCLASSES = 0x4, DIGCF_PROFILE = 0x8, DIGCF_DEVICEINTERFACE = 0x10 /获取设备文件 DllImport(kernel32.dll, SetLastError = true) private static extern int CreateFile( string lpFileName, / file name uint dwDesiredAccess, / access mode uint dwShareMode, / share mode uint lpSecurityAttributes, / SD uint dwCreationDisposition, / how to create uint dwFlagsAndAttributes, / file attributes uint hTemplateFile / handle to template file ); /读取设备文件 DllImport(Kernel32.dll,SetLastError = true) private static extern bool ReadFile ( IntPtr hFile, byte lpBuffer, uint nNumberOfBytesToRead, ref uint lpNumberOfBytesRead, IntPtr lpOverlapped ); /释放设备 DllImport(hid.dll) static public extern bool HidD_FreePreparsedData(ref IntPtr PreparsedData); /关闭访问设备句柄,结束进程的时候把这个加上保险点 DllImport(kernel32.dll) static public extern int CloseHandle(int hObject);/接下来是访问设备的代码/代码暂时没有整理,传入参数是设备序号,/有些USB设备其实有很多HID设备,就是一个接口上有几个设备,这个时候需要/用index+来逐个循环,直到获取设备返回false后,跳出去,把获取的设备/路径全记录下来就好了,我这里知道具体设备号,所以没有循环,浪费我时间 /定于句柄序号和一些参数,具体可以去网上找这些API的参数说明,后文我看能不能把资料也写上去 int HidHandle = -1; public const uint GENERIC_READ = 0x80000000; public const uint GENERIC_WRITE = 0x40000000; public const uint FILE_SHARE_READ= 0x00000001; public const uint FILE_SHARE_WRITE = 0x00000002; public const int OPEN_EXISTING = 3;private void UsBMethod(int index) HidD_GetHidGuid(ref guidHID); hDevInfo = SetupDiGetClassDevs(ref guidHID, 0, IntPtr.Zero, DIGCF.DIGCF_PRESENT | DIGCF.DIGCF_DEVICEINTERFACE); int bufferSize = 0; ArrayList HIDUSBAddress = new ArrayList(); /while (true) / /获取设备,true获取到 SP_DEVICE_INTERFACE_DATA DeviceInterfaceData = new SP_DEVICE_INTERFACE_DATA(); DeviceInterfaceData.cbSize = Marshal.SizeOf(DeviceInterfaceData); /for (int i = 0; i 3; i+) / bool result = SetupDiEnumDeviceInterfaces(hDevInfo, IntPtr.Zero, ref guidHID, (UInt32)index, ref DeviceInterfaceData); / /第一次调用出错,但可以返回正确的Size SP_DEVINFO_DATA strtInterfaceData = new SP_DEVINFO_DATA(); result = SetupDiGetDeviceInterfaceDetail(hDevInfo, ref DeviceInterfaceData, IntPtr.Zero, 0, ref bufferSize, strtInterfaceData); /第二次调用传递返回值,调用即可成功 IntPtr detailDataBuffer = Marshal.AllocHGlobal(bufferSize); SP_DEVICE_INTERFACE_DETAIL_DATA detailData = new SP_DEVICE_INTERFACE_DETAIL_DATA(); detailData.cbSize = Marshal.SizeOf(typeof(SP_DEVICE_INTERFACE_DETAIL_DATA); Marshal.StructureToPtr(detailData, detailDataBuffer, false); result = SetupDiGetDeviceInterfaceDetail(hDevInfo, ref DeviceInterfaceData, detailDataBuffer, bufferSize, ref bufferSize, strtInterfaceData); if (result = false) /break; /获取设备路径访 IntPtr pdevicePathName = (IntPtr)(int)detailDataBuffer + 4); string devicePathName = Marshal.PtrToStringAuto(pdevicePathName); HIDUSBAddress.Add(devicePathName); /index+; /break; / /连接设备文件 int aa = CT_CreateFile(devicePathName); bool bb = USBDataRead(HidHandle); /建立和设备的连接 public unsafe int CT_CreateFile(string DeviceName) HidHandle = CreateFile( DeviceName, GENERIC_READ,/ | GENERIC_WRITE,/读写,或者一起 FILE_SHARE_READ,/ | FILE_SHARE_WRITE,/共享读写,或者一起 0, OPEN_EXISTING, 0, 0); if (HidHandle = -1) return 0; else return 1; /根据CreateFile拿到的设备handle访问文件,并返回数据 public unsafe bool USBDataRead(int handle) while (true) uint read = 0; /注意字节的长度,我这里写
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 深度解析(2026)《GBT 33525-2017输送带 覆盖层性能 类别》
- 深度解析(2026)《GBT 33434-2016船舶电弧焊烟尘排放率测定方法》
- 任务2.2 通知首选项设置
- 医疗数据安全治理:区块链应用模式
- (南开中学)重庆市高2026届高三第五次质量检测语文试卷(含答案详解)
- 医疗数据安全成熟度:区块链灾备方案
- 医疗数据安全应急演练中的技术融合路径
- 医疗数据安全培训的区块链技术应用流程优化
- 医疗数据安全合规性风险应对措施
- 医疗数据安全共享绩效评价
- 交通运输行业数据集建设实施方案
- 年会礼仪小姐培训
- 工程建设砂石运输方案(3篇)
- 民族团结教学课件
- 神经介入进修汇报课件
- 物业服务保密措施方案
- (2025年标准)简单砌石墙协议书
- 济南市2025-2030年中小学及幼儿园布局规划方案公示细节
- 重庆市涪陵榨菜集团股份有限公司营运能力分析
- 感染患者终末消毒操作规范
- 《中华民族共同体概论》考试复习题库(含答案)
评论
0/150
提交评论