wmi 获取系统信息
#ifndef MY_WMI_CLASS_H__#define MY_WMI_CLASS_H__
/*******************************************************************************
*@file
*@author def< qq group: 324164944 >
*@blog http://www.cnblogs.com/itdef/
*@brief
/*******************************************************************************/
#include <Wbemidl.h>
namespace DEF
{
class CWMI
{
IWbemLocator *pLoc_;
IWbemServices *pSvc_;
public:
CWMI():pLoc_(NULL),pSvc_(NULL){}
~CWMI();
BOOL QueryInfo(const char* strSQL, const char* strAttr );
BOOL Initialize();
};
} //namespace DEF
#endif //MY_WMI_CLASS_H__
#include "stdafx.h"
/*******************************************************************************
*@file
*@author def< qq group: 324164944 >
*@blog http://www.cnblogs.com/itdef/
*@brief
/*******************************************************************************/
#include "CMyWMIClass.h"
#include <iostream>
#include <comutil.h>
using namespace DEF;
using namespace std;
#pragma comment(lib,"Wbemuuid")
#pragma comment(lib,"comsuppw")
BOOL CWMI::Initialize()
{
BOOL bRet = FALSE;
HRESULT hres; //定义COM调用的返回
try{
hres =CoInitializeEx(0, COINIT_MULTITHREADED);
if (FAILED(hres))
{
throw exception("CoInitializeEx() error.");
}
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
);
if (FAILED(hres))
{
throw exception("CoInitializeEx() error.");
}
hres = CoCreateInstance(
CLSID_WbemLocator,
0,
CLSCTX_INPROC_SERVER,
IID_IWbemLocator, (LPVOID *) &pLoc_);
if (FAILED(hres))
{
throw exception("CoCreateInstance() error.");
}
// to make IWbemServices calls.
hres = pLoc_->ConnectServer(
_bstr_t(L"ROOT\\CIMV2"), // Object path of WMI namespace
NULL, // User name. NULL = current user
NULL, // User password. NULL = current
0, // Locale. NULL indicates current
NULL, // Security flags.
0, // Authority (e.g. Kerberos)
0, // Context object
&pSvc_ // pointer to IWbemServices proxy
);
if (FAILED(hres))
{
throw exception("ConnectServer() error.");
}
hres = CoSetProxyBlanket(
pSvc_, // Indicates the proxy to set
RPC_C_AUTHN_WINNT, // RPC_C_AUTHN_xxx
RPC_C_AUTHZ_NONE, // RPC_C_AUTHZ_xxx
NULL, // Server principal name
RPC_C_AUTHN_LEVEL_CALL, // RPC_C_AUTHN_LEVEL_xxx
RPC_C_IMP_LEVEL_IMPERSONATE, // RPC_C_IMP_LEVEL_xxx
NULL, // client identity
EOAC_NONE // proxy capabilities
);
if (FAILED(hres))
{
throw exception("CoSetProxyBlanket() error.");
}
bRet = TRUE;
}catch(exception& e)
{
cout << e.what() << endl;
}
return bRet;
}
CWMI::~CWMI()
{
if( NULL != pSvc_)
{
pSvc_->Release();
pSvc_ = NULL;
}
if(pLoc_ != NULL )
{
pLoc_->Release();
pLoc_ = NULL;
}
CoUninitialize();
}
BOOL CWMI::QueryInfo(const char* strSQL, const char* strAttr )
{
HRESULT hres; //定义COM调用的返回
IWbemClassObject* pclsObj = NULL;
IEnumWbemClassObject* pEnum = NULL;
BOOL bRet = FALSE;
if( NULL == strSQL ||
NULL == strAttr)
{
return FALSE;
}
try{
hres = pSvc_->ExecQuery(
bstr_t("WQL"),
bstr_t(strSQL),
WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY,
NULL,
&pEnum);
if (FAILED(hres))
{
throw exception("ExecQuery() error.");
}
ULONG uReturn = 0;
while( pEnum )
{
HRESULT hr = pEnum->Next( WBEM_INFINITE, 1, &pclsObj, &uReturn );
if( 0 == uReturn )
{
break;
}
VARIANT vtProp;
VariantInit( &vtProp );
hr = pclsObj->Get( bstr_t( strAttr ), 0, &vtProp, 0, 0 );
if (SUCCEEDED(hr) && (V_VT(&vtProp) == VT_BSTR))
{
char* str = _com_util::ConvertBSTRToString( vtProp.bstrVal );
//printf( "%s, length = %d \n", str, strlen( str ) );
printf("%s \n",str);
delete [] str;
}
VariantClear( &vtProp );
}
bRet = TRUE;
}catch(exception& e)
{
cout << e.what() << endl;
}
if(pclsObj != NULL)
{
pclsObj->Release();
pclsObj = NULL;
}
if(pEnum != NULL)
{
pEnum->Release();
pEnum = NULL;
}
return bRet;
}
测试代码
//
#include "stdafx.h"
/*******************************************************************************
*@file
*@author def< qq group: 324164944 >
*@blog http://www.cnblogs.com/itdef/
*@brief
/*******************************************************************************/
#include <iostream>
#include "CMyWMIClass.h"
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
DEF::CWMI a;
a.Initialize();
cout << endl << "Caption ";
a.QueryInfo("SELECT * FROM Win32_OperatingSystem","Caption");
cout << "Manufacturer ";
a.QueryInfo("SELECT * FROM Win32_OperatingSystem","Manufacturer");
cout << "CSName ";
a.QueryInfo("SELECT * FROM Win32_OperatingSystem","CSName");
cout << "PNPDeviceID ";
a.QueryInfo("SELECT * FROM Win32_DiskDrive", "PNPDeviceID");
cout << "Disk SerialNumber ";
a.QueryInfo("SELECT * FROM Win32_PhysicalMedia", "SerialNumber");
cout << "TotalVisibleMemorySize ";
a.QueryInfo("SELECT * FROM Win32_OperatingSystem","TotalVisibleMemorySize");
cout << "FreePhysicalMemory ";
a.QueryInfo("SELECT * FROM Win32_OperatingSystem","FreePhysicalMemory");
cout << "SerialNumber ";
a.QueryInfo("SELECT * FROM Win32_DiskDrive","SerialNumber");
// cout << "Process Caption";
// a.QueryInfo("SELECT * FROM Win32_Process","Caption");
//
// cout << "Process Handle";
// a.QueryInfo("SELECT * FROM Win32_Process","Handle");
//
cout << "Process ExecutablePath";
a.QueryInfo("SELECT * FROM Win32_Process","ExecutablePath");
return 0;
} 我觉得代码应该再加一些注释,解释你做每一步是为了个啥。
这样更容易看懂,甚至不用看代码就能知道代码是做啥的,啥时候能用。
页:
[1]