Article View: borland.public.cppbuilder.nativeapi
Article #5562Re: sourecode in BCB6 showing how to use WMI in WinXP
From: tinyabs
Date: Mon, 08 Aug 2005 10:27
Date: Mon, 08 Aug 2005 10:27
123 lines
5144 bytes
5144 bytes
#include <vcl.h>
#include <ole2.h>
#include <iostream.h>
#include <conio.h>
#include <Wbemidl.h>
#pragma hdrstop
//---------------------------------------------------------------------------
#pragma link "Wbemuuid.lib"
#pragma argsused
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();
}
int main(int argc, char* argv[])
{
CoInitializeEx(0, COINIT_MULTITHREADED);
CoInitializeSecurity(NULL, -1, NULL, NULL,
RPC_C_AUTHN_LEVEL_CONNECT,
RPC_C_IMP_LEVEL_IMPERSONATE,
NULL, EOAC_NONE, 0
);
TStringList *list = new TStringList();
GetWmiInfo(list, "Win32_Processor");
for (int i = 0; i < list->Count; i++)
{
cout << list->Strings[i].c_str() << endl;
}
CoUninitialize();
getch();
return 0;
}
Technical Details
Message-ID:
<42f7334e@newsgroups.borland.com>Path:
arc1-ca.pugleaf.net!g2news1.google.com!news3.google.com!news.glorb.com!feeder.enertel.nl!nntpfeed-01.ops.asmr-01.energis-idc.net!news-out.nntp.be!propagator2-sterling.newsfeeds.com!in.nntp.be!caladan.landsraad.net!newsgroups.borland.com!not-for-mailReferences:
<42f63ae8$1@newsgroups.borland.com> <42f6504f$1@newsgroups.borland.com> <42f72a8f$1@newsgroups.borland.com>Show Full Headers
Complete Article Headers
X-Google-Thread: 10cc67,eef40730daa9a1f6 X-Google-Attributes: gid10cc67,public X-Google-Language: ENGLISH,ASCII-7-bit From: "tinyabs" <nospam@me.com> Newsgroups: borland.public.cppbuilder.nativeapi References: <42f63ae8$1@newsgroups.borland.com> <42f6504f$1@newsgroups.borland.com> <42f72a8f$1@newsgroups.borland.com> Subject: Re: sourecode in BCB6 showing how to use WMI in WinXP Date: Mon, 8 Aug 2005 18:27:02 +0800 Lines: 125 X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 6.00.2900.2180 X-RFC2646: Format=Flowed; Response X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2180 NNTP-Posting-Host: 222.165.17.127 Message-ID: <42f7334e@newsgroups.borland.com> X-Trace: newsgroups.borland.com 1123496782 222.165.17.127 (8 Aug 2005 03:26:22 -0700) Path: arc1-ca.pugleaf.net!g2news1.google.com!news3.google.com!news.glorb.com!feeder.enertel.nl!nntpfeed-01.ops.asmr-01.energis-idc.net!news-out.nntp.be!propagator2-sterling.newsfeeds.com!in.nntp.be!caladan.landsraad.net!newsgroups.borland.com!not-for-mail