win系统cpu温度获取

win 系统 信息 专栏收录该内容
1 篇文章 0 订阅

基于win 系统的cpu温度获取,用底层驱动来读取cpu温度寄存器的数值,注意,一般网上用到的WMI方式是无法获取cpu温度等信息的,这涉及到win系统的权限,而且,WMI是从BIOS主板上来读取数据的,基本上生产商不会把cpu温度,显卡温度等信息写入BIOS 中,而且写入的还是一个固定值,也就是说,cpu温度不会变,本人也尝试了很多方法,最后得出结论,只用通过底层去驱动才能时刻读取cpu温度的准确信息,现将代码和,文件直接贴出来以供大家学习和研究,,注意本文是在VS上编辑运行的 需要自己下载WinRing0.sys 。WinRing0x64.sys。WinRing0x64.dll 。WinRing0x64.dll 系统权限访问库

文件:Ring0.h

#pragma once

#include<Windows.h>
#include <tchar.h>
#include<stdio.h>

/接口定义/

//Ring0Driver.cpp
BOOL Start();
VOID End();

//Ring0Apis.cpp
BOOL WINAPI Rdmsr(DWORD index, PDWORD eax, PDWORD edx);
BOOL WINAPI RdmsrTx(DWORD index, PDWORD eax, PDWORD edx, DWORD_PTR threadAffinityMask);
BOOL WINAPI RdmsrPx(DWORD index, PDWORD eax, PDWORD edx, DWORD_PTR processAffinityMask);

文件: Ring0.cpp
// Ring0.cpp : 定义控制台应用程序的入口点。
//

#include"Ring0.h"

/*
读取基准温度 IA32_TEMPERATURE_TARGET
Rdmsr(IA32_TEMPERATURE_TARGET, &eax, &edx);
int Target = (eax >> 16) & 0xFF;
读取核心温度 IA32_THERM_STATUS_MSR
Rdmsr(IA32_THERM_STATUS_MSR, &eax, &edx, nCore);
调校温度差
int deltaT = 100;
if ((eax & 0x80000000) != 0)deltaT = ((eax & 0x007F0000) >> 16);
//实时温度值
int coreTemperatures = Target - deltaT;
*/
const UINT IA32_THERM_STATUS_MSR = 0x019C;
const UINT IA32_TEMPERATURE_TARGET = 0x01A2;
const UINT IA32_PACKAGE_THERM_STATUS = 0x1B1;

inline UINT GetCoreTemperature(UINT nCore)
{
DWORD eax = 0, edx = 0, Target = 0;
Rdmsr(IA32_TEMPERATURE_TARGET, &eax, &edx);
Target = (eax >> 16) & 0xFF;
RdmsrPx(IA32_THERM_STATUS_MSR, &eax, &edx, nCore);
return (eax & 0x80000000) ? Target - ((eax & 0x007F0000) >> 16) : Target;
}

BOOL __stdcall Handler(DWORD ctrlType)
{
switch (ctrlType)
{
case CTRL_CLOSE_EVENT:End(); break;
default:
break;
}
return TRUE;
}
int _tmain(int argc, _TCHAR* argv[])
{
SetConsoleCtrlHandler(Handler, TRUE);
Start();
for (int i = 0; i < 100; ++i)
{
printf_s(“Core1: %d\nCore2: %d”, GetCoreTemperature(1), GetCoreTemperature(2));
Sleep(500);
system(“cls”);
}
End();
system(“pause”);
return 0;
}

文件 :Ring0Driver.cpp

#include"Ring0.h"

/管理驱动/
//安装驱动
BOOL InstallDriver(LPCTSTR DriverId, LPCTSTR DriverPath)
{
SC_HANDLE hSCManager = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
SC_HANDLE hService = CreateService(hSCManager,
DriverId, DriverId,
SERVICE_ALL_ACCESS,
SERVICE_KERNEL_DRIVER,
SERVICE_DEMAND_START,
SERVICE_ERROR_NORMAL,
DriverPath,
0,0,0,0,0);
CloseServiceHandle(hSCManager);
if (NULL == hService)
if (ERROR_SERVICE_EXISTS != GetLastError())return FALSE;
CloseServiceHandle(hService);
return TRUE;
}
//卸载驱动
BOOL RemoveDriver(LPCTSTR DriverId)
{
SC_HANDLE hSCManager = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
SC_HANDLE hService = OpenService(hSCManager, DriverId, SERVICE_ALL_ACCESS);
if (NULL == hService)return TRUE;
BOOL bCode = DeleteService(hService);
CloseServiceHandle(hService);
CloseServiceHandle(hSCManager);
return bCode;
}
//启动驱动
BOOL StartDriver(LPCTSTR DriverId)
{
SC_HANDLE hSCManager = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
SC_HANDLE hService = OpenService(hSCManager, DriverId, SERVICE_ALL_ACCESS);
if (0 == hService) return FALSE;
if (!StartService(hService, 0, NULL))
if (ERROR_SERVICE_ALREADY_RUNNING != GetLastError())
{
CloseServiceHandle(hService);
return FALSE;
}
CloseServiceHandle(hService);
return TRUE;
}
//停止驱动
BOOL StopDriver(LPCTSTR DriverId)
{
SC_HANDLE hSCManager = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
SC_HANDLE hService = OpenService(hSCManager, DriverId, SERVICE_ALL_ACCESS);
if (0 == hService)return FALSE;
SERVICE_STATUS serviceStatus;
BOOL bCode = ControlService(hService, SERVICE_CONTROL_STOP, &serviceStatus);
CloseServiceHandle(hService);
return bCode;
}
//打开驱动获取设备句柄
BOOL OpenDriver(LPHANDLE lpDriver)
{
if (0 == lpDriver)return FALSE;
*lpDriver = CreateFile(
_T("\\.\WinRing0_1_2_0"),
GENERIC_READ | GENERIC_WRITE,
0,0,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
0);
return (INVALID_HANDLE_VALUE == *lpDriver) ? FALSE : TRUE;
}
/下面是主要接口/

