CPU Temperature using WMI in C++

A

Alain Dekker

Hi,

I'm using the code given at the bottom to read the processor load. It works
perfectly. However, when I change this line:
BSTR strQuery = (L"Select * from win32_Processor");
to
BSTR strQuery = (L"Select * from MSAcpi_ThermalZoneTemperature");

and
BSTR strClassProp = SysAllocString(L"LoadPercentage");
to
BSTR strClassProp = SysAllocString(L"CurrentTemperature");

it does NOT work.

Any ideas?

Thanks,
Alain

CODE FOR READING PROCESSOR LOAD:

CoInitialize(NULL);
if (CoInitializeSecurity(NULL, -1, NULL, NULL, RPC_C_AUTHN_LEVEL_PKT,
RPC_C_IMP_LEVEL_IMPERSONATE, NULL, EOAC_NONE, 0) != S_OK)
return;

IWbemLocator* pIWbemLocator = NULL;
IWbemServices* pWbemServices = NULL;
IEnumWbemClassObject* pEnumObject = NULL;

BSTR bstrNamespace = (L"root\\cimv2");

if (CoCreateInstance (
CLSID_WbemAdministrativeLocator,
NULL ,
CLSCTX_INPROC_SERVER | CLSCTX_LOCAL_SERVER ,
IID_IUnknown ,
( void ** ) & pIWbemLocator) != S_OK)
return;

if (pIWbemLocator->ConnectServer(bstrNamespace, NULL, NULL, NULL, 0,
NULL, NULL,&pWbemServices) != S_OK)
return;

HRESULT hRes;
BSTR strQuery = (L"Select * from win32_Processor");
BSTR strQL = (L"WQL");
hRes = pWbemServices->ExecQuery(strQL, strQuery,
WBEM_FLAG_RETURN_IMMEDIATELY, NULL, &pEnumObject);
if (hRes != S_OK)
{
MessageBox("Could not execute Query");
return;
}

hRes = pEnumObject->Reset();
if (hRes != S_OK)
{
MessageBox("Could not Enumerate");
return;
}

ULONG uCount = 1, uReturned;
IWbemClassObject* pClassObject = NULL;
hRes = pEnumObject->Next(WBEM_INFINITE,uCount, &pClassObject, &uReturned);
if(hRes != S_OK)
{
MessageBox("Could not Enumerate");
return;
}

VARIANT v;
BSTR strClassProp = SysAllocString(L"LoadPercentage");
hRes = pClassObject->Get(strClassProp, 0, &v, 0, 0);
if (hRes != S_OK)
{
MessageBox("Could not Get Value");
return;
}

SysFreeString(strClassProp);
_bstr_t bstrPath = &v; //Just to convert BSTR to ANSI
char* strPath=(char*)bstrPath;

if (SUCCEEDED(hRes))
MessageBox(strPath);
else
MessageBox("Error in getting object");

VariantClear(&v);
pIWbemLocator->Release();
pWbemServices->Release();
pEnumObject->Release();
pClassObject->Release();
CoUninitialize();
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top