【问题标题】:Find HID Devices C#查找 HID 设备 C#
【发布时间】:2013-12-02 20:50:55
【问题描述】:

我需要连接到连接到计算机的 USB 设备,我环顾四周,找到了一些示例项目,但无法使其正常工作。一切运行良好,但我什至没有进入 while 函数。 由于SetupDiGetClassDevs 来自 setupapi.dll 我无法调试此函数。我确实连接了设备,所以这不是我的问题。 hInfoSet 也确实得到 size = 8。

public static HIDDevice FindDevice(int nVid, int nPid, Type oType)
{
    string strPath = string.Empty;
    string strSearch = string.Format("vid_{0:x4}&pid_{1:x4}", nVid, nPid); // first, build the path search string
    Guid gHid;
    HidD_GetHidGuid(out gHid);  // next, get the GUID from Windows that it uses to represent the HID USB interface
    IntPtr hInfoSet = SetupDiGetClassDevs(ref gHid, null, IntPtr.Zero, DIGCF_DEVICEINTERFACE | DIGCF_PRESENT);  // this gets a list of all HID devices currently connected to the computer (InfoSet)
    try
    {
        DeviceInterfaceData oInterface = new DeviceInterfaceData(); // build up a device interface data block
        oInterface.Size = Marshal.SizeOf(oInterface);
        // Now iterate through the InfoSet memory block assigned within Windows in the call to SetupDiGetClassDevs
        // to get device details for each device connected
        int nIndex = 0;
        while (SetupDiEnumDeviceInterfaces(hInfoSet, 0, ref gHid, (uint)nIndex, ref oInterface))    // this gets the device interface information for a device at index 'nIndex' in the memory block
        {
           string strDevicePath = GetDevicePath(hInfoSet, ref oInterface);  // get the device path (see helper method 'GetDevicePath')
            if (strDevicePath.IndexOf(strSearch) >= 0)  // do a string search, if we find the VID/PID string then we found our device!
            {
                HIDDevice oNewDevice = (HIDDevice)Activator.CreateInstance(oType);  // create an instance of the class for this device
                oNewDevice.Initialise(strDevicePath);   // initialise it with the device path
                return oNewDevice;  // and return it
            }
            nIndex++;   // if we get here, we didn't find our device. So move on to the next one.
        }
    }
    finally
    {
        // Before we go, we have to free up the InfoSet memory reserved by SetupDiGetClassDevs
        SetupDiDestroyDeviceInfoList(hInfoSet);
    }
    return null;    
}

【问题讨论】:

    标签: c# usb hid setupapi


    【解决方案1】:

    如果没有进入,您是否尝试过调用 Marshal.GetLastWin32Error 并在 while 循环之后打印该值?确保在 SetupDiEnumDeviceInterfaces 上启用 SetLastError。

    然后,在此处查找您的错误代码: http://msdn.microsoft.com/en-us/library/windows/desktop/ms681381%28v=vs.85%29.aspx

    编辑 - 阅读您的评论后,结构应如下所示:

    [StructLayout(LayoutKind.Sequential)]
    struct GUID
    {
        public int a;
        public short b;
        public short c;
        [MarshalAs(UnmanagedType.ByValArray, SizeConst=8)]
        public byte[] d;
    }
    
    [StructLayout(LayoutKind.Sequential)]
    struct SP_DEVICE_INTERFACE_DATA
    {
        public uint cbSize;
        public GUID InterfaceClassGuid;
        public uint Flags;
        public IntPtr Reserved;
    }
    

    【讨论】:

    • 谢谢!我现在试了一下,得到“1784:提供的用户缓冲区对请求的操作无效”。但是我仍然不确定如何解决这个问题,你知道为什么会发生这个错误吗?
    • 我的猜测是您的“DeviceInterfaceData”结构没有正确编组(Marshal.SizeOf 返回错误的大小)。检查我编辑的帖子。
    猜你喜欢
    • 2011-11-02
    • 2012-02-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-10
    • 2013-04-05
    相关资源
    最近更新 更多