main.cpp:48:14: error: no matching function for call to 'PdhGetFormattedCounterValue' pdh.h:408:1: note: candidate function not viable: no known conversion from 'DWORD *' (aka 'unsigned long *') to 'PPDH_FMT_COUNTERVALUE' (aka '_PDH_FMT_COUNTERVALUE *') for 4th argument
时间: 2023-12-22 11:03:06 浏览: 121
这个错误是因为第四个参数应该是一个指向 PDH_FMT_COUNTERVALUE 结构体的指针,但您传递了一个 DWORD 类型的指针。您需要修改代码,将第四个参数改为一个指向 PDH_FMT_COUNTERVALUE 结构体的指针,同时修改第五个参数为 PDH_FMT_LARGE,以获取大整数格式的计数器值。
修改后的代码如下:
```C++
#include <iostream>
#include <pdh.h>
#pragma comment(lib, "pdh.lib")
int main()
{
PDH_STATUS status;
HQUERY hQuery;
HCOUNTER hCounter;
PDH_FMT_COUNTERVALUE counterValue;
// 打开查询
status = PdhOpenQuery(NULL, 0, &hQuery);
if (status != ERROR_SUCCESS) {
std::cout << "打开查询失败:" << status << std::endl;
return 1;
}
// 添加计数器
status = PdhAddCounter(hQuery, L"\\PhysicalDisk(_Total)\\Disk Read Bytes/sec", 0, &hCounter);
if (status != ERROR_SUCCESS) {
std::cout << "添加计数器失败:" << status << std::endl;
PdhCloseQuery(hQuery);
return 1;
}
// 查询计数器值
status = PdhCollectQueryData(hQuery);
if (status != ERROR_SUCCESS) {
std::cout << "查询计数器值失败:" << status << std::endl;
PdhCloseQuery(hQuery);
return 1;
}
// 获取计数器值
status = PdhGetFormattedCounterValue(hCounter, PDH_FMT_LARGE, NULL, &counterValue);
if (status != ERROR_SUCCESS) {
std::cout << "获取计数器值失败:" << status << std::endl;
PdhCloseQuery(hQuery);
return 1;
}
// 输出计数器值
std::cout << "磁盘读速度:" << counterValue.largeValue << " 字节/秒" << std::endl;
// 关闭查询
PdhCloseQuery(hQuery);
return 0;
}
```
这里使用 PDH_FMT_LARGE 格式获取计数器值,可以获取到大整数格式的计数器值。
阅读全文