C++ Builder WMI 编程问题
按照网络上搜索的方法,源码如下:
// ---------------------------------------
#include <comdef.h>
#include <wbemidl.h>
#include <vcl.h>
#pragma hdrstop
#include "WMI.h"
// ---------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
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();
}
// -----------
// ---------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner) {
}
// ---------------------------------------
void __fastcall TForm1::FormCreate(TObject *Sender) {
CoInitialize(NULL);
CoInitializeSecurity(NULL, -1, NULL, NULL, RPC_C_AUTHN_LEVEL_PKT,
RPC_C_IMP_LEVEL_IMPERSONATE, NULL, EOAC_NONE, 0);
}
// ---------------------------------------
void __fastcall TForm1::FormClose(TObject *Sender, TCloseAction &Action) {
CoUninitialize();
}
// ---------------------------------------
编译出错:
[BCC32 Error] WMI.cpp(22): E2034 Cannot convert 'WideString' to 'wchar_t *'
Full parser context
WMI.cpp(14): parsing: void GetWmiInfo(TStrings *,WideString)
[BCC32 Error] WMI.cpp(22): E2342 Type mismatch in parameter 'strNetworkResource' (wanted 'wchar_t * const', got 'WideString')
Full parser context
WMI.cpp(14): parsing: void GetWmiInfo(TStrings *,WideString)
[BCC32 Error] WMI.cpp(27): E2034 Cannot convert 'WideString' to 'wchar_t *'
Full parser context
WMI.cpp(14): parsing: void GetWmiInfo(TStrings *,WideString)
[BCC32 Error] WMI.cpp(27): E2342 Type mismatch in parameter 'strQueryLanguage' (wanted 'wchar_t * const', got 'WideString')
Full parser context
WMI.cpp(14): parsing: void GetWmiInfo(TStrings *,WideString)
请高手帮忙解决。另本人只有8分,无法给分。怎么办?
[解决办法]
将:
if (pWbemLocator->ConnectServer(wsNamespace, NULL, NULL, NULL, 0, NULL,NULL, &pWbemServices) == S_OK)
改成:
if (pWbemLocator->ConnectServer(wsNamespace.w_str(), NULL, NULL, NULL, 0, NULL,NULL, &pWbemServices) == S_OK)
再将:
if (pWbemServices->ExecQuery(wsWQL, wsQuery,WBEM_FLAG_RETURN_IMMEDIATELY, NULL, &pEnumClassObject) == S_OK)
改成:
if (pWbemServices->ExecQuery(wsWQL.w_str(), wsQuery,WBEM_FLAG_RETURN_IMMEDIATELY, NULL, &pEnumClassObject) == S_OK)
试试?