0

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

谢谢

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

4

1 回答 1

2

SetupDiGetDeviceProperty requires Vista or later, as described in the documentation. You must therefore defined WINVER and _WIN32_WINNT accordingly.

#define WINVER 0x0600
#define _WIN32_WINNT 0x0600

My guess is that your project targets an earlier version of Windows.

Alternatively you can define them in the project options, or on the command line. More details here.

If that is not the answer then is it possible that you are using an out-of-date version of the SDK that pre-dates Vista?

于 2012-05-13T18:33:40.033 回答