SetupDiGetClassDevs is Causing a Crash on Windows 11 (OS Build 22621.525) Win32 CPP

Priyank-INTC 6 Reputation points
2022-11-18T17:32:02.69+00:00

I am trying to get the list of devices for the "GUID_DEVINTERFACE_USB_HUB", In order to do that I am trying to get the DeviceInfo handle first, and it is causing a crash. It is throwing the error for Access Violation and causing a crash. The line which is causing the crash is Highlighted in Bold and Italics.

void EnumerateUsb4HostCtrl()  
{  
    LPGUID                              Guid = nullptr;  
    HDEVINFO                            DeviceInfo = nullptr;  
    DWORD                               Index = 0;  
    BOOL                                Result = FALSE;  
    BYTE                                Buf[512]{};  
    PSP_DEVICE_INTERFACE_DETAIL_DATA    DeviceInterfaceDetailData;  
    SP_DEVICE_INTERFACE_DATA            DeviceInterfaceData;  
    SP_DEVINFO_DATA                     DeviceInfoData;  
    DWORD                               dwSize;  
  
    LogDebug(__func__);  
  
    Guid = (LPGUID)&GUID_DEVINTERFACE_USB_HUB;  
  
    DeviceInfo = SetupDiGetClassDevs(Guid, NULL, NULL,  
        DIGCF_PRESENT | DIGCF_DEVICEINTERFACE);  
  
    if (DeviceInfo == INVALID_HANDLE_VALUE)  
        return;  
  
    DeviceInterfaceDetailData = (PSP_DEVICE_INTERFACE_DETAIL_DATA)Buf;  
    DeviceInterfaceData.cbSize = sizeof(DeviceInterfaceData);  
  
    while (TRUE) {  
        Result = SetupDiEnumDeviceInterfaces(DeviceInfo, NULL,  
            Guid, Index, &DeviceInterfaceData);  
        if (!Result)  
            break;  
  
        dwSize = 0;  
        SetupDiGetDeviceInterfaceDetail(DeviceInfo,  
            &DeviceInterfaceData, NULL, 0, &dwSize, NULL);  
  
        if (dwSize != 0 && dwSize <= sizeof(Buf)) {  
            DeviceInterfaceDetailData->cbSize = sizeof(*DeviceInterfaceDetailData); // 5 Bytes!  
  
            ZeroMemory((PVOID)&DeviceInfoData, sizeof(DeviceInfoData));  
            DeviceInfoData.cbSize = sizeof(DeviceInfoData);  
  
            Result =  
                SetupDiGetDeviceInterfaceDetail(DeviceInfo, &  
                    DeviceInterfaceData, DeviceInterfaceDetailData,  
                    dwSize, &dwSize,  
                    &DeviceInfoData);  
            LogDebug("Path: %ls", DeviceInterfaceDetailData->DevicePath);  
        }  
        Index++;  
    }  
  
    SetupDiDestroyDeviceInfoList(DeviceInfo);  
    return;  
}  
Windows API - Win32
Windows API - Win32
A core set of Windows application programming interfaces (APIs) for desktop and server applications. Previously known as Win32 API.
2,689 questions
Windows 11
Windows 11
A Microsoft operating system designed for productivity, creativity, and ease of use.
10,188 questions
{count} vote

2 answers

Sort by: Most helpful
  1. Castorix31 86,316 Reputation points
    2022-11-19T08:07:32.713+00:00

    I don't have GUID_DEVINTERFACE_USB4_ROOT_DRD on Windows 10, but I usually do loops like this for any Guid :
    (link because I cannot post code here) SetupDiGetDeviceInterfaceDetail

    2 people found this answer helpful.

  2. Xiaopo Yang - MSFT 12,726 Reputation points Microsoft Vendor
    2022-11-21T02:22:22.41+00:00

    I also cannot reproduce using GUID_DEVINTERFACE_USB_HUB on Windows 11 (OS Build 22621.819) because GUID_DEVINTERFACE_USB4_ROOT_DRD is not available.
    Following is the code.

    #include <Windows.h>  
    #include <SetupAPI.h>  
    //#include <initguid.h>  
    //#include <Usbiodef.h>  
    const GUID GUID_DEVINTERFACE_USB_HUB{ 0xf18a0e88, 0xc30c, 0x11d0, 0x88, 0x15, 0x00, 0xa0, 0xc9, 0x06, 0xbe, 0xd8 };  
      
    #pragma comment(lib, "SetupAPI.lib")  
    void EnumerateUsb4HostCtrl()  
    {  
        LPGUID                              Guid = nullptr;  
        HDEVINFO                            DeviceInfo = nullptr;  
        DWORD                               Index = 0;  
        BOOL                                Result = FALSE;  
        BYTE                                Buf[512]{};  
        PSP_DEVICE_INTERFACE_DETAIL_DATA    DeviceInterfaceDetailData;  
        SP_DEVICE_INTERFACE_DATA            DeviceInterfaceData;  
        SP_DEVINFO_DATA                     DeviceInfoData;  
        DWORD                               dwSize;  
      
       //LogDebug(__func__);  
      
       Guid = (LPGUID)&GUID_DEVINTERFACE_USB_HUB;  
      
       DeviceInfo = SetupDiGetClassDevs(Guid, NULL, NULL,  
            DIGCF_PRESENT | DIGCF_DEVICEINTERFACE);  
      
       if (DeviceInfo == INVALID_HANDLE_VALUE)  
            return;  
      
       DeviceInterfaceDetailData = (PSP_DEVICE_INTERFACE_DETAIL_DATA)Buf;  
        DeviceInterfaceData.cbSize = sizeof(DeviceInterfaceData);  
      
       while (TRUE) {  
            Result = SetupDiEnumDeviceInterfaces(DeviceInfo, NULL,  
                Guid, Index, &DeviceInterfaceData);  
            if (!Result)  
                break;  
      
           dwSize = 0;  
            SetupDiGetDeviceInterfaceDetail(DeviceInfo,  
                &DeviceInterfaceData, NULL, 0, &dwSize, NULL);  
      
           if (dwSize != 0 && dwSize <= sizeof(Buf)) {  
                DeviceInterfaceDetailData->cbSize = sizeof(*DeviceInterfaceDetailData); // 5 Bytes!  
      
               ZeroMemory((PVOID)&DeviceInfoData, sizeof(DeviceInfoData));  
                DeviceInfoData.cbSize = sizeof(DeviceInfoData);  
      
               Result =  
                    SetupDiGetDeviceInterfaceDetail(DeviceInfo, &  
                        DeviceInterfaceData, DeviceInterfaceDetailData,  
                        dwSize, &dwSize,  
                        &DeviceInfoData);  
                //LogDebug("Path: %ls", DeviceInterfaceDetailData->DevicePath);  
            }  
            Index++;  
        }  
      
       SetupDiDestroyDeviceInfoList(DeviceInfo);  
        return;  
    }  
      
    int main()  
    {  
        EnumerateUsb4HostCtrl();  
        return 0;  
    }  
    

    262391-microsoftteams-image-6.png

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.