🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.cppbuilder.nativeapi
Article #5563

Re: sourecode in BCB6 showing how to use WMI in WinXP

#5563
From: Luke Andy
Date: Mon, 08 Aug 2005 11:50
134 lines
5531 bytes
Dear "tinyabs",

Thank you very much for the source code.
Am I supposed to install some special library etc to my WinXP inorder to 
have the  "Wbemuuid.lib" ?

Once again, THANK YOU!.

Luke

>
> #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: <42f746fa@newsgroups.borland.com>
Path: arc1-ca.pugleaf.net!g2news1.google.com!news1.google.com!newsread.com!newsprint.newsread.com!news-out.newsfeeds.com!propagator3-LAX.newsfeeds.com!news-in.newsfeeds.com!news-in.superfeed.net!caladan.landsraad.net!newsgroups.borland.com!not-for-mail
References: <42f63ae8$1@newsgroups.borland.com> <42f6504f$1@newsgroups.borland.com> <42f72a8f$1@newsgroups.borland.com> <42f7334e@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: "Luke Andy" <landy@umail.com>
Newsgroups: borland.public.cppbuilder.nativeapi
References: <42f63ae8$1@newsgroups.borland.com> <42f6504f$1@newsgroups.borland.com> <42f72a8f$1@newsgroups.borland.com> <42f7334e@newsgroups.borland.com>
Subject: Re: sourecode in BCB6 showing how to use WMI in WinXP
Date: Mon, 8 Aug 2005 21:50:49 +1000
Lines: 136
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: 220.239.18.90
Message-ID: <42f746fa@newsgroups.borland.com>
X-Trace: newsgroups.borland.com 1123501818 220.239.18.90 (8 Aug 2005 04:50:18 -0700)
Path: arc1-ca.pugleaf.net!g2news1.google.com!news1.google.com!newsread.com!newsprint.newsread.com!news-out.newsfeeds.com!propagator3-LAX.newsfeeds.com!news-in.newsfeeds.com!news-in.superfeed.net!caladan.landsraad.net!newsgroups.borland.com!not-for-mail