封装WMI查询的简单类CWMIUtil
头文件WMIUtil.h
#pragma once
#include <Wbemidl.h>
class CWMIUtil
{
public:
CWMIUtil(void);
virtual ~CWMIUtil(void);
private:
// 禁止复制和赋值
CWMIUtil(const CWMIUtil&);
CWMIUtil& operator = (const CWMIUtil&);
public:
DWORD GetErrorCode() const;
const TCHAR *GetErrorInfo() const;
private:
void SetErrorCode(const DWORD dwErrorCode);
void SetErrorInfo(const TCHAR *szFormat, ...);
public:
BOOL Init();
BOOL ConnectServer(const TCHAR *szServer = TEXT("ROOT\\CIMV2"));
BOOL ExecQuery(const TCHAR *szQuerySql);
BOOL Next();
BOOL GetProperty(const TCHAR *szValueName, TCHAR *szValue, const DWORD dwSizeInByte);
private:
DWORD m_dwErrorCode;
CString m_csErrorInfo;
private:
BOOL m_bInitComSuccess;
private:
IWbemLocator *m_pLoc;
IWbemServices *m_pSvc;
IEnumWbemClassObject* m_pEnumerator;
IWbemClassObject *m_pCurObj;
};
#include "stdafx.h"
#include "WMIUtil.h"
#define _WIN32_DCOM
#include <comdef.h>
# pragma comment(lib, "wbemuuid.lib")
CWMIUtil::CWMIUtil(void)
: m_dwErrorCode(0), m_csErrorInfo(TEXT("")), m_bInitComSuccess(FALSE)
, m_pLoc(NULL), m_pSvc(NULL), m_pEnumerator(NULL), m_pCurObj(NULL)
{
}
CWMIUtil::~CWMIUtil(void)
{
try
{
// 释放对象
if (m_pCurObj)
{
m_pCurObj->Release();
}
if (m_pEnumerator)
{
m_pEnumerator->Release();
}
if (m_pSvc)
{
m_pSvc->Release();
}
if (m_pLoc)
{
m_pLoc->Release();
}
if (m_bInitComSuccess)
{
CoUninitialize();
}
}
catch (_com_error &e)
{
// COM exception
}
catch (CException &e)
{
// MFC exception
}
catch (...)
{
// all exception
}
}
DWORD CWMIUtil::GetErrorCode() const
{
return m_dwErrorCode;
}
const TCHAR *CWMIUtil::GetErrorInfo() const
{
return m_csErrorInfo.GetStrin