首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

如何从C#中唯一标识USB密钥?

要从C#中唯一标识USB密钥,可以使用Windows API函数SetupDiGetDeviceInterfaceDetail来获取USB设备的详细信息,包括设备的实例ID和设备路径。实例ID是一个唯一标识符,可以用于识别特定的USB设备。

以下是一个C#示例代码,演示如何使用SetupDiGetDeviceInterfaceDetail函数获取USB设备的实例ID:

代码语言:csharp
复制
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操作系统上运行。

页面内容是否对你有帮助?
有帮助
没帮助

相关·内容

领券