SetupDiGetDeviceInterfaceDetail

SetupDiGetDeviceInterfaceDetail

我正在尝试使用SetupDiGetDeviceInterfaceDetail()函数获取设备的路径,但是每次调用它都会崩溃。我已经为此工作了12多个小时,但仍然无法找出问题所在……有人可以看看他们是否可以找到真正导致此情况发生的原因吗?这是代码:

//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;
}


在DeviceManager的GetDeviceHandler()函数中调用SetupDiGetDeviceInterfaceDetail函数。

请帮忙。谢谢。

更新:我发现它在第一个SetupDiGetDeviceInterfaceDetail上失败,并返回122错误(ERROR_INSUFFICIENT_BUFFER)。但是我只是试图获得所需的缓冲区大小,那怎么可能呢?

更新2:是的,通过将deviceInterfaceData.cbsize设置为2048(用于测试的巨大空间),我对函数进行了一些更改(请参见上面的代码),现在我得到了ERROR_INVALID_PARAMETER。这越来越令人困惑...我所给的参数如何无效?只是没有意义。唯一的区别是我传递了引用而不是指针,因为否则我将收到访问冲突错误...

最佳答案

您没有为SP_DEVICE_INTERFACE_DETAIL_DATA正确分配内存。

删除SP_DEVICE_INTERFACE_DETAIL_DATA deviceInterfaceDetailedData;并尝试将其放入您的if块中:

    // 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);


我没有看过其余的代码,它也可能有错误,但这可以回答您的原始问题。

07-25 20:36