Click here to Skip to main content
15,896,063 members

Obtaining Device Path of USB Device PInvoke

nanomass asked:

Open original thread
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.
Tags: C# (C# 4.0), PInvoke, USB

Plain Text
ASM
ASP
ASP.NET
BASIC
BAT
C#
C++
COBOL
CoffeeScript
CSS
Dart
dbase
F#
FORTRAN
HTML
Java
Javascript
Kotlin
Lua
MIDL
MSIL
ObjectiveC
Pascal
PERL
PHP
PowerShell
Python
Razor
Ruby
Scala
Shell
SLN
SQL
Swift
T4
Terminal
TypeScript
VB
VBScript
XML
YAML

Preview



When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the http://www.codeproject.com/info/cpol10.aspx.



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