locked
SetupDiEnumDeviceInterface always return false RRS feed

  • Question

  • Gets,

     I've seen a lot of replies regarding this subject, but unfortunatelly I can't find what am I doing wrong. I' am trying to get a device path using windows API. However, every call that i make to SetupDiEnumDeviceInterface always return false. I've check the Guid I'm using thousand times, and even the simpler sample (call to SetupDiEnumDeviceInterface using HidD_GetHidGuid to return Hid devices info) is returning false. here is my piece of code:

    [Flags]
    
    private enum SetupDiGetClassDevsFlags : uint
    
    {
    
    DIGCF_DEFAULT = 0x00000001, // only valid with DIGCF_DEVICEINTERFACE
    
    DIGCF_PRESENT = 0x00000002,
    
    DIGCF_ALLCLASSES = 0x00000004,
    
    DIGCF_PROFILE = 0x00000008,
    
    DIGCF_DEVICEINTERFACE = 0x00000010
    
    }
    
    [StructLayout(LayoutKind.Sequential)]
    
    private struct SP_DEVINFO_DATA
    
    {
    
    int cbsize;
    
    Guid ClassGuid;
    
    int DevInst;
    
    ulong Reserved;
    
    }
    
    [StructLayout(LayoutKind.Sequential)]
    
    public struct SP_DEVICE_INTERFACE_DATA
    
    {
    
    public uint cbSize;
    
    public Guid InterfaceClassGuid;
    
    public uint Flags;
    
    public IntPtr Reserved;
    
    }
    
     
    
     
    
    [DllImport("setupAPI.dll",CharSet = CharSet.Auto)]
    
    static extern IntPtr SetupDiGetClassDevs(ref Guid ClassGuid, IntPtr Enumerator, IntPtr hwndParent, int Flags);
    
    [DllImport("setupAPI.dll", CharSet = CharSet.Auto)]
    
    static extern bool SetupDiDestroyDeviceInfoList(IntPtr DeviceInfoSet);
    
    [DllImport("setupAPI.dll", CharSet = CharSet.Auto)]
    
    static extern bool SetupDiEnumDeviceInterfaces(IntPtr hDevInfo, int devInfo, ref Guid interfaceClassGuid, UInt32 memberIndex, ref SP_DEVICE_INTERFACE_DATA deviceInterfaceData);
    
    [DllImport("hid.dll", SetLastError = true)]
    
    static extern void HidD_GetHidGuid(ref Guid hidGuid);
    
    //first step: Get a pointer to DEVICE_INFO_SET
    
    //USB_HOST_CONTROLLER GUID
    
    Guid guid = new Guid("3ABF6F2D-71C4-462A-8A92-1E6861E6AF27");
    
    //HidD_GetHidGuid(ref guid);
    
    IntPtr h = SetupDiGetClassDevs(ref guid, IntPtr.Zero, IntPtr.Zero, (int)SetupDiGetClassDevsFlags.DIGCF_PRESENT);
    
    if (h != INVALID_HANDLE_VALUE)
    
    {
    
    this.listBox1.Items.Add("SetupDiGetClassDevs called!");
    
    this.listBox1.Items.Add("pointer: "+ h.ToString());
    
    }
    
    else
    
    {
    
    this.listBox1.Items.Add("SetupDiGetClassDevs call failed.");
    
    }
    
    //Second step: Create SP_DEVICE_INTERFACE_DATA structure and enumerate all devices from class guid
    
    this.listBox1.Items.Add("Trying to create SP_DEVICE_INTERFACE_DATA..."); 
    
    SP_DEVINFO_DATA d = new SP_DEVINFO_DATA();
    
    SP_DEVICE_INTERFACE_DATA spdata = new SP_DEVICE_INTERFACE_DATA();
    
    spdata.cbSize = (uint)Marshal.SizeOf(spdata);
    
    uint i = 1;
    
    bool s = true;
    
    bool gone = false;
    
    this.listBox1.Items.Add("SP_DEVICE_INTERFACE_DATA created and cbSize set!");
    
    this.listBox1.Items.Add("Calling SetupDiEnumDeviceInterfaces...");
    
    s = SetupDiEnumDeviceInterfaces(h, 0, ref guid, i, ref spdata);
    
    while (s)
    
    {
    
    this.listBox1.Items.Add("SetupDiEnumDeviceInterfaces return true!");
    
    gone = true;
    
    i++;
    
    }
    
    if (!gone)
    
    {
    
    int err = Marshal.GetLastWin32Error();
    
    this.listBox1.Items.Add("GetLastWin32Error return err number " + err.ToString());
    
    this.listBox1.Items.Add("SetupDiEnumDeviceInterfaces return false.");
    
    }
    
    //Last step: Destroy pointer do DEVICE_INFO_SET and release resources
    
    if (SetupDiDestroyDeviceInfoList(h))
    
    {
    
    this.listBox1.Items.Add("SetupDiDestroyDeviceInfoList call completed successfully!");
    
    }
    
    else
    
    {
    
    this.listBox1.Items.Add("SetupDiDestroyDeviceInfoList call did not complete successfully.");
    
    }
    
    }
    
    
    


    

     

    Can anyone tell me what I'am doing wrong? Why SetupDiEnumDeviceInterface always return false? Last, but no least, I' am using VS C# Express 2010 on Windows 7 64 bits.

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     
    MCP,MCSA2000,MCSA-Messaging,MCSA2003,MCSE2000,MCAD-C#
    Saturday, September 3, 2011 3:33 PM

