vs2013 获取cpu温度
環境: window7 64位 cpu Intel i3-4170
一、程序運行如下:
i3-4170是雙核四線程。我暫時沒有找出區分線程數跟核心數的區別。這個問題以后再去解決吧
二、編程前準備 ( 可以直接跳到三、 處,源碼中已經有了)
獲得cpu溫度的方法是讀取cpu寄存器。由于windows的保護機制,無法直接使用匯編程序讀取寄存器,因此使用了WinRing0庫。
下載鏈接如下:
http://download.csdn.net/detail/xiufeng_wang/1140396
需要用到的文件如下:
WinRing0.dll
WinRing0.sys
WinRing0x64.dll
WinRing0x64.sys
WinRing0.lib
OlsApi.h
其中WinRing0.lib、OlsApi.h放在編譯文件處,其他的需要放在.exe文件夾下。
三、源碼如下:
http://download.csdn.net/detail/jiaowosongsong/9496571
四、源碼介紹
先來看看主函數部分(readPcInfo.cpp)
好吧主函數基本什么都沒有,還是跳過吧。
運用WinRing0獲得cpu溫度部分:
1.ReadCpuInfo.h
主要就是加載了WinRing0.lib,函數1和函數2 完成了整個流程;
2.ReadCpuInfo.cpp 函數實現
#include "stdafx.h" #include "ReadCpuInfo.h" #include <string.h>ReadCpuInfo::ReadCpuInfo() {ReadCpuInfoInit(); //構造函數初始化dllstrcpy_s(fileName, "CpuInfoRecord.txt"); }ReadCpuInfo::~ReadCpuInfo() {ReadCpuInfoExit(); }void ReadCpuInfo::ReadCpuTemp() //主要函數 {for (size_t i = 0; i < coreNumber; i++) //遍歷CPU{int mask = 0x01 << i; SetProcessAffinityMask(GetCurrentProcess(), mask);//設置當前使用線程(CPU)DWORD eax, ebx, ecx, edx;Rdmsr(0x19c, &eax, &edx); //讀取溫度寄存器(eax&0x7f0000可以獲得溫度數據)cpuTemp[i] = Tjmax - ((eax & 0x7f0000) >> 16);//實際溫度= Tjmax - 溫度數據}}void ReadCpuInfo::ReadCpuInfoInit() //初始化 {if (true == CheckAndPrint("Init DLL function", InitializeOls())) //dll的初始化{if (true == CheckAndPrint("IsCpuid function", IsCpuid())) //判斷是否支持Cpuid{if (true == CheckAndPrint("Ismsr function", IsMsr())) //是否支持Rdmsr{DWORD eax, ebx, ecx, edx;Rdmsr(0x1A2, &eax, &edx); //讀取寄存器Tjmax = (eax & 0xff0000) >> 16; //獲得Tjmax//printf("%d\n", Tjmax); //show max temperatureSYSTEM_INFO sysInfo;GetSystemInfo(&sysInfo);coreNumber = sysInfo.dwNumberOfProcessors; //獲得線程數SetProcessAffinityMask(GetCurrentProcess(), 1); //切換到第一個cpu;}}} }void ReadCpuInfo::ReadCpuInfoExit() {DeinitializeOls(); } int ReadCpuInfo::CheckAndPrint(char message[], BOOL flag) {if (flag){printf("%s is checked Enable!\n", message);return 1;}else{printf("%s is checked unEnabled!\n", message);return 0;} }void ReadCpuInfo::CpuTempDisplay() {for (size_t i = 0; i < coreNumber; i++){char buf[1024] = ""; #if (CONSOLE_PRINT)printf("Core #%d: %dC\n", i, cpuTemp[i]); #else #endifsprintf_s(buf, "Core #%d: %dC\n", i, cpuTemp[i]);RecordCpuInfo(buf);} } void ReadCpuInfo::SysTimeDisplay() {clearRecordFile();SYSTEMTIME start; //windows.h中 GetLocalTime(&start);//time.h的tm結構體一樣的效果 //printf("%d\n", start.wHour); #if (CONSOLE_PRINT)printf("date: %02d/%02d/%02d\n",start.wYear, start.wMonth, start.wDay);printf("time: %02d:%02d:%02d\n",start.wHour, start.wMinute, start.wSecond); #else #endifchar buf[1024] = "";sprintf_s(buf, "date: %02d/%02d/%02d\n", start.wYear, start.wMonth, start.wDay);RecordCpuInfo(buf);memset(buf, 0, sizeof(buf));sprintf_s(buf, "time: %02d:%02d:%02d\n",start.wHour, start.wMinute, start.wSecond);RecordCpuInfo(buf);}void ReadCpuInfo::RecordCpuInfo(char *buf) {FILE *fp;fopen_s(&fp,fileName, "a+");if (fp == NULL){printf("file open error!\n");return;}else{fwrite(buf, strlen(buf), 1, fp);}fclose(fp);}void ReadCpuInfo:: clearRecordFile() {FILE *fp;fopen_s(&fp,fileName, "w+");if (fp == NULL){printf("file open error!\n");return ;}fclose(fp); }大家看完后估計還是一頭霧水,更詳細的原理可以看看其他博客:
http://blog.csdn.net/zhangweilst/article/details/43857371
http://www.tuicool.com/articles/EF7rii
總結
以上是生活随笔為你收集整理的vs2013 获取cpu温度的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: CureIt! 简单Repack(去广告
- 下一篇: [Unity] 战斗系统学习 13:Sw