[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

How can I get CPU temperature in code

Posted on 2007-11-26
9
Medium Priority
?
4,677 Views
Last Modified: 2013-12-03
Windows Measurements Instrumentation interface contain  a method to get CPU temperature but from what I have read, it is not implemented for most motherboards.

Still, lots of products e.g. Sisoftware,MotherBoard monitor ,PC Wizard 2008 offer this service.  How do they get this data? Is it possible to inquire with the BIOS?  Is it Kernel Mode funcitonality?  

Any input - most welcome
0
Comment
Question by:yossikally
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
  • +2
9 Comments
 
LVL 8

Expert Comment

by:slado2
ID: 20351705
Check this article about reading cpu temperature using WMI.
Public Class Form1
 
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
 
        Try
 
            Dim searcher As New ManagementObjectSearcher( _
 
                "root\WMI", "SELECT * FROM MSAcpi_ThermalZoneTemperature")
 
 
 
            For Each queryObj As ManagementObject In searcher.Get()
 
                Dim temp As Double = CDbl(queryObj("CurrentTemperature"))
 
                temp = (temp - 2732) / 10.0
 
                MessageBox.Show(temp.ToString)
 
            Next
 
        Catch err As ManagementException
 
            MessageBox.Show("An error occurred while querying for WMI data: " & err.Message)
 
        End Try
 
    End Sub
 
End Class

Open in new window

0
 
LVL 8

Expert Comment

by:slado2
ID: 20351706
0
 
LVL 86

Expert Comment

by:jkr
ID: 20351745
Usually a vendor-specific driver reads that data and reports it, yet the driver interface might not be documented. One common way would be WMI, but that might not always work. Could you try

strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\WMI")
Set wbemObjectSet = objWMIService.ExecQuery ("SELECT * FROM MSAcpi_ThermalZoneTemperature")
For Each wbemObject In wbemObjectSet
       wscript.echo "-----------------------------------"
       wscript.echo "MSAcpi_ThermalZoneTemperature instance"
       wscript.echo "-----------------------------------"
       wscript.echo wbemObject.CurrentTemperature
Next

to see whether that works?
0
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 

Author Comment

by:yossikally
ID: 20353403
jkr, is there a C++ version of the aboe code?
0
 
LVL 86

Expert Comment

by:jkr
ID: 20353419
Well, sure, but that would be futile unless the VBS thing works...
0
 
LVL 86

Accepted Solution

by:
jkr earned 1000 total points
ID: 20353434
The C++ version would be

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 MSAcpi_ThermalZoneTemperature");
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"CurrentTemperature");
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();
0
 

Author Closing Comment

by:yossikally
ID: 31411013
This is probably the right solution, the only problem is that I cannot get it to run on my PC/XP, where I keep getting error 0x80041014 after the line
hRes = pEnumObject->Next(WBEM_INFINITE,uCount, &pClassObject, &uReturned);
 executes
to which there is no clear cut solution
0
 
LVL 3

Expert Comment

by:primeMover2004
ID: 20909664
jkr: did you copy this code from another website?
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
Ready to get certified? Check out some courses that help you prepare for third-party exams.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…

650 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question