Click here to Skip to main content
15,885,365 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
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.

C#
 [StructLayout(LayoutKind.Sequential)]
  public struct SP_DEVICE_INTERFACE_DETAIL_DATA // user made struct to store device path
      {
            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.
Posted

1 solution

Ok I found what my mistake was, problem was that I forgot to place the CharSet, property to auto when I created the struct.

I had:

C#
[StructLayout(LayoutKind.Sequential)]
  public struct SP_DEVICE_INTERFACE_DETAIL_DATA // user made struct to store device path
      {
            public UInt32 cbSize;
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256)]
            public string DevicePath;
      }


When It should have been

C#
[StructLayout(LayoutKind.Sequential), CharSet = CharSet.Auto]
  public struct SP_DEVICE_INTERFACE_DETAIL_DATA // user made struct to store device path
      {
            public UInt32 cbSize;
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256)]
            public string DevicePath;
      }


Hope this can help someone in the future.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900