kyau8

kyau8

  • NA
  • 1
  • 0

SetupDiGetDeviceInterfaceDetail problem in C#

Feb 16 2006 8:03 PM
I am trying to communicate with USB via the setupapi.dll APIs. I have no problem calling SetupDiGetClassDevs, SetupDiEnumDeviceInterfaces, and the first call of SetupDiGetDeviceInterfaceDetail. But my second call of the SetupDiGetDeviceInterfaceDetail does not return the device path. Here is part of my C# code:

[StrucLayout( LayoutKind.Sequential, Pack = 1)]
public class DeviceInterfaceDetailData
{
    public Int32 cbSize;
    [MarshalAs( UnmanagedType.ByValArray, SizeConst = 1)]
    public byte[] devicePath;
}
...

[DllImport( "setupapi.dll" )]
public static extern Boolean SetupDiGetDeviceInterfaceDetail(
    IntPtr deviceInformationSet,
    DeviceInterfaceData deviceInterfaceData,
    IntPtr deviceInterfaceDetailData,
    Int32 deviceInterfaceDetailDataSize,
    ref Int32 requiredSize,
    IntPtr deviceInfoData );

...

// After first call to SetupDiGetDeviceInterfaceDetail, it returns a requiredSize of 84.

...
IntPtr buffer = Marshal.AllocHGlobal( requiredSize );
DeviceInterfaceDetailData detailData = new DeviceInterfaceDetailData();
detailData.cbSize = Marshal.SizeOf( detailData );  // cbSize = 5
detailData.devicePath = new byte[ requiredSize-4 ];
Marshal.StructureToPtr( detailData, buffer, false );

if ( SetupDiGetDeviceInterfaceDetail( deviceInfoSet, deviceInterfaceData, buffer, requiredSize, ref requiredSize, IntPtr.Zero ) == false )
{
    return false;
}

// after this call, buffer does not contain the device path.

IntPtr pDevicePath = (IntPtr)((int)buffer + 4);
string deviceName = Marshal.PtrToStringAuto( pDevicePath );


Any help would be greatly appreciated!!

Answers (1)