Why is SetupDiGetDeviceProperty function not working?

半城伤御伤魂 提交于 2019-12-14 03:17:43

问题


I'm trying to use the SetupDiGetDeviceProperty, but apparently it couldn't find such functions within the setupapi.h. I have looked at the documentation and included all the header and library files, but it's just not letting me use the function... What is going? What is it that I'm doing wrong? Heres the code:

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

the SetupDiGetDeviceProperty function is called at the bottom of the ListAllDevices function.

Thanks

Edit: sorry, forgot to state the error: IntelliSense: identifier "SetupDiGetDeviceProperty" is undefined


回答1:


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?



来源:https://stackoverflow.com/questions/10574137/why-is-setupdigetdeviceproperty-function-not-working

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!