为什么SetupDiGetDeviceProperty功能不起作用?

时间:2012-05-13 18:24:32

标签: c++ device wdk

我正在尝试使用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函数。

由于

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

1 个答案:

答案 0 :(得分:2)

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

#define WINVER 0x0600
#define _WIN32_WINNT 0x0600

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

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

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