c++获取Windows的cpu温度

c或者c++怎么获取Windows的cpu温度啊,网上也找了好几个代码,但是运行后要不显示为0,要不为100,应该是驱动问题。。有没有好点的方法获取添加外部库可以得到cpu的温度(不要AI,如果要用AI可以先自己运行一下AI的代码看看能不能运行)

使用WinRing0驱动获取intel cpu 的温度代码,私信发给你

这个都是通过 wmi 接口去查询的,你先看看你的主板驱动是否装好,第三方软件(比如 aida 能不能获取温度)

Windows API函数就用WMI 来查询硬件传感器信息,三方库有 Open Hardware Monitor和CPUID SDK,也可以提供获取硬件信息的功能。
相应的文档和示例代码可以自己去这些库的官方网站上找到。
无论用哪种方式,要确保你的程序具有足够的权限访问硬件传感器信息,并且部分方法可能需要管理员权限或者专门的驱动程序支持。
具体可以阅读相关官方文档并检查代码示例是否适配你的环境和需求。

参考 https://blog.csdn.net/weixin_35757191/article/details/129505157

在Windows系统中获取温度需要通过硬件监控接口或传感器来实现,实际使用时需要根据具体硬件和传感器供应商提供的接口进行调整和修改。另外,确保已经安装了用于与传感器通信的适当驱动程序,电脑型号不一样我代码可能在你电脑上输出不一致,请问你电脑什么型号,驱动哪一个,cpu什么版本

要通过C++获取Windows系统的CPU温度,您可以使用Windows API和第三方库来实现。下面是一个使用Windows API和WMI的示例代码:

#include <windows.h>
#include <iostream>
#include <comdef.h>
#include <Wbemidl.h>
#pragma comment(lib, "wbemuuid.lib")

int main()
{
    HRESULT hres;

    // 初始化COM库
    hres = CoInitializeEx(0, COINIT_MULTITHREADED);
    if (FAILED(hres))
    {
        std::cerr << "Failed to initialize COM library" << std::endl;
        return 1;
    }

    // 启动WMI
    hres = CoInitializeSecurity(
        NULL,
        -1,
        NULL,
        NULL,
        RPC_C_AUTHN_LEVEL_DEFAULT,
        RPC_C_IMP_LEVEL_IMPERSONATE,
        NULL,
        EOAC_NONE,
        NULL);
    if (FAILED(hres))
    {
        std::cerr << "Failed to initialize security" << std::endl;
        CoUninitialize();
        return 1;
    }

    // 获取WMI服务
    IWbemLocator *pLoc = NULL;
    hres = CoCreateInstance(
        CLSID_WbemLocator,
        0,
        CLSCTX_INPROC_SERVER,
        IID_IWbemLocator,
        (LPVOID *)&pLoc);

    if (FAILED(hres))
    {
        std::cerr << "Failed to create IWbemLocator object" << std::endl;
        CoUninitialize();
        return 1;
    }

    IWbemServices *pSvc = NULL;
    hres = pLoc->ConnectServer(
        _bstr_t(L"ROOT\\WMI"),
        NULL,
        NULL,
        0,
        NULL,
        0,
        0,
        &pSvc);

    if (FAILED(hres))
    {
        std::cerr << "Failed to connect WMI server" << std::endl;
        pLoc->Release();
        CoUninitialize();
        return 1;
    }

    // 设置安全性
    hres = CoSetProxyBlanket(
        pSvc,
        RPC_C_AUTHN_WINNT,
        RPC_C_AUTHZ_NONE,
        NULL,
        RPC_C_AUTHN_LEVEL_CALL,
        RPC_C_IMP_LEVEL_IMPERSONATE,
        NULL,
        EOAC_NONE);

    if (FAILED(hres))
    {
        std::cerr << "Failed to set proxy blanket" << std::endl;
        pSvc->Release();
        pLoc->Release();
        CoUninitialize();
        return 1;
    }

    // 查询温度
    IEnumWbemClassObject *pEnumerator = NULL;
    hres = pSvc->ExecQuery(
        bstr_t("WQL"),
        bstr_t("SELECT * FROM MSAcpi_ThermalZoneTemperature"),
        WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY,
        NULL,
        &pEnumerator);

    if (FAILED(hres))
    {
        std::cerr << "Failed to execute WQL query" << std::endl;
        pSvc->Release();
        pLoc->Release();
        CoUninitialize();
        return 1;
    }

    IWbemClassObject *pclsObj = NULL;
    ULONG uReturn = 0;

    while (pEnumerator)
    {
        hres = pEnumerator->Next(WBEM_INFINITE, 1,
                                 &pclsObj, &uReturn);

        if (0 == uReturn)
        {
            break;
        }

        VARIANT vtProp;
        hres = pclsObj->Get(L"CurrentTemperature", 0, &vtProp, 0, 0);
        if (SUCCEEDED(hres))
        {
            // CPU温度是一个16进制的值,需进行转换
            int temperature = 0;
            std::stringstream ss;
            ss << std::hex << vtProp.uintVal;
            ss >> temperature;

            std::cout << "CPU温度: " << temperature << " Celsius" << std::endl;
        }

        VariantClear(&vtProp);
        pclsObj->Release();
    }

    // 清理
    pSvc->Release();
    pLoc->Release();
    pEnumerator->Release();
    CoUninitialize();

    return 0;
}

