VC2010++读取CPU温度的代码
时间: 2024-03-15 14:44:15 浏览: 16
以下是使用VC++ 2010读取CPU温度的示例代码:
```c++
#include <Windows.h>
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
DWORD BufSize = MAX_PATH;
char Buffer[MAX_PATH];
// 获取CPU温度的WMI类路径
GetSystemDirectory(Buffer, BufSize);
strcat_s(Buffer, "\\wbem\\");
SetEnvironmentVariable("PATH", Buffer);
strcat_s(Buffer, "WMIC.exe CPU Get Temperature /Value");
// 执行WMIC命令并获取输出
FILE* pipe = _popen(Buffer, "r");
if (!pipe)
{
cout << "无法执行WMIC命令!" << endl;
return 1;
}
char temp[128];
while (!feof(pipe))
{
if (fgets(temp, 128, pipe))
{
cout << temp;
}
}
_pclose(pipe);
return 0;
}
```
请注意,此代码仅适用于Windows系统,并且需要管理员权限才能读取CPU温度。此外,某些计算机可能无法通过WMI获取CPU温度,因此此代码可能不适用于所有计算机。