为什么 SetupDiGetDeviceProperty 函数不起作用
Why is SetupDiGetDeviceProperty function not working?
我正在尝试使用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 函数的底部调用。
谢谢
编辑:对不起,忘记说明错误:智能感知:标识符"SetupDiGetDeviceProperty"未定义
SetupDiGetDeviceProperty
需要 Vista 或更高版本,如文档中所述。因此,您必须相应地定义WINVER
和_WIN32_WINNT
。
#define WINVER 0x0600
#define _WIN32_WINNT 0x0600
我的猜测是你的项目针对的是早期版本的Windows。
或者,您可以在项目选项中或在命令行上定义它们。更多细节在这里。
如果这不是答案,那么您是否可能使用的是早于 Vista 的过时版本的 SDK?
相关文章:
- C++为什么尽管我调用了void函数,它却不起作用
- 仅包含可移动 std::map 的类的移动构造函数不起作用
- 通过构造函数创建一些值并尝试添加到文档中使用 rapidjson 不起作用
- 为什么我的数组双精度函数不起作用?
- 使用 va_arg 传递给函数va_list不起作用
- Windows.h 输入在函数之外不起作用
- 类和构造函数中的函数根本不起作用,并且不返回任何错误
- 类中的虚拟布尔函数参数不起作用
- getline 函数似乎在 C++ 中不起作用
- 将新放置与 std::函数一起使用不起作用
- binary_search() 函数在我的函数体中不起作用
- 为什么读取函数在 sha1 c++ 实现中不起作用?
- 在函数中声明的 ifStream 对象在发布模式下不起作用
- 指针变量在 cout 函数中不起作用
- std::函数不起作用,但普通的旧函数指针可以 - 为什么?
- C++ 函数,用于查找数组中四个最小最大元素的总和不起作用
- 为什么必须使用'*p=a',而使用'p=&a'在构造函数中不起作用
- C++ Arduino - 随机函数不起作用
- QObject::连接不起作用 - 使用函数语法找不到信号
- 函数在类中不起作用-函数调用缺少参数列表