Hello everyone.
I tried many times to get information about the CPU temperature, but all attempts were as unsuccessful as possible, the only time I compiled without errors at such a moment:
FString USystemInfoBPLibrary::GetCPUTemperature()
{
HRESULT hres;
IWbemLocator* pLoc = nullptr;
IWbemServices* pSvc = nullptr;
hres = CoInitializeEx(0, COINIT_MULTITHREADED);
if (FAILED(hres))
{
return FString("COM Initialization Failed");
}
hres = CoInitializeSecurity(
NULL, -1, NULL, NULL,
RPC_C_AUTHN_LEVEL_DEFAULT,
RPC_C_IMP_LEVEL_IMPERSONATE,
NULL, EOAC_NONE, NULL
);
if (FAILED(hres))
{
CoUninitialize();
return FString("Security Initialization Failed");
}
hres = CoCreateInstance(
CLSID_WbemLocator, 0, CLSCTX_INPROC_SERVER,
IID_IWbemLocator, (LPVOID*)&pLoc
);
if (FAILED(hres))
{
CoUninitialize();
return FString("WMI Locator Creation Failed");
}
hres = pLoc->ConnectServer(
_bstr_t(L"ROOT\\CIMV2"),
NULL, NULL, 0, NULL, 0, 0, &pSvc
);
if (FAILED(hres))
{
pLoc->Release();
CoUninitialize();
return FString("WMI Connection Failed");
}
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))
{
pSvc->Release();
pLoc->Release();
CoUninitialize();
return FString("Failed to Set Proxy Blanket");
}
IEnumWbemClassObject* pEnumerator = nullptr;
hres = pSvc->ExecQuery(
bstr_t("WQL"),
bstr_t("SELECT * FROM Win32_TemperatureProbe"),
WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY, NULL, &pEnumerator
);
if (FAILED(hres))
{
pSvc->Release();
pLoc->Release();
return FString("WMI Query Execution Failed");
}
IWbemClassObject* pclsObj = nullptr;
ULONG uReturn = 0;
while (pEnumerator)
{
HRESULT hr = pEnumerator->Next(WBEM_INFINITE, 1, &pclsObj, &uReturn);
if (uReturn == 0) break;
VARIANT vtProp;
hr = pclsObj->Get(L"CurrentReading", 0, &vtProp, 0, 0);
if (SUCCEEDED(hr))
{
float temperature = (vtProp.intVal / 10.0f) - 273.15f;
VariantClear(&vtProp);
pclsObj->Release();
pSvc->Release();
pLoc->Release();
pEnumerator->Release();
CoUninitialize();
return FString::Printf(TEXT("CPU Temperature: %.2f C"), temperature);
}
pclsObj->Release();
}
pSvc->Release();
pLoc->Release();
pEnumerator->Release();
CoUninitialize();
return FString("No Data Available");
}
And there I received: COM Initialization Failed
I use includes:
#include "Windows/AllowWindowsPlatformTypes.h"
#include "Windows/AllowWindowsPlatformAtomics.h"
#include <Wbemidl.h>
#include <comutil.h>
#include "Windows/HideWindowsPlatformAtomics.h"
#include "Windows/HideWindowsPlatformTypes.h"
and etc....
That’s why I have a question, can I somehow get information about the temperature of my CPU, or maybe the GPU?