Hi, I am working with the setupapi.dll in order to control a USB device.
I am calling the SetupDiGetDeviceInterfaceDetail function to obtain the device path, of my USB device. The function is called successfully however the device path returns as "/". here is the code That I am using. I suspect that has to do something with the formatting of the native method. Here is the
Link to the PInvoke method.
[StructLayout(LayoutKind.Sequential)]
public struct SP_DEVICE_INTERFACE_DETAIL_DATA
{
public UInt32 cbSize;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256)]
public string DevicePath;
}
[DllImport("setupapi.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern Boolean SetupDiGetDeviceInterfaceDetail(IntPtr hDevInfo, ref SP_DEVICE_INTERFACE_DATA deviceInterfaceData, ref SP_DEVICE_INTERFACE_DETAIL_DATA deviceInterfaceDetailData,
UInt32 deviceInterfaceDetailDataSize, out UInt32 requiredSize, ref SP_DEVINFO_DATA deviceInfoData);
public List<string> GetDetailsDeviceInterface()
{
try
{
UInt32 nBytes = 256;
UInt32 nRequiredSize;
List<string> DetailsList = new List<string>();
if (SetupAPICalls.SetupDiGetDeviceInterfaceDetail(info, ref DeviceData, ref DetailData, nBytes, out nRequiredSize, ref DeviceInfo));
{
UInt32 reg_type;
IntPtr ptrBuf = new IntPtr();
ptrBuf = Marshal.AllocHGlobal((int)BUFFER_SIZE);
Debug.WriteLine("Device Path: "+DetailData.DevicePath);
DetailsList.Add(DetailData.DevicePath);
if (SetupAPICalls.SetupDiGetDeviceRegistryProperty(info, ref DeviceInfo, 0x00000000, out reg_type, ptrBuf, nBytes, out nRequiredSize))
{
string ControllerDeviceDesc = Marshal.PtrToStringAuto(ptrBuf);
Debug.WriteLine("Device description: " + ControllerDeviceDesc);
DetailsList.Add(ControllerDeviceDesc);
return DetailsList;
}
else
{
throw new Exception("DfuSe: Unable to obtain Device description: " + new Win32Exception(Marshal.GetLastWin32Error()).Message);
}
}
throw new Exception("DfuSe: Unable to obtain Device Info, please Connect device: "+new Win32Exception(Marshal.GetLastWin32Error()).Message);
}
catch(Win32Exception ex)
{
throw ex;
}
}
Thanks in anticipation.