CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 12 of 12
  1. #1
    Join Date
    Dec 2004
    Posts
    10

    SetupDiEnumDeviceInterfaces()

    Hello,

    I have a problem with SetupDiEnumDeviceInterfaces(). It returns FALSE and GetLastError() returns ERROR_NO_MORE_ITEMS. It never returns TRUE meaning it thinks there's no devices attached to the USB port. I have a USB device connected. Device manager says it's set up correctly. I'm passing the ClassGuid for the USB device class.

    I'm wondering if anyone knows why it never returns TRUE?

    Thanks.

    Here's the code:
    #include "stdafx.h"
    #include "windows.h"
    #include "Setupapi.h"
    #include "stdio.h"

    struct __declspec(uuid("36FC9E60-C465-11CF-8056-444553540000")) uuidUsbDevClass
    {
    };



    int main(int argc, char* argv[])
    {
    char szTraceBuf[256];
    GUID guidUsbDevClass = __uuidof(uuidUsbDevClass);
    // Get device interface info set handle for all devices attached to system
    HDEVINFO hDevInfo = SetupDiGetClassDevs(
    &guidUsbDevClass, /* CONST GUID * ClassGuid - USB class GUID */
    NULL, /* PCTSTR Enumerator */
    NULL, /* HWND hwndParent */
    DIGCF_DEVICEINTERFACE | DIGCF_PRESENT /* DWORD Flags */
    );

    if (hDevInfo == INVALID_HANDLE_VALUE)
    {
    sprintf(szTraceBuf, "SetupDiClassDevs() failed. GetLastError() " \
    "returns: 0x%x\n", GetLastError());
    OutputDebugString(szTraceBuf);
    printf("SetupDiClassDevs() failed. GetLastError() " \
    "returns: 0x%x\n", GetLastError());
    return 1;
    }

    sprintf(szTraceBuf, "Device info set handle for all devices attached to " \
    "system: 0x%x\n", hDevInfo);
    OutputDebugString(szTraceBuf);
    printf("Device info set handle for all devices attached to " \
    "system: 0x%x\n", hDevInfo);

    // Retrieve a context structure for a device interface of a device
    // information set.
    DWORD dwIndex = 0;
    SP_DEVICE_INTERFACE_DATA devInterfaceData;
    ZeroMemory(&devInterfaceData, sizeof(SP_DEVICE_INTERFACE_DATA));
    devInterfaceData.cbSize = sizeof(SP_DEVICE_INTERFACE_DATA);
    BOOL bRet = FALSE;
    while(TRUE)
    {
    bRet = SetupDiEnumDeviceInterfaces(
    hDevInfo, /* HDEVINFO DeviceInfoSet */
    NULL, /* PSP_DEVINFO_DATA DeviceInfoData */
    &guidUsbDevClass, /* CONST GUID * InterfaceClassGuid */
    dwIndex,
    &devInterfaceData /* PSP_DEVICE_INTERFACE_DATA DeviceInterfaceData */
    );
    if (!bRet)
    {
    sprintf(szTraceBuf, "SetupDiEnumDeviceInterfaces failed " \
    "GetLastError() returns: 0x%x\n", GetLastError());
    OutputDebugString(szTraceBuf);
    printf("SetupDiEnumDeviceInterfaces failed " \
    "GetLastError() returns: 0x%x\n", GetLastError());

    if (GetLastError() == ERROR_NO_MORE_ITEMS)
    {
    break;
    }
    }

    dwIndex++;
    }

    sprintf(szTraceBuf, "Number of device interface sets representing all " \
    "devices attached to system: 0x%x\n", dwIndex);
    OutputDebugString(szTraceBuf);
    printf("Number of device interface sets representing all " \
    "devices attached to system: 0x%x\n", dwIndex);

    SetupDiDestroyDeviceInfoList(hDevInfo);

    return 0;
    }

  2. #2
    Join Date
    Dec 2004
    Posts
    10

    Re: SetupDiEnumDeviceInterfaces()

    Here's more to add:
    I'm doing this enumeration at the application level (user mode) not driver level (kernel mode).

    In order to be able to enumerate the devices does ther driver have to call IoRegisterDeviceInterface()?

    Funny thing is when I call SetDiGetClassDevs() with whatever combonation for the 1st and 4th argument's I always get back the same handle. I'm passing the ClassGuid for the USB devices that come under the registry key:
    HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Enum\USB
    My theroy is that if you pass only DIGCF_DEVICEINTERFACE as the 4th arg along with the USB ClassGuid then SetupDiEnumDeviceInterfaces() should return a count = to the number of univeral serial bus controllers listed in the Computer Management dialog under the device manager tree icon.
    If you pass the combonation of flags DIGCF_PRESENT | DIGCF_DEVICEINTERFACE and I have my device attached to the host SetupDiEnumDeviceInterfaces() should return 1.

    That's all for now.

    Thanks,
    Scott

  3. #3
    Join Date
    Dec 2004
    Posts
    10

    Re: SetupDiEnumDeviceInterfaces()

    Problem solved. I was using the wrong GUID. The correct GUID is:
    A5DCBF10-6530-11D2-901F-00C04FB951ED.
    Here the code that works. Its basically the same except for how the GUID is declared and passed to functions SetupDiGetClassDevs() and SetupDiEnumDeviceInterfaces().

    #include "stdafx.h"
    #include "windows.h"
    #include "Setupapi.h"
    #include "stdio.h"

    static /*const*/ GUID GUID_DEVINTERFACE_USB_DEVICE =
    { 0xA5DCBF10L, 0x6530, 0x11D2, { 0x90, 0x1F, 0x00, 0xC0, 0x4F, 0xB9, 0x51, 0xED } };



    int main(int argc, char* argv[])
    {
    char szTraceBuf[256];
    // Get device interface info set handle for all devices attached to system
    HDEVINFO hDevInfo = SetupDiGetClassDevs(
    &GUID_DEVINTERFACE_USB_DEVICE, /* CONST GUID * ClassGuid - USB class GUID */
    NULL, /* PCTSTR Enumerator */
    NULL, /* HWND hwndParent */
    DIGCF_PRESENT | DIGCF_DEVICEINTERFACE /* DWORD Flags */
    );

    if (hDevInfo == INVALID_HANDLE_VALUE)
    {
    sprintf(szTraceBuf, "SetupDiClassDevs() failed. GetLastError() " \
    "returns: 0x%x\n", GetLastError());
    OutputDebugString(szTraceBuf);
    printf("SetupDiClassDevs() failed. GetLastError() " \
    "returns: 0x%x\n", GetLastError());
    return 1;
    }

    sprintf(szTraceBuf, "Device info set handle for all devices attached to " \
    "system: 0x%x\n", hDevInfo);
    OutputDebugString(szTraceBuf);
    printf("Device info set handle for all devices attached to " \
    "system: 0x%x\n", hDevInfo);

    // Retrieve a context structure for a device interface of a device
    // information set.
    DWORD dwIndex = 0;
    SP_DEVICE_INTERFACE_DATA devInterfaceData;
    ZeroMemory(&devInterfaceData, sizeof(SP_DEVICE_INTERFACE_DATA));
    devInterfaceData.cbSize = sizeof(SP_DEVICE_INTERFACE_DATA);
    BOOL bRet = FALSE;
    while(TRUE)
    {
    bRet = SetupDiEnumDeviceInterfaces(
    hDevInfo, /* HDEVINFO DeviceInfoSet */
    NULL, /* PSP_DEVINFO_DATA DeviceInfoData */
    &GUID_DEVINTERFACE_USB_DEVICE, /* CONST GUID * InterfaceClassGuid */
    dwIndex,
    &devInterfaceData /* PSP_DEVICE_INTERFACE_DATA DeviceInterfaceData */
    );
    if (!bRet)
    {
    sprintf(szTraceBuf, "SetupDiEnumDeviceInterfaces failed " \
    "GetLastError() returns: 0x%x\n", GetLastError());
    OutputDebugString(szTraceBuf);
    printf("SetupDiEnumDeviceInterfaces failed " \
    "GetLastError() returns: 0x%x\n", GetLastError());

    if (GetLastError() == ERROR_NO_MORE_ITEMS)
    {
    break;
    }
    }

    dwIndex++;
    }

    sprintf(szTraceBuf, "Number of device interface sets representing all " \
    "devices attached to system: 0x%x\n", dwIndex);
    OutputDebugString(szTraceBuf);
    printf("Number of device interface sets representing all " \
    "devices attached to system: 0x%x\n", dwIndex);

    SetupDiDestroyDeviceInfoList(hDevInfo);

    return 0;
    }

  4. #4
    Join Date
    Mar 2005
    Posts
    1

    Thumbs up Re: SetupDiEnumDeviceInterfaces()

    Congratulation!

  5. #5
    Join Date
    Jun 2005
    Posts
    1

    Re: SetupDiEnumDeviceInterfaces()

    I really need to know where you found the device interface GUID as distinct from the device setup GUID.....

  6. #6
    Join Date
    Mar 2005
    Posts
    12

    Re: SetupDiEnumDeviceInterfaces()

    I really need to know where you found the device interface GUID as distinct from the device setup GUID.....
    Yes, so do I.

    Has anyone got the (current) 'interface GUID' header file that they could post here?

    Keith

  7. #7
    Join Date
    Sep 2005
    Posts
    1

    Re: SetupDiEnumDeviceInterfaces()

    Look in the winioctl.h header in the sdk include directory. make sure to include initguid.h as well if you plan to use the predefined guids.
    eric

  8. #8
    Join Date
    Mar 2005
    Posts
    12

    Re: SetupDiEnumDeviceInterfaces()

    Quote Originally Posted by guitarmy
    Look in the winioctl.h header in the sdk include directory. make sure to include initguid.h as well if you plan to use the predefined guids.
    eric
    Thanks guitarmy... unfortunately winioctl.h contains GUIDs for IO only (disk, comport etc).

    I think the only way to get them is with the MS DDK, and that's not available for free download.

    Keith

  9. #9
    Join Date
    Apr 2006
    Posts
    1

    Re: SetupDiEnumDeviceInterfaces()

    /*
    USB specific GUIDs
    */


    /* f18a0e88-c30c-11d0-8815-00a0c906bed8 */
    DEFINE_GUID(GUID_DEVINTERFACE_USB_HUB, 0xf18a0e88, 0xc30c, 0x11d0, 0x88, 0x15, 0x00, \
    0xa0, 0xc9, 0x06, 0xbe, 0xd8);

    /* A5DCBF10-6530-11D2-901F-00C04FB951ED */
    DEFINE_GUID(GUID_DEVINTERFACE_USB_DEVICE, 0xA5DCBF10L, 0x6530, 0x11D2, 0x90, 0x1F, 0x00, \
    0xC0, 0x4F, 0xB9, 0x51, 0xED);

    /* 3ABF6F2D-71C4-462a-8A92-1E6861E6AF27 */
    DEFINE_GUID(GUID_DEVINTERFACE_USB_HOST_CONTROLLER, 0x3abf6f2d, 0x71c4, 0x462a, 0x8a, 0x92, 0x1e, \
    0x68, 0x61, 0xe6, 0xaf, 0x27);

    /* 4E623B20-CB14-11D1-B331-00A0C959BBD2 */
    DEFINE_GUID(GUID_USB_WMI_STD_DATA, 0x4E623B20L, 0xCB14, 0x11D1, 0xB3, 0x31, 0x00,\
    0xA0, 0xC9, 0x59, 0xBB, 0xD2);

    /* 4E623B20-CB14-11D1-B331-00A0C959BBD2 */
    DEFINE_GUID(GUID_USB_WMI_STD_NOTIFICATION, 0x4E623B20L, 0xCB14, 0x11D1, 0xB3, 0x31, 0x00,\
    0xA0, 0xC9, 0x59, 0xBB, 0xD2);


    /*
    Obsolete device interface class GUID names.
    (use of above GUID_DEVINTERFACE_* names is recommended).
    --*/

    #define GUID_CLASS_USBHUB GUID_DEVINTERFACE_USB_HUB
    #define GUID_CLASS_USB_DEVICE GUID_DEVINTERFACE_USB_DEVICE
    #define GUID_CLASS_USB_HOST_CONTROLLER GUID_DEVINTERFACE_USB_HOST_CONTROLLER

    #define FILE_DEVICE_USB FILE_DEVICE_UNKNOWN

  10. #10
    Join Date
    Nov 2006
    Posts
    1

    Re: SetupDiEnumDeviceInterfaces()

    Quote Originally Posted by optionalreaction View Post
    Thanks guitarmy... unfortunately winioctl.h contains GUIDs for IO only (disk, comport etc).

    I think the only way to get them is with the MS DDK, and that's not available for free download.

    Keith
    I know this thread is really old - just if someone is still looking, you can find guids in devguid.h header.

  11. #11
    Join Date
    May 2006
    Posts
    3

    Re: SetupDiEnumDeviceInterfaces()

    OR

    they will try this

    USB SymbolicLink Generator v-2009.1.0.0.200

    http://ulymar.u4m2.com/wp-content/up...09/09/main.PNG



    http://ulymar.u4m2.com/?p=64


    br

  12. #12
    Join Date
    Jun 2011
    Posts
    3

    Question SetupDiEnumDeviceInterfaces()

    Hi

    From my application, I need to get handle(s) to the AHCI controller(s) on a system that is/are using a particular driver (like "msahci").

    I tried using SetupDixxx() APIs:

    Step 1: Fetched the DeviceInformationSet for PCI devices.

    hDevInfo = SetupDiGetClassDevs(NULL,
    REGSTR_KEY_PCIENUM, // Enumerator
    0,
    DIGCF_PRESENT | DIGCF_ALLCLASSES);
    Step 2: Enumerated all the elements in the set.

    for (i=0;SetupDiEnumDeviceInfo(hDevInfo,i,&DeviceInfoData);i++)

    Step 3: Retrieved Device Registry Property (SPDRP_SERVICE) for each element.

    while (!SetupDiGetDeviceRegistryProperty( hDevInfo,
    &DeviceInfoData,
    SPDRP_SERVICE , //To list the service used by the Device
    &DataT,
    (PBYTE)buffer,
    buffersize,
    &buffersize))

    Step 4: Comparing details returned from each device with the required value:
    if(strcmp(buffer, DRIVER_NAME) == 0)
    {
    printf("\n **** This is the required controller.****");
    }

    Step 5: ???????
    Now I need to get "DeviceInterfaceData" for this element, so that I can get the 'path'
    by using "SetupDiGetDeviceInterfaceDetail()"

    Step 6: CreateFile() using the 'path' received from the previous step, to get handle to
    the controller.

    I have got stuck in 'step 5' because to get DeviceInterfaceData we can use
    SetupDiEnumDeviceInterfaces() which needs InterfaceClass GUID but what is available with me is Setup class GUID. So, am not able to proceed

    Request for help from anyone having faced same problem and got some solution to it.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured