Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7986065
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T11:44:24+00:00 2026-06-04T11:44:24+00:00

I’m trying to get the path of a device by using the SetupDiGetDeviceInterfaceDetail() function,

  • 0

I’m trying to get the path of a device by using the SetupDiGetDeviceInterfaceDetail() function, but it crashes everytime I call it. I’ve have been working on this for over 12 hours but still couldn’t find out what is wrong with it… Can someone see if they can find what is actually causing this to happen? Heres the code:

//DeviceManager.h
#include <windows.h>
//#include <hidsdi.h>
#include <setupapi.h>
#include <iostream>
#include <cfgmgr32.h>
#include <tchar.h>
#include <devpkey.h>
#include <string>

extern "C"{
    #include <hidsdi.h>
}

//#pragma comment (lib, "setupapi.lib")

class DeviceManager
{
public:
    DeviceManager();
    ~DeviceManager();

    void ListAllDevices();
    void GetDeviceHandler();

    //HANDLE PSMove;
    //byte reportBuffer[57];
    GUID guid;
//private:
    HDEVINFO deviceInfoSet;             //A list of all the devices
    SP_DEVINFO_DATA deviceInfoData;     //A device from deviceInfoSet

    SP_DEVICE_INTERFACE_DATA deviceInterfaceData;
    SP_DEVICE_INTERFACE_DETAIL_DATA deviceInterfaceDetailedData;
};

//DeviceManager.cpp
#include"DeviceManager.h"

DeviceManager::DeviceManager()
{
    //deviceInterfaceData = new SP_DEVICE_INTERFACE_DATA;
    //deviceInterfaceDetailedData = new SP_DEVICE_INTERFACE_DETAIL_DATA;

    HidD_GetHidGuid(&guid);
    deviceInfoSet = SetupDiGetClassDevs(&guid, NULL, NULL, DIGCF_PRESENT|DIGCF_DEVICEINTERFACE); //Gets all Devices
    GetDeviceHandler();
}

DeviceManager::~DeviceManager()
{
}

void DeviceManager::ListAllDevices()
{
    DWORD deviceIndex = 0;

    deviceInfoData.cbSize = sizeof(deviceInfoData);

    while(SetupDiEnumDeviceInfo(deviceInfoSet, deviceIndex, &deviceInfoData))
    {
        deviceInfoData.cbSize = sizeof(deviceInfoData);

        ULONG tcharSize;
        CM_Get_Device_ID_Size(&tcharSize, deviceInfoData.DevInst, 0);
        TCHAR* deviceIDBuffer = new TCHAR[tcharSize];   //the device ID will be stored in this array, so the tcharSize needs to be big enough to hold all the info.
                                                        //Or we can use MAX_DEVICE_ID_LEN, which is 200
        CM_Get_Device_ID(deviceInfoData.DevInst, deviceIDBuffer, MAX_PATH, 0); //gets the devices ID - a long string that looks like a file path.

        std::cout << deviceIDBuffer << std::endl;

        deviceIndex++;
    }
}

void DeviceManager::GetDeviceHandler()
{
     DWORD deviceIndex = 0;

SP_DEVINFO_DATA deviceInfoData;
SP_DEVICE_INTERFACE_DATA deviceInterfaceData;
SP_DEVICE_INTERFACE_DETAIL_DATA deviceInterfaceDetailedData;

deviceInfoData.cbSize = sizeof(deviceInfoData);

while(SetupDiEnumDeviceInfo(deviceInfoSet, deviceIndex, &deviceInfoData))
{
    TCHAR deviceID[MAX_DEVICE_ID_LEN];

    CM_Get_Device_ID(deviceInfoData.DevInst, deviceID, MAX_DEVICE_ID_LEN, 0);

    //std::cout << deviceID << std::endl;

    deviceInterfaceData.cbSize = sizeof(SP_DEVICE_INTERFACE_DATA);
    if(SetupDiEnumDeviceInterfaces(deviceInfoSet, &deviceInfoData, &guid, 0, &deviceInterfaceData))
    {
        DWORD bufferLength = 0;
        //deviceInterfaceData.cbSize = sizeof(SP_DEVICE_INTERFACE_DATA);
        deviceInterfaceData.cbSize = 2048;
        //std::cout << "it works not" << std::endl;

        if(SetupDiGetDeviceInterfaceDetail(deviceInfoSet, &deviceInterfaceData, NULL, 0, &bufferLength, NULL))
        {
            //deviceInterfaceData.cbSize = sizeof(bufferLength);
            std::cout << "It works!" << std::endl;
        }
        else
        {
            std::cout << GetLastError() << std::endl;
        }

    }
    else
    {
        //std::cout << GetLastError() << std::endl;
    }


    deviceIndex++;
}

}

//mainapp.cpp
#pragma once

int main()
{
DeviceManager deviceManager;

return 0;
}

The SetupDiGetDeviceInterfaceDetail function is called in the GetDeviceHandler() function of DeviceManager.

Please help. Thanks.

UPDATE: I have found out that it failed on the first SetupDiGetDeviceInterfaceDetail and is returning a 122 error (ERROR_INSUFFICIENT_BUFFER). But I am only trying to get the required buffer size, so how can this be??

UPDATE 2: right, I have changed the function a bit (see above code) by setting the deviceInterfaceData.cbsize to 2048 (a huge space for testing purposes) and now I’m getting a ERROR_INVALID_PARAMETER. This is getting more and more confusing… How can the parameters I’ve given is invalid? Just doesn’t make sense. The only difference is I passed in References instead of Pointers because otherwise I will get a access violation error…

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T11:44:26+00:00Added an answer on June 4, 2026 at 11:44 am

    You’re not allocating memory properly for the SP_DEVICE_INTERFACE_DETAIL_DATA.

    Remove SP_DEVICE_INTERFACE_DETAIL_DATA deviceInterfaceDetailedData; and try putting this inside your if block:

        // Get the required bufferLength
        SetupDiGetDeviceInterfaceDetail(deviceInfoSet,
                                        &deviceInterfaceData,
                                        nullptr,
                                        0,
                                        &bufferLength,
                                        nullptr);
        if(GetLastError() != ERROR_INSUFFICIENT_BUFFER)
        {
            std::cout << "Failed to get bufferLength.  Error "
                      << GetLastError() << '\n';
            return;
        }
    
        // Create device interface detailed information struct pointer
        // and allocate memory to it.
        PSP_DEVICE_INTERFACE_DETAIL_DATA deviceInterfaceDetailedData(nullptr);
        deviceInterfaceDetailedData =
            static_cast<PSP_INTERFACE_DEVICE_DETAIL_DATA>(malloc(bufferLength));
        if(deviceInterfaceDetailedData == nullptr)
        {
            std::cout << "Failed to allocate memory.  Error "
                      << GetLastError() << '\n';
            return;
        }
        deviceInterfaceDetailedData->cbSize =
            sizeof(SP_INTERFACE_DEVICE_DETAIL_DATA);
    
        // Get detailed information
        if(SetupDiGetDeviceInterfaceDetail(deviceInfoSet,
                                           &deviceInterfaceData,
                                           deviceInterfaceDetailedData,
                                           bufferLength,
                                           &bufferLength,
                                           nullptr))
        {
            //deviceInterfaceData.cbSize = sizeof(bufferLength);
            std::cout << "It works!" << std::endl;
        }
        else
        {
            std::cout << GetLastError() << std::endl;
        }
    
        free(deviceInterfaceDetailedData);
    

    I haven’t looked at the rest of the code, it may have errors too, but this answers your original question.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a French site that I want to parse, but am running into
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I want to construct a data frame in an Rcpp function, but when I
I have thousands of HTML files to process using Groovy/Java and I need to
I am trying to loop through a bunch of documents I have to put
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
Basically, what I'm trying to create is a page of div tags, each has
I am trying to understand how to use SyndicationItem to display feed which is
link Im having trouble converting the html entites into html characters, (&# 8217;) i

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.