91超碰碰碰碰久久久久久综合_超碰av人澡人澡人澡人澡人掠_国产黄大片在线观看画质优化_txt小说免费全本

溫馨提示×

C#中WinUSB設備的識別與枚舉

c#
小樊
82
2024-08-12 00:09:56
欄目: 編程語言

在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設備的操作。

0
曲水县| 桂阳县| 广元市| 交城县| 睢宁县| 万全县| 古蔺县| 焦作市| 佳木斯市| 台北县| 阿图什市| 莱阳市| 乃东县| 滨海县| 宜阳县| 邻水| 临洮县| 宁国市| 罗定市| 满城县| 柘城县| 钟山县| 留坝县| 淳化县| 基隆市| 搜索| 绥德县| 丘北县| 信宜市| 蕲春县| 尚志市| 江源县| 南木林县| 壤塘县| 柘城县| 洮南市| 兴城市| 道孚县| 海盐县| 云龙县| 翼城县|