二話不說,直接給代碼,如果您真想做這方面的東西,還是稍微研究下,沒有現成的好類用,就需要自己瞭解其原理
// 以下是調用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);
&nbs