转换代码以查找从C ++到C#的USB设备

时间:2014-02-26 17:47:36

标签: c# c++

我已经在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。我认为这意味着它是我发送它的参数的问题,但无法搞清楚。

非常感谢任何有关如何退回连接设备的帮助。

1 个答案:

答案 0 :(得分:0)