I get correct Device path with this test :
{
string sDevicePath = null;
Guid hidGuid;
HidD_GetHidGuid(out hidGuid);
IntPtr hDeviceInfoSet = SetupDiGetClassDevs(ref hidGuid, IntPtr.Zero, IntPtr.Zero, DIGCF_DEVICEINTERFACE | DIGCF_PRESENT);
if (hDeviceInfoSet != IntPtr.Zero)
{
var did = new SP_DEVICE_INTERFACE_DATA() { cbSize = Marshal.SizeOf(typeof(SP_DEVICE_INTERFACE_DATA)) };
for (var nIndex = 0; SetupDiEnumDeviceInterfaces(hDeviceInfoSet, IntPtr.Zero, ref hidGuid, nIndex, ref did); nIndex++)
{
int dwBytes = 0;
bool bRet = SetupDiGetDeviceInterfaceDetail(hDeviceInfoSet, ref did, IntPtr.Zero, 0, ref dwBytes, IntPtr.Zero);
if (!bRet && Marshal.GetLastWin32Error() == ERROR_INSUFFICIENT_BUFFER)
{
SP_DEVICE_INTERFACE_DETAIL_DATA didd = new SP_DEVICE_INTERFACE_DETAIL_DATA();
didd.cbSize = IntPtr.Size == 8 ? 8 : 6;
SP_DEVINFO_DATA dd = new SP_DEVINFO_DATA();
dd.cbSize = Marshal.SizeOf(dd);
bRet = SetupDiGetDeviceInterfaceDetail(hDeviceInfoSet, ref did, ref didd, dwBytes, ref dwBytes, ref dd);
if (bRet)
{
sDevicePath = didd.DevicePath;
Console.WriteLine("Device Path : {0}", sDevicePath);
}
}
}
SetupDiDestroyDeviceInfoList(hDeviceInfoSet);
}
}
with :
[DllImport("Hid.dll", CharSet = CharSet.Unicode, SetLastError = true)]
public static extern void HidD_GetHidGuid(out Guid HidGuid);
[DllImport("Setupapi.dll", CharSet = CharSet.Unicode, SetLastError = true)]
public static extern IntPtr SetupDiGetClassDevs(ref Guid ClassGuid, IntPtr Enumerator, IntPtr hWndParent, int Flags);
public const int DIGCF_PRESENT = 0x00000002;
public const int DIGCF_DEVICEINTERFACE = 0x00000010;
public const int ERROR_INSUFFICIENT_BUFFER = 122;
public const int MAX_PATH = 260;
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode, Pack = PACK_SIZE)]
public struct SP_DEVINFO_DATA
{
public int cbSize;
public Guid ClassGuid;
public int DevInst;
public IntPtr Reserved;
}
#if WIN64
public const int PACK_SIZE = 8;
#else
public const int PACK_SIZE = 1;
#endif
[DllImport("Setupapi.dll", CharSet = CharSet.Unicode, SetLastError = true)]
public static extern bool SetupDiGetDeviceInterfaceDetail(IntPtr DeviceInfoSet, ref SP_DEVICE_INTERFACE_DATA DeviceInterfaceData, ref SP_DEVICE_INTERFACE_DETAIL_DATA DeviceInterfaceDetailData, int DeviceInterfaceDetailDataSize, ref int RequiredSize, ref SP_DEVINFO_DATA DeviceInfoData );
[DllImport("Setupapi.dll", CharSet = CharSet.Unicode, SetLastError = true)]
public static extern bool SetupDiGetDeviceInterfaceDetail(IntPtr DeviceInfoSet, ref SP_DEVICE_INTERFACE_DATA DeviceInterfaceData, IntPtr Ptr, int DeviceInterfaceDetailDataSize, ref int RequiredSize, IntPtr PtrInfo);
[DllImport("Setupapi.dll", CharSet = CharSet.Unicode, SetLastError = true)]
public static extern bool SetupDiEnumDeviceInterfaces(IntPtr DeviceInfoSet, IntPtr DeviceInfoData, ref Guid InterfaceClassGuid, int MemberIndex, ref SP_DEVICE_INTERFACE_DATA DeviceInterfaceData);
[StructLayout(LayoutKind.Sequential)]
public struct SP_DEVICE_INTERFACE_DATA
{
public int cbSize;
public Guid InterfaceClassGuid;
public int Flags;
public IntPtr Reserved;
}
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
public struct SP_DEVICE_INTERFACE_DETAIL_DATA
{
public int cbSize;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = MAX_PATH)]
public string DevicePath;
}
[DllImport("Setupapi.dll", CharSet = CharSet.Unicode, ExactSpelling = true, SetLastError = true)]
public static extern bool SetupDiDestroyDeviceInfoList(IntPtr DeviceInfoSet);