要从C#中唯一标识USB密钥,可以使用Windows API函数SetupDiGetDeviceInterfaceDetail
来获取USB设备的详细信息,包括设备的实例ID和设备路径。实例ID是一个唯一标识符,可以用于识别特定的USB设备。
以下是一个C#示例代码,演示如何使用SetupDiGetDeviceInterfaceDetail
函数获取USB设备的实例ID:
using System;
using System.Runtime.InteropServices;
public class USBDevice
{
[DllImport("setupapi.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern bool SetupDiDestroyDeviceInfoList(IntPtr DeviceInfoSet);
[DllImport("setupapi.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern IntPtr SetupDiGetClassDevs(ref Guid ClassGuid, string Enumerator, IntPtr hwndParent, int Flags);
[DllImport("setupapi.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern bool SetupDiEnumDeviceInterfaces(IntPtr DeviceInfoSet, int DeviceInfoData, ref Guid InterfaceClassGuid, int MemberIndex, ref SP_DEVICE_INTERFACE_DATA DeviceInterfaceData);
[DllImport("setupapi.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern bool SetupDiGetDeviceInterfaceDetail(IntPtr DeviceInfoSet, ref SP_DEVICE_INTERFACE_DATA DeviceInterfaceData, ref SP_DEVICE_INTERFACE_DETAIL_DATA DeviceInterfaceDetailData, int DeviceInterfaceDetailDataSize, ref int RequiredSize, IntPtr DeviceInfoData);
public static string GetUSBDeviceInstanceID(Guid guid)
{
int bufferSize = 1024;
IntPtr detailDataBuffer = Marshal.AllocHGlobal(bufferSize);
SP_DEVICE_INTERFACE_DATA interfaceData = new SP_DEVICE_INTERFACE_DATA();
SP_DEVICE_INTERFACE_DETAIL_DATA detailData = new SP_DEVICE_INTERFACE_DETAIL_DATA();
IntPtr deviceInfoSet = SetupDiGetClassDevs(ref guid, null, IntPtr.Zero, 1);
int index = 0;
try
{
while (SetupDiEnumDeviceInterfaces(deviceInfoSet, 0, ref guid, index, ref interfaceData))
{
int requiredSize = 0;
bool success = SetupDiGetDeviceInterfaceDetail(deviceInfoSet, ref interfaceData, ref detailData, bufferSize, ref requiredSize, IntPtr.Zero);
if (!success && Marshal.GetLastWin32Error() != 14) // ERROR_INSUFFICIENT_BUFFER
{
throw new Win32Exception();
}
if (requiredSize > bufferSize)
{
bufferSize = requiredSize;
detailDataBuffer = Marshal.ReAllocHGlobal(detailDataBuffer, (IntPtr)bufferSize);
detailData.cbSize = 5;
success = SetupDiGetDeviceInterfaceDetail(deviceInfoSet, ref interfaceData, ref detailData, bufferSize, ref requiredSize, IntPtr.Zero);
}
if (success)
{
string devicePath = detailData.DevicePath;
string instanceID = devicePath.Substring(devicePath.LastIndexOf('\\') + 1);
return instanceID;
}
index++;
}
}
finally
{
SetupDiDestroyDeviceInfoList(deviceInfoSet);
Marshal.FreeHGlobal(detailDataBuffer);
}
return null;
}
}
[StructLayout(LayoutKind.Sequential)]
public struct SP_DEVICE_INTERFACE_DATA
{
public int cbSize;
public Guid InterfaceClassGuid;
public int Flags;
public int Reserved;
}
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
public struct SP_DEVICE_INTERFACE_DETAIL_DATA
{
public int cbSize;
public string DevicePath;
}
使用USBDevice.GetUSBDeviceInstanceID(Guid.Empty)
方法可以获取第一个USB设备的实例ID。如果要获取其他USB设备的实例ID,可以修改index
变量的值。
请注意,这个方法只能在Windows操作系统上运行。
领取专属 10元无门槛券
手把手带您无忧上云