16,472
社区成员
发帖
与我相关
我的任务
分享
// MyWmi.cpp: implementation of the CMyWmi class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "WMI.h"
#include "MyWmi.h"
#include <wbemcli.h> // WMI interface declarations
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CMyWmi::CMyWmi()
{
m_strNameSpace = "";
m_pIWbemServices = NULL;
}
CMyWmi::~CMyWmi()
{
if(m_pIWbemServices != NULL)
{
m_pIWbemServices->Release();
m_pIWbemServices = NULL;
}
Uninit();
}
BOOL CMyWmi::InitSecurity()
{
HRESULT hres = CoInitializeSecurity(NULL, -1, NULL, NULL,
RPC_C_AUTHN_LEVEL_PKT_PRIVACY,
RPC_C_IMP_LEVEL_IMPERSONATE,
NULL,
EOAC_SECURE_REFS, //change to EOAC_NONE if you change dwAuthnLevel to RPC_C_AUTHN_LEVEL_NONE
NULL);
return SUCCEEDED(hres);
}
BOOL CMyWmi::Init()
{
if(SUCCEEDED(CoInitializeEx(NULL, COINIT_MULTITHREADED)))
{
if(!InitSecurity())
{
CoUninitialize();
return FALSE;
}
return TRUE;
}
return FALSE;
}
void CMyWmi::Uninit()
{
CoUninitialize();
}
BOOL CMyWmi::Connect()
{
IWbemLocator *pIWbemLocator = NULL;
m_strNameSpace = _T("\\\\.\\root\\cimv2");
if(CoCreateInstance(CLSID_WbemLocator,
NULL,
CLSCTX_INPROC_SERVER,
IID_IWbemLocator,
(LPVOID *) &pIWbemLocator) == S_OK)
{
BSTR pNamespace = m_strNameSpace.AllocSysString();
if(!pNamespace)
{
pIWbemLocator->Release();
return FALSE;
}
HRESULT hr = S_OK;
hr = pIWbemLocator->ConnectServer(pNamespace, NULL, NULL, NULL, 0L, NULL, NULL, &m_pIWbemServices);
if(hr == S_OK)
{
if(pNamespace)
{
SysFreeString(pNamespace);
}
if(pIWbemLocator)
{
pIWbemLocator->Release();
pIWbemLocator = NULL;
}
return TRUE;
}
}
return FALSE;
}
DWORD CMyWmi::GetCpuSpeed()
{
BSTR bstrCpuName=NULL;
BSTR bstrPropName = NULL;
VARIANT pVal;
CString strText="";
int nCpuNum=0;
DWORD dwCpuSpeed=0;
IWbemClassObject *pCpuInst=NULL;
IWbemQualifierSet* pQualSet = NULL;
bstrPropName = SysAllocString(L"CurrentClockSpeed");
if(bstrPropName == NULL)
{
return dwCpuSpeed;
}
while(1)
{
strText.Format("Win32_Processor.DeviceID=\"CPU%d\"", nCpuNum);
bstrCpuName = strText.AllocSysString();
if(bstrCpuName == NULL)
{
break;
}
if(m_pIWbemServices->GetObject(bstrCpuName, 0L, NULL, &pCpuInst, NULL) != S_OK)
{
break;
}
if(pCpuInst->Get(bstrPropName, 0L, &pVal, NULL, NULL) == S_OK)
{
nCpuNum++;
dwCpuSpeed += pVal.lVal;
}
VariantClear(&pVal);
SysFreeString(bstrCpuName);
bstrCpuName = NULL;
}
if(bstrPropName != NULL)
{
SysFreeString(bstrPropName);
bstrPropName = NULL;
}
if(bstrCpuName != NULL)
{
SysFreeString(bstrCpuName);
bstrCpuName = NULL;
}
char szText[256]="\0";
sprintf(szText, "CPU个数: %d, CPU频率: %ld Mhz", nCpuNum, dwCpuSpeed);
MessageBox(AfxGetMainWnd()->m_hWnd, szText, "CPU信息", MB_OK);
return dwCpuSpeed;
}
// MyWmi.h: interface for the CMyWmi class.
//
//////////////////////////////////////////////////////////////////////
#if !defined(AFX_MYWMI_H__F6FDA53D_BF49_4113_8038_0B31A08699E6__INCLUDED_)
#define AFX_MYWMI_H__F6FDA53D_BF49_4113_8038_0B31A08699E6__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
#include <wbemcli.h> // WMI interface declarations
class CMyWmi
{
public:
CMyWmi();
virtual ~CMyWmi();
public:
DWORD GetCpuSpeed();
BOOL Connect();
void Uninit();
BOOL Init();
private:
CString m_strNameSpace;
private:
IWbemServices *m_pIWbemServices;
BOOL InitSecurity();
};
#endif // !defined(AFX_MYWMI_H__F6FDA53D_BF49_4113_8038_0B31A08699E6__INCLUDED_)