0

I'm trying to find the vendor id and the product id of a USB card. For this purpose, I use setupapi.dll. In my code, I use the SetupDiGetDeviceInterfaceDetail call twice and on the second time, the function returns true with no error, but I don't know what I can do after that.

My code:

result = HidD_GetHidGuid(ref HidGuid);
DeviceInfoSet = SetupDiGetClassDevs(ref HidGuid, IntPtr.Zero, IntPtr.Zero, 18);
do
{
  SP_DEVICE_INTERFACE_DATA MyDeviceInterfaceData = new SP_DEVICE_INTERFACE_DATA();
  MyDeviceInterfaceData.cbSize = 28;

  result = SetupDiEnumDeviceInterfaces(DeviceInfoSet, IntPtr.Zero, ref HidGuid,
    MemberIndex, ref MyDeviceInterfaceData);
  if (result != 0)
  {
    result = SetupDiGetDeviceInterfaceDetail(DeviceInfoSet, ref MyDeviceInterfaceData,
      IntPtr.Zero, 0, out neededSize, IntPtr.Zero);
    //int lastError = Marshal.GetLastWin32Error();
    int requiredSize = (int)neededSize;
    // build a DevInfo Data structure
    SP_DEVINFO_DATA da = new SP_DEVINFO_DATA();
    da.cbSize = Marshal.SizeOf(da);

    SP_DEVICE_INTERFACE_DETAIL_DATA MyDeviceInterfaceDetailData = new
      SP_DEVICE_INTERFACE_DETAIL_DATA();
    if (IntPtr.Size == 8) // for 64 bit operating systems
      MyDeviceInterfaceDetailData.cbSize = 8;
    else
      MyDeviceInterfaceDetailData.cbSize = 4 + Marshal.SystemDefaultCharSize;
        // for 32 bit systems

    IntPtr detailDataBuffer = Marshal.AllocHGlobal(requiredSize);
    Marshal.StructureToPtr(MyDeviceInterfaceDetailData, detailDataBuffer, false);
    result = SetupDiGetDeviceInterfaceDetail(DeviceInfoSet, ref MyDeviceInterfaceData,
      detailDataBuffer, requiredSize, out neededSize, IntPtr.Zero);
    int lastError = Marshal.GetLastWin32Error();

When I try the following code, the result is "-" :

string instanceID = Marshal.PtrToStringAuto(detailDataBuffer);

How can I get the vendor ID and the product ID?

Sabuncu
  • 5,095
  • 5
  • 55
  • 89
boboch
  • 63
  • 2
  • 7

1 Answers1

0

In the SP_DEVICE_INTERFACE_DETAIL_DATA struct you find the DevicePath, which is something like \?\hid#vid_045e&pid_00f0#7&13ac544b&0&0000#{GUID}. Just fiddle out the vid_045e and pid_00f0 part.

Uwe Sieber
  • 66
  • 4