为什么 SetupDiGetDeviceProperty 函数不起作用

Why is SetupDiGetDeviceProperty function not working?

本文关键字:不起作用 函数 SetupDiGetDeviceProperty 为什么      更新时间:2023-10-16

我正在尝试使用SetupDiGetDeviceProperty,但显然它在setupapi.h中找不到这样的函数。我已经查看了文档并包含了所有的头文件和库文件,但它只是不让我使用该功能......这是怎么回事?我做错了什么?这是代码:

//Mainframe.cpp file
#include"DeviceManager.h"
int main()
{
    int iQuit;
    DeviceManager deviceManager;
    deviceManager.ListAllDevices();
    std::cin >> iQuit;
    return 0;
}
//DeviceManager.h file
#include <windows.h>
#include <setupapi.h>
#include <iostream>
#include <cfgmgr32.h>
#include <tchar.h>
#include <devpkey.h>
//#pragma comment (lib, "setupapi.lib")
class DeviceManager
{
public:
    DeviceManager();
    ~DeviceManager();
    void ListAllDevices();
};
//DeviceManager.cpp file
#include"DeviceManager.h"
DeviceManager::DeviceManager()
{
}
DeviceManager::~DeviceManager()
{
}
void DeviceManager::ListAllDevices()
{
    HDEVINFO deviceInfoSet;             //A list of all the devices
    SP_DEVINFO_DATA deviceInfoData;     //A device from deviceInfoSet
    DEVPROPTYPE devicePropertyType;
    //CONFIGRET device;
    DWORD deviceIndex = 0;
    DWORD size;
    TCHAR description[1024];
    bool foundAllDevices = false;
    deviceInfoSet = SetupDiGetClassDevs(NULL, TEXT("USB"), NULL, DIGCF_PRESENT|DIGCF_ALLCLASSES); //Gets all Devices
    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.
        SetupDiGetDeviceProperty(deviceInfoSet, deviceInfoData, DEVPKEY_NAME, devicePropertyType, description, sizeof(description), size, 0);
        std::cout << deviceIDbuffer << std::endl;
        deviceIndex++;
    }
}

SetupDiGetDeviceProperty 函数在 ListAllDevices 函数的底部调用。

谢谢

编辑:对不起,忘记说明错误:智能感知:标识符"SetupDiGetDeviceProperty"未定义

SetupDiGetDeviceProperty需要 Vista 或更高版本,如文档中所述。因此,您必须相应地定义WINVER_WIN32_WINNT

#define WINVER 0x0600
#define _WIN32_WINNT 0x0600

我的猜测是你的项目针对的是早期版本的Windows。

或者,您可以在项目选项中或在命令行上定义它们。更多细节在这里。

如果这不是答案,那么您是否可能使用的是早于 Vista 的过时版本的 SDK?