我目前正在尝试P / Invoke到HID(在Unity中(因此使用.Net 2.0))。 但是,在调用SetupDiGetDeviceInterfaceDetail时,它返回错误代码1784.
MIND:这是第二次调用,因为第一次调用应该(并且确实)返回错误122,并设置所需的缓冲区大小。
..Replaced With Full Source Below..
使用结果:
..Replaced With Full Source Below..
目前还没有使用DETAIL_DATA,我看到我应该使用IntPtr代替。
如何摆脱我的错误1784并获取/迭代我的设备路径? 另外,我读过我应该使用“null”,而不是dData,但这个错误也是如此(无法将null转换为DEVICE_INTERFACE_DATA)
编辑:完整的来源
DebugInput.cs:
using UnityEngine;
using System.Collections;
using System;
using System.Runtime.InteropServices;
using Microsoft.Win32.SafeHandles;
using Assets;
public class DebugInput : MonoBehaviour {
static readonly IntPtr INVALID_HANDLE_VALUE = new IntPtr(-1);
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if (Input.GetKeyDown(KeyCode.F3))
{
Debug.Log("Checking");
WindowsHID hid = new WindowsHID();
Guid gHid = hid.HIDGuid; // "4d1e55b2-f16f-11cf-88cb-001111000030"
Debug.Log(gHid);
IntPtr hDevInfo = hid.GetClassDevs(gHid); // 475463200L
if (hDevInfo != INVALID_HANDLE_VALUE)
{
Debug.Log("INTPTR RETREIVED");
WindowsHID.DEVICE_INTERFACE_DATA diData = new WindowsHID.DEVICE_INTERFACE_DATA();
diData.cbSize = Marshal.SizeOf(diData); // 32
Boolean Check = false;
uint enumerator = 0;
Check = hid.SetupInterfaces(hDevInfo, IntPtr.Zero, ref gHid, enumerator, ref diData); // Check = True, hDevInfo = 475463200L, diData.reserved = 653193792L (hDevInfo changes on each rebuild)
uint sizeNeeded;
WindowsHID.DEVINFO_DATA dData = new WindowsHID.DEVINFO_DATA();
dData.cbSize = (uint)Marshal.SizeOf(dData); // 32u
bool result3 = WindowsHID.SetupDiGetDeviceInterfaceDetail(hDevInfo, diData, IntPtr.Zero, 0, out sizeNeeded, dData); // sizeNeeded becomes 180u
if (!result3)
{
int error = Marshal.GetLastWin32Error(); // Expecting error 122, since we are only setting SizeNeeded Here (getting error 122)
Debug.Log(error);
}
IntPtr DeviceInterfaceDetailData = Marshal.AllocHGlobal((int)sizeNeeded); // 4640736L
try
{
uint size = sizeNeeded; // 180u
Marshal.WriteInt32(DeviceInterfaceDetailData, (int)size); // DevinceInterfaceDetailData doesnt change (IS THIS LINE NEEDED?)
bool result4 = WindowsHID.SetupDiGetDeviceInterfaceDetail(hDevInfo, diData, DeviceInterfaceDetailData, size, out sizeNeeded, dData);
if (!result4)
{
int error = Marshal.GetLastWin32Error(); // GETTING ERROR 1784???
Debug.Log(error);
}
}
finally
{
Marshal.FreeHGlobal(DeviceInterfaceDetailData);
}
}
else
{
Debug.Log("INVALID GUID");
}
}
}
}
WindowsHID.cs:
using UnityEngine;
using System;
using System.Runtime.InteropServices;
using System.Collections;
namespace Assets
{
public class WindowsHID
{
/// <summary>Used in SetupDiClassDevs to get devices present in the system</summary>
protected const int DIGCF_PRESENT = 0x02;
/// <summary>Used in SetupDiClassDevs to get device interface details</summary>
protected const int DIGCF_DEVICEINTERFACE = 0x10;
protected const int DIGCF_ALLCLASSES = 0x04;
static readonly IntPtr INVALID_HANDLE_VALUE = new IntPtr(-1);
public WindowsHID()
{
}
/// <summary>
/// Provides Info About a Single USB-Device
/// </summary>
[StructLayout(LayoutKind.Sequential)]
public struct DEVICE_INTERFACE_DATA
{
public Int32 cbSize;
public Guid interfaceClassGuid;
public Int32 flags;
public UIntPtr reserved;
}
[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Auto, Pack=1)]
public struct DEVICE_INTERFACE_DETAIL_DATA
{
public int cbSize;
public char DevicePath;
}
[StructLayout(LayoutKind.Sequential)]
public struct DEVINFO_DATA
{
public uint cbSize;
public Guid classGuid;
public uint devInst;
public IntPtr reserved;
}
#region P/Invoke
/// <summary>
/// Gets the Windows GUID for the HID class Devices
/// </summary>
/// <param name="guid"></param>
[DllImport("hid.dll", SetLastError = true)]
protected static extern void HidD_GetHidGuid(out Guid guid);
/// <summary>
///
/// </summary>
/// <param name="ClassGuid"></param>
/// <param name="Enumerator"></param>
/// <param name="hwndParent"></param>
/// <param name="Flags"></param>
/// <returns></returns>
[DllImport("setupapi.dll", CharSet = CharSet.Auto, SetLastError = true)]
static extern IntPtr SetupDiGetClassDevs(ref Guid ClassGuid, [MarshalAs(UnmanagedType.LPTStr)] string Enumerator, IntPtr hwndParent, uint Flags);
/// <summary>
///
/// </summary>
/// <param name="hDevInfo"></param>
/// <param name="devInfo"></param>
/// <param name="interfaceClassGuid"></param>
/// <param name="memberIndex"></param>
/// <param name="deviceInterfaceData"></param>
/// <returns></returns>
[DllImport("setupapi.dll", SetLastError = true)]
protected static extern Boolean SetupDiEnumDeviceInterfaces(IntPtr hDevInfo, int devInfo, ref Guid interfaceClassGuid, uint memberIndex, ref DEVICE_INTERFACE_DATA deviceInterfaceData);
/// <summary>
///
/// </summary>
/// <param name="hDevInfo"></param>
/// <param name="deviceInterfaceData"></param>
/// <param name="deviceInterfaceDetailData"></param>
/// <param name="deviceInterfaceDetailDataSize"></param>
/// <param name="requiredSize"></param>
/// <param name="deviceInfoData"></param>
/// <returns></returns>
[DllImport("setupapi.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern Boolean SetupDiGetDeviceInterfaceDetail(IntPtr hDevInfo, DEVICE_INTERFACE_DATA deviceInterfaceData, IntPtr deviceInterfaceDetailData, uint deviceInterfaceDetailDataSize, out uint requiredSize, DEVINFO_DATA deviceInfoData);
#endregion
public Guid HIDGuid
{
get
{
Guid gHid;
HidD_GetHidGuid(out gHid);
return gHid;
}
}
public IntPtr GetClassDevs(Guid gHid)
{
return SetupDiGetClassDevs(ref gHid, null, IntPtr.Zero, DIGCF_DEVICEINTERFACE);// | DIGCF_ALLCLASSES);
}
public Boolean SetupInterfaces(IntPtr hDevInfo, IntPtr devInfo, ref Guid interfaceClassGuid, uint memberIndex, ref DEVICE_INTERFACE_DATA deviceInterfaceData)
{
Boolean res = SetupDiEnumDeviceInterfaces(hDevInfo, 0, ref interfaceClassGuid, memberIndex, ref deviceInterfaceData);
int a = Marshal.GetLastWin32Error();
return res;
}
public Boolean SetupInterfaceDetail(IntPtr hDevInfo, DEVICE_INTERFACE_DATA deviceInterfaceData, IntPtr deviceInterfaceDetailData, uint deviceInterfaceDetailDataSize, out uint requiredSize, DEVINFO_DATA deviceInfoData)
{
Boolean a = SetupDiGetDeviceInterfaceDetail(hDevInfo, deviceInterfaceData, deviceInterfaceDetailData, deviceInterfaceDetailDataSize, out requiredSize, deviceInfoData);
int b = Marshal.GetLastWin32Error();
return a;
}
}
}
答案 0 :(得分:3)
您需要替换以下行:
Marshal.WriteInt32(DeviceInterfaceDetailData, (int)size);
通过
Marshal.WriteInt32(DeviceInterfaceDetailData, IntPtr.Size == 8 ? 8 : 6);
因为SetupDiGetDeviceInterfaceDetail文档明确说明了这一点:
调用者必须将DeviceInterfaceDetailData.cbSize设置为 调用此函数之前的sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA)。 cbSize成员始终包含固定部分的大小 数据结构,而不是反映可变长度字符串的大小 结束。
sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA)
取决于进程位数,它是带有X86进程的6(字节打包+ 1个字符,auto - &gt; unicode - &gt; 4 + 2 * 1)和8和X64进程(无论如何都是8字节打包)。
另请注意,您对SetupDiGetDeviceInterfaceDetail的定义应使用结构的ref参数。一旦你改变了所有这些,就可以获得这样的设备路径:
string devicePath = Marshal.PtrToStringAuto(DeviceInterfaceDetailData + 4);
(PtrToStringAuto因为您在p / invoke定义中选择了自动字符集)