C# 使用SetupDiGetDevicePropertyW获取USB信息
我正在尝试让SetupDiGetDevicePropertyW方法工作,但没有成功 p/Invoke声明为:C# 使用SetupDiGetDevicePropertyW获取USB信息,c#,winforms,winapi,C#,Winforms,Winapi,我正在尝试让SetupDiGetDevicePropertyW方法工作,但没有成功 p/Invoke声明为: // Device Property [StructLayout(LayoutKind.Sequential)] internal struct DEVPROPKEY { public Guid fmtid; public UInt32 pid; } /// <summary> /// Provides details about a single USB d
// 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;
}
}
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);
我在这里做错了什么?我刚刚意识到我在使用DeviceInterfaceData,而我应该使用从SetupDiEnumDeviceInfo调用派生的DeviceInfo数据。如果您不需要进一步的帮助,请删除您的问题。
UnsafeNativeMethods.HidD_GetHidGuid(out result);