//驱动服务ID和显示名称
LPCTSTR DRIVER_ID = _T(“WinRing0_1_2_0”);
//设备句柄
HANDLE hDriver = INVALID_HANDLE_VALUE;

//驱动版本兼顾32系统位和64位系统
LPCTSTR DRIVER_NAME = _T(“WinRing0x64.sys”);
VOID IsWow64()
{
typedef BOOL(WINAPI *LPFN_ISWOW64PROCESS) (HANDLE, PBOOL);
LPFN_ISWOW64PROCESS fnIsWow64Process;
BOOL bIsWow64 = FALSE;
fnIsWow64Process = (LPFN_ISWOW64PROCESS)GetProcAddress(GetModuleHandle(_T(“kernel32”)), “IsWow64Process”);
if (NULL != fnIsWow64Process)
fnIsWow64Process(GetCurrentProcess(), &bIsWow64);

if (bIsWow64)DRIVER_NAME = _T("WinRing0x64.sys");
else DRIVER_NAME = _T("WinRing0.sys");

}

#include<stdio.h>

BOOL Start()
{
BOOL bResult = TRUE;
bResult = RemoveDriver(DRIVER_ID);
TCHAR lpDirectory[256];
GetCurrentDirectory(256, lpDirectory);
IsWow64();
//驱动路径 兼容Unicode和MBCS
#ifdef UNICODE
wsprintf(lpDirectory, _T("%s\%s"), lpDirectory, DRIVER_NAME);
#else
sprintf_s(lpDirectory, _T("%s\%s"), lpDirectory, DRIVER_NAME);
#endif
bResult = InstallDriver(DRIVER_ID, lpDirectory);
if (!bResult)return FALSE;
bResult = StartDriver(DRIVER_ID);
if (!bResult)return FALSE;
bResult = OpenDriver(&hDriver);
if (!bResult)return FALSE;

return TRUE;

}
//终止
VOID End()
{
CloseHandle(hDriver);
StopDriver(DRIVER_ID);
RemoveDriver(DRIVER_ID);
}

文件:Ring0Apis.cpp

#include"Ring0.h"

/设备IO/

//设备句柄
extern HANDLE hDriver;

#define OLS_TYPE 40000
#define IOCTL_OLS_READ_MSR CTL_CODE(OLS_TYPE, 0x821, METHOD_BUFFERED, FILE_ANY_ACCESS)
//读取MSR寄存器 不限制线程
BOOL WINAPI Rdmsr(DWORD index, PDWORD eax, PDWORD edx)
{
if (INVALID_HANDLE_VALUE == hDriver)return FALSE;
BYTE outBuffer[8] = { 0 };
DWORD dwReturnLength = 0;
if (DeviceIoControl(
hDriver,
IOCTL_OLS_READ_MSR,
&index,
sizeof(index),
&outBuffer,
sizeof(outBuffer),
&dwReturnLength,
0))
{
memcpy(eax, outBuffer, 4);
memcpy(edx, outBuffer + 4, 4);
return TRUE;
}
return FALSE;
}
//限制线程
BOOL WINAPI RdmsrTx(DWORD index, PDWORD eax, PDWORD edx, DWORD_PTR threadAffinityMask)
{
DWORD_PTR mask = SetThreadAffinityMask(GetCurrentThread(), threadAffinityMask);
BOOL bResult = Rdmsr(index, eax, edx);
SetThreadAffinityMask(GetCurrentThread(), mask);
return bResult;
}
//限制进程
BOOL WINAPI RdmsrPx(DWORD index, PDWORD eax, PDWORD edx, DWORD_PTR processAffinityMask)
{
DWORD_PTR mask = SetThreadAffinityMask(GetCurrentThread(), processAffinityMask);
BOOL bResult = Rdmsr(index, eax, edx);
SetThreadAffinityMask(GetCurrentThread(), mask);
return bResult;
}

  • 0
    点赞
  • 1
    评论
  • 9
    收藏
  • 一键三连
    一键三连
  • 扫一扫,分享海报

©️2021 CSDN 皮肤主题: 创作都市 设计师:CSDN官方博客 返回首页
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值