I tried a lot to use Win32_Processor(WMI class) to obtain
the processor speed in a Win2K system using C++, but could
not succeed.
Kindly help me with some sample code.
Thanks,
Smiles
Feel free to use this....
//==============================================================
#include "stdafx.h"
IWbemServices* ConnectToWMINamespace(BSTR bstrNamespace);
IEnumWbemClassObject* pEnum;
void main(int argc, char **argv)
{
HRESULT hr;
_bstr_t bstrNamespace(L"\\\\.\\root\\cimv2");
_bstr_t bstrWQL(L"WQL");
_bstr_t bstrQuery;
/* Lets connect to the cimv2 namespace in WMI */
IWbemServices* pWMIServices = ConnectToWMINamespace(bstrNamespace);
if (pWMIServices != NULL)
{
cout << "Now that we are connected with the WMI Service..." << endl <<
endl;
/**************************************************************************/
/* Now we will get a enumerator for all instances of the the Processor
*/
/**************************************************************************/
bstrQuery = L"SELECT * FROM Win32_Processor";
pEnum = NULL;
hr =
pWMIServices->ExecQuery(bstrWQL,bstrQuery,WBEM_FLAG_FORWARD_ONLY,NULL,&pEnum
);
if (FAILED(hr))
cout << "Query Execution Failed!!!. Error code = 0x" << hex << hr <<
endl;
else
{
/* Lets look at the data */
IWbemClassObject* pObj = NULL;
ULONG retCount;
_variant_t vSpeed;
for (int pi = 1; SUCCEEDED(hr=pEnum->Next(10000,1,&pObj,&retCount))
&& retCount; pi++)
{
hr = pObj->Get(L"CurrentClockSpeed",0,&vSpeed,NULL,NULL);
if (FAILED(hr))
cout << "Failed to retrieve CurrentClockSpeed for the
Processor!!!. Error code = 0x" << hex << hr << endl;
else
{
if (vSpeed.vt == VT_NULL)
cout << "Processor " << pi << ": The Current clock speed is :
*** VT_NULL ***" << endl;
else
cout << "Processor " << pi << ": The Current clock speed is : "
<< vSpeed.lVal << " Mhz" << endl;
}
hr = pObj->Get(L"MaxClockSpeed",0,&vSpeed,NULL,NULL);
if (FAILED(hr))
cout << "Failed to retrieve MaxClockSpeed for the Processor!!!.
Error code = 0x" << hex << hr << endl << endl;
else
{
if (vSpeed.vt == VT_NULL)
cout << "Processor " << pi << ": The Mac clock speed is : ***
VT_NULL ***" << endl << endl;
else
cout << "Processor " << pi << ": The Max clock speed is : " <<
vSpeed.lVal << " Mhz" << endl << endl;
}
pObj->Release();
}
if (FAILED(hr))
cout << "Failed to retrieve Processor instance from the
enumerator!!!. Error code = 0x" << hex << hr << endl << endl;
pEnum->Release();
pEnum = NULL;
}
}
/* Release final interface */
if (pWMIServices)
{
pWMIServices->Release();
CoUninitialize();
}
cout << endl << endl << "Press any key to exit." << endl;
getch();
}
IWbemServices* ConnectToWMINamespace(BSTR bstrNamespace)
{
HRESULT hres;
hres = CoInitializeEx(0, COINIT_MULTITHREADED); // Initialize COM.
if (FAILED(hres))
{
cout << "Failed to initialize COM library. Error code = 0x" << hex <<
hres << endl;
return NULL; // Program has failed.
}
hres = CoInitializeSecurity( NULL,
-1,
NULL,
NULL,
RPC_C_AUTHN_LEVEL_CONNECT,
RPC_C_IMP_LEVEL_IMPERSONATE,
NULL,
EOAC_NONE,
0);
if (FAILED(hres))
{
cout << "Failed to initialize security. Error code = 0x" << hex << hres
<< endl;
CoUninitialize();
return NULL; // Program has failed.
}
IWbemLocator *pLoc = 0;
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;
CoUninitialize();
return NULL; // Program has failed.
}
IWbemServices *pSvc = 0;
// Connect to the root\default namespace with the current user.
hres = pLoc->ConnectServer( bstrNamespace,
NULL,
NULL,
0,
NULL,
0,
0,
&pSvc);
if (FAILED(hres))
{
cout << "Could not connect. Error code = 0x" << hex << hres << endl;
pLoc->Release();
CoUninitialize();
return NULL; // Program has failed.
}
cout << "Connected to WMI" << endl << endl;
// Set the proxy so that impersonation of the client occurs.
// if you allow impersonation in the CoInitializeSecurity call
// then you could omit this step.
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))
{
cout << "Could not set proxy blanket. Error code = 0x" << hex << hres
<< endl;
pSvc->Release();
pLoc->Release();
CoUninitialize();
return NULL; // Program has failed.
}
// Cleanup
// ========
pLoc->Release();
return pSvc; // Program successfully completed.
}
//==============================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
//==================================================
// stdafx.h : include file for standard system include files,
// or project specific include files that are used frequently, but
// are changed infrequently
//
#if !defined(AFX_STDAFX_H__5FA6AA45_FF92_4B33_A670_9571A1AA61F4__INCLUDED_)
#define AFX_STDAFX_H__5FA6AA45_FF92_4B33_A670_9571A1AA61F4__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
#define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows
headers
#define POLARITY
#define _WIN32_DCOM
#include <stdio.h>
#include <conio.h>
#include <iostream.h>
#include <comdef.h>
/*************************************************************/
/* Make sure platform SDK is installed or the WMI SDK from: */
/* http://msdn.microsoft.com/downloads/sdks/wmi/download.asp */
/*************************************************************/
#include <wbemcli.h>
// TODO: reference additional headers your program requires here
//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately
before the previous line.
#endif //
!defined(AFX_STDAFX_H__5FA6AA45_FF92_4B33_A670_9571A1AA61F4__INCLUDED_)
//==================================================
Hope this helps
-Wes