我正在尝试使用SetupDiGetDevicePropertyW方法,但没有成功。
P / Invoke声明为:
// Device Property
[StructLayout(LayoutKind.Sequential)]
internal struct DEVPROPKEY
{
public Guid fmtid;
public UInt32 pid;
}
/// <summary>
/// Provides details about a single USB device.
/// </summary>
[StructLayout(LayoutKind.Sequential, Pack = 1)]
internal struct DeviceInterfaceData
{
public int Size;
public Guid InterfaceClassGuid;
public int Flags;
public UIntPtr Reserved;
}
[DllImport("setupapi.dll", SetLastError = true)]
internal static extern bool SetupDiGetDevicePropertyW(
IntPtr deviceInfoSet,
ref DeviceInterfaceData DeviceInfoData,
ref DEVPROPKEY propertyKey,
out UInt64 propertyType, // or Uint32 ?
IntPtr propertyBuffer, // or byte[]
uint propertyBufferSize,
out uint requiredSize,
UInt32 flags);
/// <summary>
/// Gets a new key for the bus reported device description.
/// </summary>
internal static DEVPROPKEY DEVPKEY_Device_BusReportedDeviceDesc
{
get
{
DEVPROPKEY key = new DEVPROPKEY();
key.pid = 4;
key.fmtid = new Guid((uint)0x540b947e, (ushort)0x8b40, (ushort)0x45bc, 0xa8, 0xa2, 0x6a, 0x0b, 0x89, 0x4c, 0xbd, 0xa2);
return key;
}
}
我打电话给:
uint propertyRegDataType = 0;
uint requiredSize = 0;
IntPtr buffer = Marshal.AllocHGlobal(1024);
if (UnsafeNativeMethods.SetupDiGetDevicePropertyW(infoSet, ref deviceInterfaceData, ref devicePropertyKey, out propertyType, buffer, 1024, out requiredSize, 0))
{
}
else
{
int test = Marshal.GetLastWin32Error(); // returns 87 - invalid parameter
}
infoSet的值来自:
IntPtr infoSet = UnsafeNativeMethods.SetupDiGetClassDevs(ref hidGuid, null, IntPtr.Zero, DIGCF_DEVICEINTERFACE | DIGCF_PRESENT);
deviceInterfaceData的值包括接口的GUID,来自:
UnsafeNativeMethods.SetupDiEnumDeviceInterfaces(infoSet, 0, ref hidGuid, (uint)i, ref deviceInterfaceData);
hidGuid来自(其中结果是Guid对象:
UnsafeNativeMethods.HidD_GetHidGuid(out result);
我在这里做错了什么?