Answers

  • Bruno Silveira wrote:
    >
    >   Thanks for the reply. That was just an example. My app should identify
    >a specific device (Samsung Galaxy S 2 phone), get his path and then
    >transfer a file from computer to phone, using CreateFile.
    >
    >   What is the best way to accomplish this task?
     
    The best way is to find a published API to accomplish the task.  Unless you
    know which ioctls to call once you open the device, what you're doing now
    is just stabbing randomly in the dark.  What leads you to believe your
    current path will lead to success?  I'm not saying it won't, I'm just
    wondering if you found a sample that shows this.
    --
    Tim Roberts, timr@probo.com
    Providenza & Boekelheide, Inc.
     

    Tim Roberts, DDK MVP Providenza & Boekelheide, Inc.
    • Marked as answer by Bruno Silveira Tuesday, September 6, 2011 2:14 PM
    Monday, September 5, 2011 7:20 PM

All replies

  • Bruno Silveira wrote:
    >
    >I've seen a lot of replies regarding this subject, but unfortunatelly I
    >can't find what am I doing wrong.
     
    Several things, it would appear...  What is it that you are actually trying
    to achieve here?
     
    >I' am trying to get a device path using windows API. However, every
    >call that i make to SetupDiEnumDeviceInterface always return false.
    >I've check the Guid I'm using thousand times, ...
    >...
    >Guid guid = new Guid("3ABF6F2D-71C4-462A-8A92-1E6861E6AF27");
     
    That's GUID_DEVINTERFACE_USB_HOST_CONTROLLER.  It is a device interface
    GUID.  If you want to search for device interface guids, you need to
    specify DIGCF_DEVICEINTERFACE in the call to SetupDiGetClassDevs:
     
    >IntPtr h = SetupDiGetClassDevs(ref guid, IntPtr.Zero, IntPtr.Zero, (int)SetupDiGetClassDevsFlags.DIGCF_PRESENT);
     
    Without that flag, what you're doing is searching for devices with that
    GUID as their install class (that is, the ClassGuid value in their INF
    file).  There aren't any.
     
    Do you really want to search for host controllers, or was that just an
    example?
    --
    Tim Roberts, timr@probo.com
    Providenza & Boekelheide, Inc.
     

    Tim Roberts, DDK MVP Providenza & Boekelheide, Inc.
    Saturday, September 3, 2011 11:19 PM
  • Tim,

       Thanks for the reply. That was just an example. My app should identify a specific device (Samsung Galaxy S 2 phone), get his path and then transfer a file from computer to phone, using CreateFile.

       What is the best way to accomplish this task?

    Regards,

    Bruno


    MCP,MCSA2000,MCSA-Messaging,MCSA2003,MCSE2000,MCAD-C#
    Sunday, September 4, 2011 1:03 AM
  • Bruno Silveira wrote:
    >
    >   Thanks for the reply. That was just an example. My app should identify
    >a specific device (Samsung Galaxy S 2 phone), get his path and then
    >transfer a file from computer to phone, using CreateFile.
    >
    >   What is the best way to accomplish this task?
     
    The best way is to find a published API to accomplish the task.  Unless you
    know which ioctls to call once you open the device, what you're doing now
    is just stabbing randomly in the dark.  What leads you to believe your
    current path will lead to success?  I'm not saying it won't, I'm just
    wondering if you found a sample that shows this.
    --
    Tim Roberts, timr@probo.com
    Providenza & Boekelheide, Inc.
     

    Tim Roberts, DDK MVP Providenza & Boekelheide, Inc.
    • Marked as answer by Bruno Silveira Tuesday, September 6, 2011 2:14 PM
    Monday, September 5, 2011 7:20 PM
  • Tim,

       Thanks for the tip. These are my first projects with Windows API, so I' m struggling a little bit to find the things that I want. After your comment, I searched deeper and found WPD API, which is just what I'm looking for. Thanks.

    Regards,

    Bruno


    MCP,MCSA2000,MCSA-Messaging,MCSA2003,MCSE2000,MCAD-C#
    Tuesday, September 6, 2011 2:17 PM