C# 如何捕获一个USB设备发送到PC的数据(-)_自由飞翔,放飞自我_百度空间

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



郑重声明:资讯 【C# 如何捕获一个USB设备发送到PC的数据(-)_自由飞翔,放飞自我_百度空间】由 发布,版权归原作者及其所在单位,其原创性以及文中陈述文字和内容未经(企业库qiyeku.com)证实,请读者仅作参考,并请自行核实相关内容。若本文有侵犯到您的版权, 请你提供相关证明及申请并与我们联系(qiyeku # qq.com)或【在线投诉】,我们审核后将会尽快处理。
—— 相关资讯 ——