一、使用到的头文件有:
#include <iostream>
#include <iomanip> 格式化输出控制必须
#include <comdef.h> 使用_bstr_t,bstr_t,variant,_variant_t变量时
#include <wbemidl.h> 连接WMI必须
二、预处理指令: #pragma comment(lib,"wbemuuid.lib") 连接一个库文件
三、保证调用CoInitializeEx成功的配置:
菜单选择:"Project"--->"Settings"--->"C/C++"
在Catelgory项右边的下拉列表选择"Preprocessor",接着在"Preprocessor Definitions: "下面输入: _WIN32_DCOM
------------------------------在Winxp VC6.0下调试成功--------------------------------
四、详细代码如下:
int main(int argc, char* argv[])
{
//第一步,初始化COM
HRESULT hres;
hres=CoInitializeEx(NULL,COINIT_MULTITHREADED);
if (FAILED(hres))
{
cout<<"Can‘t initialize COM object "
<<"Error code 0X"
<<hex<<hres<<endl;
return 1;
}
//设置进程安全级别
hres=CoInitializeSecurity(
NULL,
-1, //Com negotiates service
NULL, //Authentication services
NULL, //reserved
RPC_C_AUTHN_LEVEL_DEFAULT,//authentication
RPC_C_IMP_LEVEL_IMPERSONATE,//impersonation
NULL,//authentication info
EOAC_NONE,//additional capabilities
NULL);//reserved
if (FAILED(hres))
{
cout<<"Failed to initialize security. "
<<"Error code= 0X"
<<hex<<hres<<endl;
CoUninitialize();
return 1;
}
//第二步,创建一个WMI命名空间连接
IWbemLocator *pLoc=0;
hres=CoCreateInstance(
CLSID_WbemLocator,
0,
CLSCTX_INPROC_SERVER,
IID_IWbemLocator,
(LPVOID*)&pLoc);
if (FAILED(hres))
{
cout<<"Failed to create IwbemLocator object. "
<<"Error code=0X"
<<hex<<hres<<endl;
CoUninitialize();
return 1;
}
IWbemServices *pSvc=0;
//使用pLoc连接到"root\cimv2",并把pSvc的指针也搞定了
hres=pLoc->ConnectServer(
_bstr_t(L"root\\cimv2"),//WMI namespace
NULL, //User Name
NULL, //User password
0, //Locale
NULL, //Security flags
0, //authority
0, //context object
&pSvc //iwbemservices proxy
);
if (FAILED(hres))
{
cout<<"Could not connect. Error code=0x"
<<hex<<hres<<endl;
pLoc->Release();
CoUninitialize();
return 1;
}
else
{
cout<<"Connected to Root\\cimv2 wmi namespace"<<endl;
}
//第三步,设置连接的安全级别
hres=CoSetProxyBlanket(
pSvc, //the proxy to set
RPC_C_AUTHN_WINNT, //authentication service
RPC_C_AUTHZ_NONE, //authorization serivce
NULL, //server principal name
RPC_C_AUTHN_LEVEL_CALL, //authenticatio level
RPC_C_IMP_LEVEL_IMPERSONATE, //impersonation level
NULL, //client identity
EOAC_NONE //proxy capabilities
);
if (FAILED(hres))
{
cout<<"could not set proxy blanket. Error code=0x"
<<hex<<hres<<endl;
pSvc->Release();
pLoc->Release();
CoUninitialize();
return 1;
}
else
{
cout<<"It‘s OK to set proxy blanket."<<endl;
}
//-----------------------前三步通常情况下固定格式,不需改变-------------------------
//第四步,执行代码
IEnumWbemClassObject* pEnumerator=NULL;
hres=pSvc->ExecQuery(
bstr_t("WQL"),
bstr_t("select * from win32_process"),
WBEM_FLAG_FORWARD_ONLY|WBEM_FLAG_RETURN_IMMEDIATELY,
NULL,
&pEnumerator);
if (FAILED(hres))
{
cout<<"Query for process failed. "
<<"Error code=0x"
<<hex<<hres<<endl;
pSvc->Release();
pLoc->Release();
CoUninitialize();
return 1;
}
else
{
IWbemClassObject *pclsObj;
ULONG uReturn=0;
VARIANT vcProp;
while(pEnumerator)
{
hres=pEnumerator->Next(WBEM_INFINITE,1,&pclsObj,&uReturn);
if (0==uReturn)
{
break;
}
else
{
VariantInit(&vcProp);
}
hres=pclsObj->Get(L"name",0,&vcProp,0,0);
wcout<<L"Process Name: "<<vcProp.bstrVal<<endl;
VariantClear(&vcProp);
}
}
//第五步,清除关闭程序
pSvc->Release();
pLoc->Release();
CoUninitialize();
return 0;
}
本文出自 “无情剑客” 博客,请务必保留此出处http://tommy.blog.51cto.com/113624/1357016