将代码以查找 USB 设备从 C++ 转换为 C#

Converting code to find USB devices from C++ to C#

本文关键字:C++ 转换 代码 查找 USB      更新时间:2023-10-16

我在C++中得到了一些代码,可以找到连接的USB设备并为它们生成一些详细的输出,它使用setupapi.h获取信息。

    char * myClass::getUSBData(){
    HDEVINFO hDevInfo;
       SP_DEVINFO_DATA DeviceInfoData;
       DWORD i;
       // Create a HDEVINFO with all present devices.
       static GUID GUID_DEVINTERFACE_USB_DEVICE ={ 0xA5DCBF10L, 0x6530, 0x11D2, { 0x90, 0x1F, 0x00, 0xC0, 0x4F, 0xB9, 0x51, 0xED } };
       hDevInfo = SetupDiGetClassDevs(&GUID_DEVINTERFACE_USB_DEVICE, NULL, NULL, DIGCF_PRESENT | DIGCF_DEVICEINTERFACE);
       if (hDevInfo == INVALID_HANDLE_VALUE)
       {
           // Insert error handling here.
           return "";
       }
       // Enumerate through all devices in Set.
       DeviceInfoData.cbSize = sizeof(SP_DEVINFO_DATA);
       string jsonReturn="";
       int count = 0;
       for (i=0;SetupDiEnumDeviceInfo(hDevInfo,i,
           &DeviceInfoData);i++)
       {
           //actions i removed for the post
       }
    }

我找到了一些在 c# 中做类似事情的代码并对其进行了适当的修改(我相信),代码如下

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.InteropServices;
namespace ConsoleApplication1
{
    enum DigcfConstants : int
    {
        DIGCF_DEFAULT = 0x00000001,
        DIGCF_PRESENT = 0x00000002,
        DIGCF_ALLCLASSES = 0x00000004,
        DIGCF_PROFILE = 0x00000008,
        DIGCF_DEVICEINTERFACE = 0x00000010
    }
    [StructLayout(LayoutKind.Sequential)]
    struct SP_DEVINFO_DATA
    {
        public int cbSize;
        public GUID ClassGuid;
        public int DevInst;    // DEVINST handle
        public UIntPtr Reserved; // this is type'd as a pointer to a ULong in setupapi.h, however, in C/C++ a long is a DWord
    }

    [StructLayout(LayoutKind.Sequential, Size = 0x10)]
    public struct GUID
    {
        public Int64 Data1;
        public Int16 Data2;
        public Int16 Data3;
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 8)]
        public byte[] Data4;
        public GUID(Int64 d1, Int16 d2, Int16 d3, byte[] d4)
        {
            Data1 = d1;
            Data2 = d2;
            Data3 = d3;
            Data4 = new byte[8];
            Array.Copy(d4, Data4, d4.Length);
        }
    }
    class NativeMethods
    {
        [DllImport("Kernel32.dll")]
        public static extern int GetLastError();
        [DllImport("Setupapi.dll", SetLastError = true)]
        public static extern IntPtr SetupDiGetClassDevs(ref GUID guid, string enumerator, IntPtr parentHandle, int flags);
        [DllImport("Setupapi.dll")]
        [return: MarshalAs(UnmanagedType.Bool)]
        public static extern unsafe bool SetupDiEnumDeviceInfo(IntPtr handle, int index, ref SP_DEVINFO_DATA spDevInfoData);
    }
    class Program
    {
        public static unsafe void Main(string[] args)
        {
            GUID ddClassGuid;
            unchecked
            {
                // GUID_DEVINTERFACE_DISK
                ddClassGuid = new GUID(0xA5DCBF10L, (short)0x6530, 0x11D2, new byte[] { 0x90, 0x1F, 0x00, 0xC0, 0x4F, 0xB9, 0x51, 0xED });
            }
            IntPtr devList = NativeMethods.SetupDiGetClassDevs(ref ddClassGuid, null, IntPtr.Zero, (int)DigcfConstants.DIGCF_PRESENT | (int)DigcfConstants.DIGCF_DEVICEINTERFACE);
            if (devList.ToInt32() == -1)
            {
                Console.WriteLine("INVALID_HANDLE_VALUE");
                return;
            }
            else
            {
                Console.WriteLine("FOUND DEVICE");
                int index = 0;
                SP_DEVINFO_DATA sdd = new SP_DEVINFO_DATA();
                Console.WriteLine("cbSize = {0}", sdd.cbSize);
                bool test = NativeMethods.SetupDiEnumDeviceInfo(devList, index, ref sdd);
                while (NativeMethods.SetupDiEnumDeviceInfo(devList, index, ref sdd))
                {
                    Console.WriteLine("cbSize = {0}", sdd.cbSize);
                    index++;
                }
                //NativeMethods.SetupDiDestroyDeviceInfoList(devList);
                Console.ReadLine();
            }
        }
    }
}

找到设备后,它会进入 else 语句,但NativeMethods.SetupDiEnumDeviceInfo总是返回 false。 我认为这意味着这是我发送的参数有问题,但无法弄清楚。

有关如何返回连接的设备的任何帮助将不胜感激。

在这里,这个项目应该可以帮助你

http://www.codeproject.com/Articles/63878/Enumerate-and-Auto-Detect-USB-Drives