奇怪 用下面的例子编译不通过!
① 初始化 COM 接口:
访问 WMI, 必须先初始化 COM 接口, 在程式的一开始调用 CoInitialize(NULL); 初始化, 在结束时调用 CoUninitialize(); 释放资源。
这两个函数在 #include <comdef.h> 里面定义。
② 获取访问 WMI 权限:
CoInitializeSecurity(NULL, -1, NULL, NULL, RPC_C_AUTHN_LEVEL_PKT, RPC_C_IMP_LEVEL_IMPERSONATE, NULL, EOAC_NONE, 0);
假如这个函数返回 S_OK 获取权限成功, 否则为失败。
③ 通过 IWbemLocator 和 IWbemServices 这两个 COM 接口访问 WMI, 获取系统信息:
这个函数的参数: lpList 返回信息, wsClass 为要查找的系统信息类, 这些 COM 接口在 #include <wbemidl.h> 里定义。
void GetWmiInfo(TStrings *lpList, WideString wsClass)
{
IWbemLocator *pWbemLocator = NULL;
if(CoCreateInstance(CLSID_WbemAdministrativeLocator, NULL, CLSCTX_INPROC_SERVER|CLSCTX_LOCAL_SERVER, IID_IUnknown, (void**)&pWbemLocator) == S_OK)
{
IWbemServices *pWbemServices = NULL;
WideString wsNamespace = (L"root\\cimv2");
if(pWbemLocator->ConnectServer(wsNamespace, NULL, NULL, NULL, 0, NULL, NULL, &pWbemServices) == S_OK)
{
IEnumWbemClassObject *pEnumClassObject = NULL;
WideString wsWQL=L"WQL", wsQuery=WideString(L"Select * from ") wsClass;
if(pWbemServices->ExecQuery(wsWQL, wsQuery, WBEM_FLAG_RETURN_IMMEDIATELY,NULL, &pEnumClassObject) == S_OK)
{
IWbemClassObject *pClassObject = NULL;
ULONG uCount = 1, uReturned;
if(pEnumClassObject->Reset() == S_OK)
{
int iEnumIdx = 0;
while(pEnumClassObject->Next(WBEM_INFINITE, uCount, &pClassObject, &uReturned) == S_OK)
{
lpList->Add("---------------- [" IntToStr(iEnumIdx) "] -----------------");
SAFEARRAY *pvNames = NULL;
if(pClassObject->GetNames(NULL, WBEM_FLAG_ALWAYS | WBEM_MASK_CONDITION_ORIGIN, NULL, &pvNames) == S_OK)
{
long vbl, vbu;
SafeArrayGetLBound(pvNames, 1, &vbl);
SafeArrayGetUBound(pvNames, 1, &vbu);
for(long idx=vbl; idx<=vbu; idx )
{
long aidx = idx;
wchar_t *wsName = 0;
VARIANT vValue;
VariantInit(&vValue);
SafeArrayGetElement(pvNames, &aidx, &wsName);
BSTR bs = SysAllocString(wsName);
HRESULT hRes = pClassObject->Get(bs, 0, &vValue, NULL, 0);
SysFreeString(bs);
if(hRes == S_OK)
{
AnsiString s;
Variant v = *(Variant*)&vValue;
if(v.IsArray())
{
for(int i=v.ArrayLowBound(); i<=v.ArrayHighBound(); i )
{
Variant a = v.GetElement(i);
if(!s.IsEmpty())
s =", ";
s =VarToStr(a);
}
}
else
{
s = VarToStr(v);
}
lpList->Add(AnsiString(wsName) "=" s);
}
VariantClear(&vValue);
SysFreeString(wsName);
}
}
if(pvNames)SafeArrayDestroy(pvNames);
iEnumIdx ;
}
}
if(pClassObject)pClassObject->Release();
}
if(pEnumClassObject)pEnumClassObject->Release();
}
if(pWbemServices)pWbemServices->Release();
}
if(pWbemLocator)pWbemLocator->Release();
}
//---------------------------------------
// 通过 WIN32_bios 获取 BIOS 信息:
void __fastcall TForm1::Button1Click(TObject *Sender)
{
Memo1->Lines->Add("================== [WIN32_bios] =================");
GetWmiInfo(Memo1->Lines, "WIN32_bios");
Memo1->Lines->Add("");
}
#include <Wbemidl.h>
#pragma link "Wbemuuid.lib"
//---------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
CoInitialize(NULL);
}
//---------------------------------------
void GetWmiInfo(TStrings *lpList, WideString wstrClass)
{
IWbemLocator *pWbemLocator = NULL;
if(CoCreateInstance(CLSID_WbemAdministrativeLocator,
NULL, CLSCTX_INPROC_SERVER
[解决办法]
CLSCTX_LOCAL_SERVER,
IID_IUnknown, (void**)&pWbemLocator) == S_OK)
{
IWbemServices *pWbemServices = NULL;
WideString wsNamespace = (L"root\\cimv2");
if(pWbemLocator->ConnectServer(wsNamespace, NULL, NULL, NULL,
0, NULL, NULL, &pWbemServices) == S_OK)
{
IEnumWbemClassObject *pEnumClassObject = NULL;
WideString wstrWQL = L"WQL";
WideString wstrQuery = WideString(L"Select * from ") + wstrClass;
if(pWbemServices->ExecQuery(wstrWQL, wstrQuery,
WBEM_FLAG_RETURN_IMMEDIATELY, NULL, &pEnumClassObject) == S_OK)
{
IWbemClassObject *pClassObject = NULL;
ULONG dwCount = 1, dwReturned;
if(pEnumClassObject->Reset() == S_OK)
{
int nEnumIdx = 0;
while(pEnumClassObject->Next(WBEM_INFINITE, dwCount,
&pClassObject, &dwReturned) == S_OK)
{
lpList->Add("---------------- ["+IntToStr(nEnumIdx)+"] -----------------");
SAFEARRAY *pvNames = NULL;
if(pClassObject->GetNames(NULL,
WBEM_FLAG_ALWAYS
[解决办法]
WBEM_MASK_CONDITION_ORIGIN,
NULL, &pvNames) == S_OK)
{
long lArrayLowBound, lArrayUpperBound;
SafeArrayGetLBound(pvNames, 1, &lArrayLowBound);
SafeArrayGetUBound(pvNames, 1, &lArrayUpperBound);
for(long lIndex=lArrayLowBound; lIndex<=lArrayUpperBound; lIndex++)
{
long lIndexTemp = lIndex;
wchar_t *wstrName = 0;
VARIANT vValue;
VariantInit(&vValue);
SafeArrayGetElement(pvNames, &lIndexTemp, &wstrName);
BSTR bsz = SysAllocString(wstrName);
HRESULT hRes = pClassObject->Get(bsz, 0, &vValue, NULL, 0);
SysFreeString(bsz);
if(hRes == S_OK)
{
AnsiString strTemp;
Variant v = *(Variant*)&vValue;
if(v.IsArray())
{
for(int i=v.ArrayLowBound(); i<=v.ArrayHighBound(); i++)
{
Variant a = v.GetElement(i);
if(!strTemp.IsEmpty())
strTemp += ", ";
strTemp += VarToStr(a);
}
}
else
{
strTemp = VarToStr(v);
}
lpList->Add(AnsiString(wstrName) + "=" + strTemp);
}
VariantClear(&vValue);
SysFreeString(wstrName);
}
}
if(pvNames)
SafeArrayDestroy(pvNames);
nEnumIdx++;
}
}
if(pClassObject)
pClassObject->Release();
}
if(pEnumClassObject)
pEnumClassObject->Release();
}
if(pWbemServices)
pWbemServices->Release();
}
if(pWbemLocator)
pWbemLocator->Release();
}
//---------------------------------------
// 通过 WIN32_bios 获取 BIOS 信息:
void __fastcall TForm1::Button1Click(TObject *Sender)
{
Memo1->Lines->Add("================== [WIN32_bios] =================");
GetWmiInfo(Memo1->Lines, "WIN32_bios");
Memo1->Lines->Add("");
}
//---------------------------------------
void __fastcall TForm1::FormDestroy(TObject *Sender)
{
CoUninitialize();
}