Tuesday, June 7, 2011

WMI Coding Example Using Borland C Builder

This code snippet is one of many that can be found at:

http://www.planetmps.com/

//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
 : TForm(Owner)
{
}
String GetValue(String N, String tempstr)
{
  int pos = 0;
  String Ne = N + " = ";
  pos = AnsiPos(Ne, tempstr);
  if(pos > 0)
  {
 pos = pos + Ne.Length();
 int l = tempstr.Length();
 tempstr = tempstr.SubString(pos, l);
 pos = AnsiPos(";", tempstr);
 tempstr = tempstr.SubString(2, pos -3);
 return tempstr;
  }
  else
  {
 return 0x20 + 0x20;
  }
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormCreate(TObject *Sender)
{
 LPUNKNOWN punkEnum;
 LPUNKNOWN punkEnum1;
 LPUNKNOWN pEnum;
 IEnumVARIANT * mocEnum=NULL;
 IEnumVARIANT * mocEnum1=NULL;
 IEnumVARIANT * propEnum=NULL;
 VARIANT obj;
 VARIANT obj1;
 VariantInit(&obj);
 int nIndex;
 unsigned long c;
 unsigned long cc;
 String mystr;
 VARIANT prop;
 VariantInit(&prop);
 int nIndex1;
 unsigned long cc1;
 VariantInit(&obj1);
 int nIndex2;
 unsigned long cc2;
 Variant l = Variant::CreateObject("Wbemscripting.SwbemLocator");
 Variant svc = l.OleFunction("ConnectServer", ".", "root\\cimv2");
 Variant objs = svc.OleFunction("instancesOf", "Win32_Process");
 punkEnum = objs.OlePropertyGet("_NewEnum");
 punkEnum->QueryInterface(IID_IEnumVARIANT, (LPVOID far*)&mocEnum);
 mocEnum->AddRef();
 punkEnum->Release();
 while(mocEnum->Next(1, &obj, &c) == S_OK)
 {
   Variant obj1 = (Variant(obj.pdispVal));
   Variant PropSet = obj1.OlePropertyGet("Properties_");
   pEnum = PropSet.OlePropertyGet("_NewEnum");
   IEnumVARIANT * propEnum=NULL;
   tagVARIANT tprop;
   unsigned long d = 0;
   if(pEnum->QueryInterface(IID_IEnumVARIANT, (LPVOID far*)&propEnum) == S_OK)
   {
  while(propEnum->Next(1, &tprop, &d) == S_OK)
  {
    String Name = Variant(tprop).OlePropertyGet("Name");
    String OT = obj1.OleFunction("GetObjectText_");
    mystr = mystr + Name + ": " + GetValue(Name, OT) + "\n";
  }
  Variant wsh = Variant::CreateObject("WScript.Shell");
  wsh.OleFunction("Popup", OleVariant(mystr));
  mystr="";
  break;
   }
 }
}

No comments:

Post a Comment