请注意,此代码使用WMI (Windows Management Instrumentation) 查询MSAcpi_ThermalZoneTemperature类以获取CPU温度。代码中的查询语句可以根据需要进行修改。此外,您需要在编译时链接wbemuuid.lib库。

请注意,CPU温度的获取可能会因计算机硬件和驱动程序的差异而有所

除了使用WMI,还可以通过其他方式来获取Windows系统的CPU温度。以下是一些常用的方法:

  1. 使用第三方库和工具:有一些第三方库和工具可用于获取CPU温度,例如Open Hardware Monitor、HWiNFO、Core Temp等。您可以在这些工具的文档中查找相关的API或命令行接口来获取温度信息,并使用C++与其进行交互。

  2. 使用Win32 API:您可以尝试使用Win32 API的一些函数来获取硬件信息,包括CPU温度。例如,使用GetSystemPowerStatus函数和SYSTEM_POWER_STATUS结构可以获取电源状态信息,其中可能包含CPU温度。请注意,此方法的可行性和准确性可能因操作系统和硬件厂商而有所不同。

  3. 使用驱动程序接口(例如,通过特定供应商的SDK):某些硬件供应商提供了软件开发工具包(SDK),允许开发者通过驱动程序接口获取硬件信息,包括CPU温度。您可以查看相关供应商的文档和开发者资源,以了解如何使用其SDK来获取CPU温度。

请注意,具体的实现方法会依赖于您的硬件配置和操作系统。建议在使用第三方库或工具时阅读其文档和使用条款,并在使用API时参考相应的文档和规范。

参考

Truth to be told, it depends on the hardware.

A library that works on most hardware is OpenHardwareMonitorLib. Unfortunately, it has no documentation and actually doesn't really exist as an independent piece of software. It's part of an open source software named "Open Hardware Monitor". it's done in .NET C Sharp and of course, only works for Windows. Fortunately you can get it as a DLL, and the GUI is completely separated from the actual backend which is OpenHardwareMonitorLib.

Read this post on how to use it from C++

How to call a C# library from Native C++ (using C++\CLI and IJW)

So considering it has no docs it can be a bit hard to work with. After reading the source for a while this is my take:


using OpenHardwareMonitor.Hardware;
...
float? cpu_temperature_celcius = null;
Computer computer = new Computer();
computer.CPUEnabled = true;
computer.Open();
foreach (IHardware hardware in computer.Hardware)
    if (hardware.HardwareType == HardwareType.CPU)
        foreach (ISensor sensor in hardware.Sensors)
            if (sensor.SensorType == SensorType.Temperature)
                cpu_temperature_celcius = sensor.Value;

This C# code gets the temperature of the CPU in Celcius. Tested on an Intel Haswell CPU. It will most likely work for most other CPUs from AMD and Intel. OpenHardwareMonitorLib.dll is needed. You can compile it from source

You can get a lot of other information about the system with this library.

Note that the CPU of the user can have multiple temperature sensors. For example, a temperature sensor for every core, so don't always take the last one as I did in the example above.

Good luck.