SetupDiGetDeviceProperty

SetupDiGetDeviceProperty

我正在尝试使用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++;
    }
}


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

谢谢

编辑:抱歉,忘记声明错误:IntelliSense:标识符“ SetupDiGetDeviceProperty”未定义

最佳答案

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

#define WINVER 0x0600
#define _WIN32_WINNT 0x0600


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

或者,您可以在项目选项或命令行中定义它们。更多详细信息here

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

关于c++ - 为什么SetupDiGetDeviceProperty函数不起作用?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/10574137/

10-09 19:55