以下是使用WinRing0获取CPU温度和风扇转速的代码示例:

#include <iostream>
#include <Windows.h>
#include "WinRing0/OlsApiInit.h"
#include "WinRing0/OlsDef.h"

int main()
{
    // 初始化WinRing0
    if (!OlsInit())
    {
        std::cout << "Failed to initialize WinRing0." << std::endl;
        return 1;
    }

    // 获取CPU温度和风扇转速
    DWORD dwNumberOfProcessors = 0;
    if (!OlsGetNumberOfProcessors(&dwNumberOfProcessors))
    {
        std::cout << "Failed to get number of processors." << std::endl;
        return 1;
    }

    for (DWORD i = 0; i < dwNumberOfProcessors; i++)
    {
        DWORD dwTemperature = 0;
        DWORD dwFanSpeed = 0;

        if (OlsReadIoPortByte(i, 0x2E, &dwTemperature) && OlsReadIoPortByte(i, 0x2F, &dwFanSpeed))
        {
            std::cout << "CPU " << i << " Temperature: " << dwTemperature << "°C" << std::endl;
            std::cout << "CPU " << i << " Fan Speed: " << dwFanSpeed << " RPM" << std::endl;
        }
        else
        {
            std::cout << "Failed to read CPU " << i << " temperature and fan speed." << std::endl;
        }
    }

    // 清理WinRing0
    OlsCleanup();

    return 0;
}

请注意,此代码使用了WinRing0库,您需要将其正确地添加到您的项目中,并在编译时包含相应的头文件和库文件。您可以从WinRing0的官方网站或第三方资源中获取该库。

标签: 教育


原文地址: https://cveoy.top/t/topic/hFk7 著作权归作者所有。请勿转载和采集!