你这个程序的头文件没写全啊
时间: 2024-03-19 08:44:40 浏览: 25
非常抱歉,是我疏忽了。以下是需要包含的头文件:
```c++
#include <iostream>
#include <Windows.h>
#include <Pdh.h>
#pragma comment(lib, "Pdh.lib")
```
其中,`Windows.h` 是 Windows API 的头文件,`<iostream>` 是 C++ 标准输入输出流的头文件,`<Pdh.h>` 是性能数据助手 API 的头文件,需要使用 `Pdh.lib` 库,所以在程序中添加了 `#pragma comment(lib, "Pdh.lib")`,以保证能够链接该库。
另外,在使用 `Pdh.h` 中的函数时,需要使用 `PdhAddCounter`、`PdhCollectQueryData`、`PdhOpenQuery`、`PdhGetFormattedCounterValue` 等函数,具体使用方法可参考以下示例代码:
```c++
PDH_STATUS status;
PDH_HQUERY hQuery;
PDH_HCOUNTER hCounter;
DWORD dwCounterType;
PDH_FMT_COUNTERVALUE counterVal;
status = PdhOpenQuery(NULL, NULL, &hQuery);
if (status != ERROR_SUCCESS) {
std::cerr << "PdhOpenQuery failed with error code " << status << std::endl;
return 1;
}
status = PdhAddCounter(hQuery, L"\\Processor(_Total)\\% Processor Time", NULL, &hCounter);
if (status != ERROR_SUCCESS) {
std::cerr << "PdhAddCounter failed with error code " << status << std::endl;
PdhCloseQuery(hQuery);
return 1;
}
status = PdhCollectQueryData(hQuery);
if (status != ERROR_SUCCESS) {
std::cerr << "PdhCollectQueryData failed with error code " << status << std::endl;
PdhCloseQuery(hQuery);
return 1;
}
status = PdhGetFormattedCounterValue(hCounter, PDH_FMT_DOUBLE, &dwCounterType, &counterVal);
if (status != ERROR_SUCCESS) {
std::cerr << "PdhGetFormattedCounterValue failed with error code " << status << std::endl;
PdhCloseQuery(hQuery);
return 1;
}
std::cout << "CPU Usage: " << counterVal.doubleValue << "%" << std::endl;
PdhCloseQuery(hQuery);
```
以上代码可以获取系统的 CPU 占用率,并输出到标准输出流中。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)