为什么SetupDiGetDeviceProperty函数不工作? [英] Why is SetupDiGetDeviceProperty function not working?

查看:2270
本文介绍了为什么SetupDiGetDeviceProperty函数不工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用SetupDiGetDeviceProperty,但显然它不能在setupapi.h中找到这样的函数。我看了文档,并包括所有的标题和库文件,但它只是不让我使用的功能...什么是?这是什么,我做错了?代码如下:

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

SetupDiGetDeviceProperty函数在ListAllDevices

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

感谢

编辑:对不起,忘记说错误:IntelliSense:identifierSetupDiGetDevicePropertyis未定义

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

推荐答案

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

#define WINVER 0x0600
#define _WIN32_WINNT 0x0600

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

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.

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

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?

这篇关于为什么SetupDiGetDeviceProperty函数不工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