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;
namespace GameControl
{
class Class1
{
//以下是調用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);
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;
public 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();
string devicePathName = "";
while (true)
{
//獲取設備,true獲取到
SP_DEVICE_INTERFACE_DATA DeviceInterfaceData = new SP_DEVICE_INTERFACE_DATA();
DeviceInterfaceData.cbSize = Marshal.SizeOf(DeviceInterfaceData);
bool result = false;
for (int i = 0; i < 3; i++)
{
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);
devicePathName = Marshal.PtrToStringAuto(pdevicePathName);
HIDUSBAddress.Add(devicePathName);
index++;
break;
}
//連接設備文件
int aa = CT_CreateFile(devicePathName);
bool bb = USBDataRead(HidHandle);
}
//建立和設備的連接
public 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 bool USBDataRead(int handle)
{
while (true)
{
uint read = 0;
//注意字節的長度,我這里寫的是8位,其實可以通過API獲取具體的長度,這樣安全點,
//具體方法我知道,但是沒有寫,過幾天整理完代碼,一起給出來
Byte[] m_rd_data = new Byte[8];
bool isread = ReadFile((IntPtr)handle, m_rd_data, (uint)8, ref read, IntPtr.Zero);
//這里已經是拿到的數據了
Byte[] m_rd_dataout = new Byte[read];
Array.Copy(m_rd_data, m_rd_dataout, read);
}
}
}
}
參考
https://github.com/allanpk716/USB4ScanGun