C#中WinUSB设备的识别与枚举

avatar
作者
筋斗云
阅读量:0

在C#中识别和枚举WinUSB设备,我们可以使用Windows API函数来实现。下面是一个简单的示例代码,演示了如何使用C#来识别和枚举WinUSB设备:

using System; using System.Runtime.InteropServices;  class Program {     [StructLayout(LayoutKind.Sequential)]     public struct SP_DEVICE_INTERFACE_DATA     {         public int cbSize;         public Guid interfaceClassGuid;         public int flags;         public IntPtr reserved;     }      [StructLayout(LayoutKind.Sequential)]     public struct SP_DEVINFO_DATA     {         public int cbSize;         public Guid classGuid;         public int devInst;         public IntPtr reserved;     }      [DllImport("setupapi.dll", SetLastError = true)]     public static extern IntPtr SetupDiGetClassDevs(ref Guid classGuid, IntPtr enumerator, IntPtr hwndParent, int flags);      [DllImport("setupapi.dll", SetLastError = true)]     public static extern bool SetupDiEnumDeviceInterfaces(IntPtr deviceInfoSet, IntPtr deviceInfoData, ref Guid interfaceClassGuid, int memberIndex, ref SP_DEVICE_INTERFACE_DATA deviceInterfaceData);      [DllImport("setupapi.dll", SetLastError = true)]     public static extern bool SetupDiGetDeviceInterfaceDetail(IntPtr deviceInfoSet, ref SP_DEVICE_INTERFACE_DATA deviceInterfaceData, IntPtr deviceInterfaceDetailData, int deviceInterfaceDetailDataSize, ref int requiredSize, ref SP_DEVINFO_DATA deviceInfoData);      static void Main(string[] args)     {         Guid guid = new Guid("{88BAE032-5A81-49f0-BC3D-A4FF138216D6}"); // WinUSB GUID          IntPtr deviceInfoSet = SetupDiGetClassDevs(ref guid, IntPtr.Zero, IntPtr.Zero, 0);         if (deviceInfoSet != IntPtr.Zero)         {             SP_DEVICE_INTERFACE_DATA deviceInterfaceData = new SP_DEVICE_INTERFACE_DATA();             deviceInterfaceData.cbSize = Marshal.SizeOf(deviceInterfaceData);              for (int i = 0; SetupDiEnumDeviceInterfaces(deviceInfoSet, IntPtr.Zero, ref guid, i, ref deviceInterfaceData); i++)             {                 // Get device path                 int requiredSize = 0;                 SP_DEVINFO_DATA deviceInfoData = new SP_DEVINFO_DATA();                 if (SetupDiGetDeviceInterfaceDetail(deviceInfoSet, ref deviceInterfaceData, IntPtr.Zero, 0, ref requiredSize, ref deviceInfoData))                 {                     IntPtr deviceInterfaceDetailData = Marshal.AllocHGlobal(requiredSize);                     try                     {                         // Fill the deviceInterfaceDetailData                         if (SetupDiGetDeviceInterfaceDetail(deviceInfoSet, ref deviceInterfaceData, deviceInterfaceDetailData, requiredSize, ref requiredSize, ref deviceInfoData))                         {                             // Get device path from deviceInterfaceDetailData                             // Do something with the device path                         }                     }                     finally                     {                         Marshal.FreeHGlobal(deviceInterfaceDetailData);                     }                 }             }         }     } } 

在上面的示例中,我们使用了SetupDiGetClassDevsSetupDiEnumDeviceInterfacesSetupDiGetDeviceInterfaceDetail这几个Windows API函数来获取WinUSB设备的信息。通过调用这些函数,我们可以获取WinUSB设备的路径信息等。在实际应用中,我们可以根据获取到的设备路径信息,来进行对WinUSB设备的操作。

广告一刻

为您即时展示最新活动产品广告消息,让您随时掌握产品活动新动态!