Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#4.0 PInvoke USB
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 // 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 4-Jun-12 9:09am

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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:
 
[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
 
[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.
  Permalink  

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



Advertise | Privacy | Mobile
Web02 | 2.8.140926.1 | Last Updated 4 Jun 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100