Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Win32Api USB SetupDiGetDeviceInterfaceDetail fail

Tags:

c#

.net

winapi

usb

I am attempting to connect to a USB GPS device. I can successfully connect to the device if I manually create a file via CreateFile WinApi (using the path specified in Device Manager).

However, when I try to select the device through enumeration I fail @ the SetupDiGetDeviceInterfaceDetail call.

I have C code that works correctly, but my C# translation does not appear to work correctly. I have tried many variations with essentially the same results.

C Code that works

// Get enumerator handle for the specified ClassGuid
HDEVINFO theDevInfo = SetupDiGetClassDevs((GUID*)&GUID_DEVINTERFACE_GRMNUSB, NULL, NULL,
    DIGCF_PRESENT | DIGCF_INTERFACEDEVICE);

SP_DEVICE_INTERFACE_DATA theInterfaceData;
theInterfaceData.cbSize = sizeof(theInterfaceData);

// populate theInterfaceData which contains device class information
if (!SetupDiEnumDeviceInterfaces(theDevInfo, NULL, (GUID*)&GUID_DEVINTERFACE_GRMNUSB, 0, &theInterfaceData) &&
    GetLastError() == ERROR_NO_MORE_ITEMS)
{
  gHandle = 0;
  return;
}
// This is normally used to obtain the device path information using theInterfaceData obtained above
bool initialized = SetupDiGetDeviceInterfaceDetail(theDevInfo, &theInterfaceData, NULL, 0, &theBytesReturned, NULL);
// theBytesReturned = 83
theDevDetailData =
(PSP_INTERFACE_DEVICE_DETAIL_DATA)malloc(theBytesReturned);
theDevDetailData->cbSize = sizeof(SP_INTERFACE_DEVICE_DETAIL_DATA);

bool initialized = SetupDiGetDeviceInterfaceDetail(theDevInfo, &theInterfaceData, theDevDetailData, theBytesReturned, NULL, &theDevInfoData);

C#

[DllImport(@"setupapi.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern Boolean SetupDiGetDeviceInterfaceDetail(
    IntPtr hDevInfo,
    ref SP_DEVICE_INTERFACE_DATA deviceInterfaceData,
    IntPtr deviceInterfaceDetailData,
    UInt32 deviceInterfaceDetailDataSize,
    out UInt32 requiredSize,
    IntPtr deviceInfoData
    );

[StructLayout(LayoutKind.Sequential)]
public struct SP_DEVICE_INTERFACE_DATA
{
    public Int32 cbSize;
    public Guid interfaceClassGuid;
    public Int32 flags;
    private UIntPtr reserved;
}


// Get enumerator handle for the specified ClassGuid
IntPtr theDevInfo = SetupDiGetClassDevs(ref ClassGuid, (DiGetClassFlags.DIGCF_PRESENT | DiGetClassFlags.DIGCF_DEVICEINTERFACE));

SP_DEVICE_INTERFACE_DATA DevInterfaceData = new SP_DEVICE_INTERFACE_DATA();
DevInterfaceData.cbSize = Marshal.SizeOf(DevInterfaceData);

initialized = SetupDiEnumDeviceInterfaces(theDevInfo, IntPtr.Zero, ref ClassGuid, 0,
        ref DevInterfaceData); 
// I assume The DevInterfaceData is populated correctly as it matches the C Code
// And I've compared the values in memory and they match

uint bytesReturned = 0;
initialized = SetupDiGetDeviceInterfaceDetail(theDevInfo, ref DevInterfaceData, IntPtr.Zero, 0, out bytesReturned, IntPtr.Zero);
// I expect bytesReturned = 83 and initialized = true which is the value that is returned in the C Code
// Instead the value 162 is returned
like image 363
galford13x Avatar asked Jun 29 '26 13:06

galford13x


1 Answers

Congratulations, it is working. You'll get a Unicode string, it's twice as long. And a FALSE return is correct. You just need to call Marshal.GetLastWin32Error() and verify that you got ERROR_INSUFFICIENT_BUFFER. Your C code is broken, probably because you forgot to initialize theBytesReturned to zero.

like image 189
Hans Passant Avatar answered Jul 02 '26 02:07

Hans Passant



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!