WMI編程之一:在VC中使用WMI
經過一周的忙碌,總算是有點時間可以寫一寫博客了,現在就講講如何在VC中使用WMI。下面就分五步來構建一個簡單的WMI的應用框架。在寫程序前千萬不要忘了包含頭文件和鏈接庫文件。
#include <Wbemidl.h>
# pragma comment(lib, "wbemuuid.lib")
# pragma comment(lib, "wbemuuid.lib")
之后我們可以了第一步:初始化COM
前面已經講過,WMI是基于COM(組件對象模型)的,所以在使用WMI前,我們必須首先初始化COM。這里主要用到兩個函數
HRESULT hres;
//初始化 COM.
hres = CoInitializeEx(0, COINIT_MULTITHREADED);
if (FAILED(hres))
{
cout << "Failed to initialize COM library. "
<< "Error code = 0x"
<< hex << hres << endl;
return 1; // Program has failed.
}
// 設置進程安全級別
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; // Program has failed.
}
//初始化 COM.
hres = CoInitializeEx(0, COINIT_MULTITHREADED);
if (FAILED(hres))
{
cout << "Failed to initialize COM library. "
<< "Error code = 0x"
<< hex << hres << endl;
return 1; // Program has failed.
}
// 設置進程安全級別
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; // Program has failed.
}
第二步:創建一個WMI命名空間連接
//創建一個CLSID_WbemLocator對象
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; // Program has failed.
}
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; // Program has failed.
}
//已經連接到WMI了
cout << "Connected to ROOT\\CIMV2 WMI namespace" << endl;
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; // Program has failed.
}
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; // Program has failed.
}
//已經連接到WMI了
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 service
NULL, // Server principal name
RPC_C_AUTHN_LEVEL_CALL, // authentication 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; // Program has failed.
}
= CoSetProxyBlanket(
pSvc,
// the proxy to set
RPC_C_AUTHN_WINNT, // authentication service
RPC_C_AUTHZ_NONE, // authorization service
NULL, // Server principal name
RPC_C_AUTHN_LEVEL_CALL, // authentication 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; // Program has failed.
}
第四步:執行你的代碼,達成你的目的
這里一不小心就要引入WQL這個概念了。WQL就是WMI中的查詢語言,WQL的全稱是WMI Query Language,簡稱為WQL,翻譯成中文好像可以成為Windows管理規范查詢語言。熟悉SQL語言的朋友會感覺它和SQL非常相似。
WQL其實非常簡單,它有如下特點:
1、每個WQL語句必須以SELECT開始;
2、SELECT后跟你需要查詢的屬性名(我剛才對應SQL將其稱之為字段名了),也可以像SQL一樣,以*表示返回所有屬性值;
3、FROM關鍵字;
4、你要查詢的類的名字;
//這里是列出正在運行的進程的例子
//為了接收結果,你必須定義一個枚舉對象
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 (AILED(hres))
{
cout << "Query for processes failed. "
<< "Error code = 0x"
<< hex << hres << endl;
pSvc->Release();
pLoc->Release();
CoUninitialize();
return 1; // Program has failed.
}
else
{
IWbemClassObject *pclsObj;
ULONG uReturn = 0;
while (pEnumerator)
{
// 推出下一個對象
res = pEnumerator->Next(WBEM_INFINITE, 1,
&pclsObj, &uReturn);
//沒有東西了就跳出去吧
if(0 == uReturn)
{
break;
}
VARIANT vtProp;
// Get the value of the Name property
hres = pclsObj->Get(L"Name", 0, &vtProp, 0, 0);
wcout << "Process Name : " << vtProp.bstrVal << endl;
VariantClear(&vtProp);
}
}
//為了接收結果,你必須定義一個枚舉對象
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 (AILED(hres))
{
cout << "Query for processes failed. "
<< "Error code = 0x"
<< hex << hres << endl;
pSvc->Release();
pLoc->Release();
CoUninitialize();
return 1; // Program has failed.
}
else
{
IWbemClassObject *pclsObj;
ULONG uReturn = 0;
while (pEnumerator)
{
// 推出下一個對象
res = pEnumerator->Next(WBEM_INFINITE, 1,
&pclsObj, &uReturn);
//沒有東西了就跳出去吧
if(0 == uReturn)
{
break;
}
VARIANT vtProp;
// Get the value of the Name property
hres = pclsObj->Get(L"Name", 0, &vtProp, 0, 0);
wcout << "Process Name : " << vtProp.bstrVal << endl;
VariantClear(&vtProp);
}
}
第五步:清除關閉你的程序
釋放掉該釋放的東西是個好習慣。
pSvc->Release();
pLoc->Release();
CoUninitialize();
pLoc->Release();
CoUninitialize();
到這里,我們就基本完成了WMI最簡單的應用框架,是不是感覺還有很多工作需要做?沒關系,下次我們再介紹一個有UI的框架,以方便大家的使用。這里是本章的例子:http://files.cnblogs.com/hamwolf/WMIBase.zip
全站熱搜