SetupDiGetDeviceInstanceId Run time Error (Access Violation)

checkingrandom 206 Reputation points
2022-09-05T11:41:38.423+00:00

Im Trying to get the Instance ID of USB Device using the SetupDi API using the c++.

So for this I used SetupDiGetClassDevs -> SetupDiEnumDeviceInfo -> SetupDiGetDeviceInstanceId to get the instance ID.

but while debugging I'm getting the error as below

Exception thrown at 0x761EA543 (setupapi.dll) in setupDi_example.exe: 0xC0000005: Access violation reading location 0xCCCCCCCC.

ZeroMemory(&devInfoData, sizeof(devInfoData));  
            devInfoData.cbSize = sizeof(devInfoData);  
            pDevDetail = (PSP_DEVINFO_DATA)buffer;  
            pDevDetail->cbSize = sizeof(SP_DEVINFO_DATA);  
            PTSTR buf = NULL;  
            DWORD bufSize = 0;  
            DWORD reqSize = 0;  
              
            if (!SetupDiGetDeviceInstanceId(hDevInfo, pDevDetail, buf, bufSize, &reqSize))  
            {  
                if (bufSize < reqSize)  
                {  
                    if (NULL != buf)  
                    {  
                        delete[] buf;  
                        buf = NULL;  
                        bufSize = 0;  
                    }  
                    buf = new TCHAR[reqSize];  
                    if (NULL == buf)  
                    {  
                        cout << "Insufficient memory" << endl;  
                        break;  
                    }  
                    bufSize = reqSize;  
                    if (!SetupDiGetDeviceInstanceId(hDevInfo, pDevDetail, buf, bufSize, &reqSize))  
                        cout << "Error in SetUpDiGetDeviceInstanceID " << GetLastError() << endl;  
                    else  
                        cout <<"Instance ID "<< buf << endl;  
                }  
                else  
                {  
                    cout << "Error in SetUpDiGetDeviceInstanceID " << GetLastError() << endl;  
                    break;  
                }  
            }  
  
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,448 questions
C++
C++
A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.
3,561 questions
{count} votes

Accepted answer
  1. Castorix31 82,036 Reputation points
    2022-09-05T12:39:21.16+00:00

    This test works on my PC (Windows 10 21H1, VS 2022) =>

                    SP_DEVINFO_DATA did;  
                    DWORD nIndex = 0;  
                    HDEVINFO hDI = SetupDiGetClassDevs(&GUID_DEVINTERFACE_USB_DEVICE, NULL, NULL, DIGCF_DEVICEINTERFACE | DIGCF_PRESENT);  
                    LPTSTR pwszInstanceId = (LPTSTR)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, (MAX_DEVICE_ID_LEN * sizeof(WCHAR)));  
                    if ((NULL != pwszInstanceId) && (INVALID_HANDLE_VALUE != hDI))  
                    {  
                        while (did.cbSize = sizeof(SP_DEVINFO_DATA), SetupDiEnumDeviceInfo(hDI, nIndex, &did))  
                        {  
                            bool bSuccess = SetupDiGetDeviceInstanceId(hDI, &did, pwszInstanceId, MAX_DEVICE_ID_LEN, NULL);  
                            if (bSuccess)  
                            {  
                                // Code...  
                            }  
                            nIndex++;  
                        }  
                        SetupDiDestroyDeviceInfoList(hDI);  
                    }  
                    if (pwszInstanceId)  
                        HeapFree(GetProcessHeap(), 0, pwszInstanceId);
    
    1 person found this answer helpful.
    0 comments No comments

0 additional answers

Sort by: Most helpful