简体   繁体   English

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

[英]Converting code to find USB devices from C++ to C#

I have been given some code in C++ that finds attached USB devices and generates some detailed output for them it uses setupapi.h get the info. 我已经用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
       }
    }

I have found some code that does similar things in c# and modified it (i believe) appropriately, the code is below 我发现一些代码在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();
            }
        }
    }
}

it gets into the else statement after finding the device, but NativeMethods.SetupDiEnumDeviceInfo always returns a false. 找到设备后,它进入else语句,但是NativeMethods.SetupDiEnumDeviceInfo始终返回false。 i assume this means it's a problem with the parameters i am sending it, but can't figure it out. 我认为这意味着我发送的参数存在问题,但无法弄清楚。

any help on how to return the devices connected would be very much appreciated. 非常感谢您提供有关如何退回连接设备的任何帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM