Getting cpu temps using WMI

Hello I downloaded: https://github.com/rruz/wmi-delphi-code-creator this software to generate wmi code for temps.
Generated code from software and changed to support windows forms
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
 #include "ostuff.h"
#include <windows.h>
#include <stdio.h>
#include <string>
#include <algorithm> 
#using <system.management.dll>
//#include "stdafx.h"
#define _WIN32_DCOM
#include <iostream>
//using namespace std;
#include <comdef.h>
#include <Wbemidl.h>
# pragma comment(lib, "wbemuuid.lib")

//CREDENTIAL structure
//http://msdn.microsoft.com/en-us/library/windows/desktop/aa374788%28v=vs.85%29.aspx
#define CRED_MAX_USERNAME_LENGTH            513
#define CRED_MAX_CREDENTIAL_BLOB_SIZE       512
#define CREDUI_MAX_USERNAME_LENGTH CRED_MAX_USERNAME_LENGTH
#define CREDUI_MAX_PASSWORD_LENGTH (CRED_MAX_CREDENTIAL_BLOB_SIZE / 2)


#pragma comment(lib, "wbemuuid.lib")
#pragma comment(lib, "user32.lib")

using namespace System;
using namespace System::Management;

System::String^ temp();
System::String^ temp()
{
	wchar_t pszName[CREDUI_MAX_USERNAME_LENGTH + 1] = L"user";
	wchar_t pszPwd[CREDUI_MAX_PASSWORD_LENGTH + 1] = L"password";
	BSTR strNetworkResource;
	bool localconn = true;
	strNetworkResource = localconn ? L"\\\\.\\root\\WMI" : L"\\\\remote--machine\\root\\WMI";
	COAUTHIDENTITY *userAcct = NULL;
	COAUTHIDENTITY authIdent;
	HRESULT hres;
	hres = CoInitializeEx(0, COINIT_MULTITHREADED);
	/*if (FAILED(hres))
	{
		System:String ^ msg;
		msg = "Failed to initialize COM library. Error code: " + _com_error(hres).ErrorMessage();
		return msg;
	}*/
	if (localconn)
		hres = CoInitializeSecurity(
			NULL,
			-1,                          // COM authentication
			NULL,                        // Authentication services
			NULL,                        // Reserved
			RPC_C_AUTHN_LEVEL_DEFAULT,   // Default authentication
			RPC_C_IMP_LEVEL_IMPERSONATE, // Default Impersonation
			NULL,                        // Authentication info
			EOAC_NONE,                   // Additional capabilities
			NULL                         // Reserved
		);
	else
		hres = CoInitializeSecurity(
			NULL,
			-1,                          // COM authentication
			NULL,                        // Authentication services
			NULL,                        // Reserved
			RPC_C_AUTHN_LEVEL_DEFAULT,   // Default authentication
			RPC_C_IMP_LEVEL_IDENTIFY,    // Default Impersonation
			NULL,                        // Authentication info
			EOAC_NONE,                   // Additional capabilities
			NULL                         // Reserved
		);

	/*if (FAILED(hres))
	{
		CoUninitialize();
		return "Failed to initialize security. Error code = 0x" + hres;
	}*/

	IWbemLocator *pLoc = NULL;
	hres = CoCreateInstance(CLSID_WbemLocator, 0, CLSCTX_INPROC_SERVER, IID_IWbemLocator, (LPVOID *)&pLoc);
	/*if (FAILED(hres))
	{
		cout << "Failed to create IWbemLocator object." << " Err code = 0x" << hex << hres << endl;
		cout << _com_error(hres).ErrorMessage() << endl;
		CoUninitialize();
		return 1;
	}*/
	IWbemServices *pSvc = NULL;
	if (localconn)
		hres = pLoc->ConnectServer(
			_bstr_t(strNetworkResource),
			NULL,
			NULL,
			0,
			NULL,
			0,
			0,
			&pSvc
		);
	else
		hres = pLoc->ConnectServer(
			_bstr_t(strNetworkResource),
			_bstr_t(pszName),
			_bstr_t(pszPwd),
			NULL,
			NULL,
			NULL,
			NULL,
			&pSvc
		);
	/*if (FAILED(hres))
	{
		cout << "Could not connect. Error code = 0x" << hex << hres << endl;
		cout << _com_error(hres).ErrorMessage() << endl;
		pLoc->Release();
		CoUninitialize();
		cout << "press enter to exit" << endl;
		cin.get();
		return 1;                // Program has failed.
	}*/
	//cout << "Connected to root\\WMI WMI namespace" << endl;
	if (localconn)
		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
		);
	else
	{
		authIdent.PasswordLength = wcslen(pszPwd);
		authIdent.Password = (USHORT*)pszPwd;
		authIdent.User = (USHORT*)pszName;
		authIdent.UserLength = wcslen(pszName);
		authIdent.Domain = 0;
		authIdent.DomainLength = 0;
		authIdent.Flags = SEC_WINNT_AUTH_IDENTITY_UNICODE;
		userAcct = &authIdent;
		hres = CoSetProxyBlanket(
			pSvc,
			RPC_C_AUTHN_DEFAULT,
			RPC_C_AUTHZ_DEFAULT,
			COLE_DEFAULT_PRINCIPAL,
			RPC_C_AUTHN_LEVEL_PKT_PRIVACY,
			RPC_C_IMP_LEVEL_IMPERSONATE,
			userAcct,
			EOAC_NONE
		);
	}
	/*if (FAILED(hres))
	{
		cout << "Could not set proxy blanket. Error code = 0x" << hex << hres << endl;
		cout << _com_error(hres).ErrorMessage() << endl;
		pSvc->Release();
		pLoc->Release();
		CoUninitialize();
		cout << "press enter to exit" << endl;
		cin.get();
		return 1;               // Program has failed.
	}*/
	IEnumWbemClassObject* pEnumerator = NULL;
	hres = pSvc->ExecQuery(L"WQL", L"SELECT * FROM MSAcpi_ThermalZoneTemperature", WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY, NULL, &pEnumerator);
	/* if (FAILED(hres))
	{
		/*cout << "ExecQuery failed" << " Error code = 0x" << hex << hres << endl;
		cout << _com_error(hres).ErrorMessage() << endl;
		pSvc->Release();
		pLoc->Release();
		CoUninitialize();
		cout << "press enter to exit" << endl;
		cin.get();
		return 1;             // Program has failed.
	}*/
	if (!localconn)
	{

		hres = CoSetProxyBlanket(
			pEnumerator,
			RPC_C_AUTHN_DEFAULT,
			RPC_C_AUTHZ_DEFAULT,
			COLE_DEFAULT_PRINCIPAL,
			RPC_C_AUTHN_LEVEL_PKT_PRIVACY,
			RPC_C_IMP_LEVEL_IMPERSONATE,
			userAcct,
			EOAC_NONE
		);

		if (FAILED(hres))
		{
			/*cout << "Could not set proxy blanket on enumerator. Error code = 0x" << hex << hres << endl;
			cout << _com_error(hres).ErrorMessage() << endl;
			pEnumerator->Release();
			pSvc->Release();
			pLoc->Release();
			CoUninitialize();*/
		}
	}
	IWbemClassObject *pclsObj = NULL;
	ULONG uReturn = 0;
	while (pEnumerator)
	{
		HRESULT hr = pEnumerator->Next(WBEM_INFINITE, 1, &pclsObj, &uReturn);
		if (0 == uReturn || FAILED(hr)) break;
		VARIANT vtProp;
		hr = pclsObj->Get(L"CurrentTemperature", 0, &vtProp, 0, 0);// Uint32
		if (!FAILED(hr))
		{
			if ((vtProp.vt == VT_NULL) || (vtProp.vt == VT_EMPTY)) return "CurrentTemperature : " + ((vtProp.vt == VT_NULL) ? "NULL" : "EMPTY");
			else if ((vtProp.vt & VT_ARRAY))return "CurrentTemperature : " + "Array types not supported (yet)";
			else return "CurrentTemperature : " + vtProp.uintVal;
		}
		VariantClear(&vtProp);
		pclsObj->Release();
		pclsObj = NULL;
	}

	// Cleanup

	pSvc->Release();
	pLoc->Release();
	pEnumerator->Release();
	if (pclsObj != NULL)
		pclsObj->Release();

	CoUninitialize();
}

Output(in same file as temps() function):
msg = temp() +"\n";
But it doesnt show nothing.
P.S. where failed checks are they disabled since software generates code for console. What did i miss?
P.S.S. my project is C++ > CLR empty project with C++17
Last edited on
I can't see the problem, so you'll have to track it down. I'd start right here : put in some cout statements for testing to see which statement is actually working. Maybe even show the current temp to make sure it's working.

You might put this over in the windows forum.

1
2
3
4
5
6
	if (!FAILED(hr))
		{
			if ((vtProp.vt == VT_NULL) || (vtProp.vt == VT_EMPTY)) return "CurrentTemperature : " + ((vtProp.vt == VT_NULL) ? "NULL" : "EMPTY");
			else if ((vtProp.vt & VT_ARRAY))return "CurrentTemperature : " + "Array types not supported (yet)";
			else return "CurrentTemperature : " + vtProp.uintVal;
		}

Last edited on
SamuelAdams thanks for help, but playin with code i noticed that temp data is delayed or not correct, so traveling accros the internet found this: http://www.alcpu.com/CoreTemp/developers.html
But have no clue how to integrate in my project, if someone could give me simple tutorial i would be very thankful. Thanks.
Up.
Topic archived. No new replies allowed.