簡體   English   中英

轉換代碼以將USB設備從C ++轉換為C#

[英]Converting code to find USB devices from C++ to 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。 我認為這意味着我發送的參數存在問題,但無法弄清楚。

非常感謝您提供有關如何退回連接設備的任何幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM