Click here to Skip to main content
15,885,899 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
.i try to get it by wmi ...but faild.......i used C...

C++
#define _WIN32_WINNT 0x0400 
#define _WIN32_DCOM 

#include <stdio.h> 
#include <tchar.h> 
#include <windows.h> 
#include <wbemidl.h> 
#pragma comment(lib,"wbemuuid.lib")

VOID GetCPUTemperatureInfo()
{
	// result code from COM calls 
	HRESULT hr = 0; 

	// COM interface pointers 
	IWbemLocator         *locator  = NULL; 
	IWbemServices        *services = NULL; 
	IEnumWbemClassObject *results  = NULL; 

	// BSTR strings we'll use (http://msdn.microsoft.com/en-us/library/ms221069.aspx) 
	BSTR resource = SysAllocString(L"ROOT\\CIMV2"); 
	BSTR language = SysAllocString(L"WQL"); 
	BSTR query    = SysAllocString(L"SELECT * FROM Win32_TemperatureProbe"); 

	// initialize COM 
	hr = CoInitializeEx(0, COINIT_MULTITHREADED); 
	hr = CoInitializeSecurity(NULL, -1, NULL, NULL, RPC_C_AUTHN_LEVEL_DEFAULT, RPC_C_IMP_LEVEL_IMPERSONATE, NULL, EOAC_NONE, NULL); 

	// connect to WMI 
	hr = CoCreateInstance(&CLSID_WbemLocator, 0, CLSCTX_INPROC_SERVER, &IID_IWbemLocator, (LPVOID *) &locator); 
	hr = locator->lpVtbl->ConnectServer(locator, resource, NULL, NULL, NULL, 0, NULL, NULL, &services); 

	// issue a WMI query 
	hr = services->lpVtbl->ExecQuery(services, language, query, WBEM_FLAG_BIDIRECTIONAL, NULL, &results); 

	// list the query results 
	if (results != NULL) { 
		IWbemClassObject *result = NULL; 
		ULONG returnedCount = 0; 

		// enumerate the retrieved objects 
		while((hr = results->lpVtbl->Next(results, WBEM_INFINITE, 1, &result, &returnedCount)) == S_OK) { 
			VARIANT CurrentReading;

			// obtain the desired properties of the next result and print them out 
			hr = result->lpVtbl->Get(result, L"CurrentReading", 0, &CurrentReading, 0, 0); 

			printf(L"CurrentReading: %d\n", CurrentReading); 

			// release the current result object 
			result->lpVtbl->Release(result); 
		}
	}

	// release WMI COM interfaces 
	results->lpVtbl->Release(results); 
	services->lpVtbl->Release(services); 
	locator->lpVtbl->Release(locator); 

	// unwind everything else we've allocated 
	CoUninitialize(); 

	SysFreeString(query); 
	SysFreeString(language); 
	SysFreeString(resource); 

	return;
}
Posted

1 solution

You can't guarantee to get CPU or HDD temperatures in that way (or any other) - it depends entirely on whether the manufacturer of the motherboard and HDD have provided the hardware and drivers that conform to the spec and do provide it to the system at all. As this is not compulsory, many manufacturers do not do it at all, and the values you get are meaningless.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900