The Road to Delphi – a Blog about programming

Delphi – Free Pascal – Oxygene

WMI Delphi Code Creator

12 Comments

UPDATE

The new page of this project is located here.

Introducing the WMI Delphi Code Creator © tool allows you to generate delphi code that uses WMI to complete a management task such as querying for wmi data.

This freeware tool is inspired by the  WMI Code Creator.

Features

  • Create full delphi console project
  • Create a 100% functional delphi procedure wich encapsulates the logic to retrieve WMI information
  • Full access to metadata of any WMI Class registered in the system
  • direct link to MSDN web page containig a description of the WMI Class

Todo

  • support fo call WMI methods.
  • support for WMI events
  • remote WMI support
  • Support more programming languages (delphi-prism, C++ builder)
  • Dynamic execution of generated code.
  • and more….

Used tools for write this application

Recommended Links about WMI

Screenshots

This slideshow requires JavaScript.

sample code generated by the application

//------------------------------------------------------------------------------
//     This code was generated by the Wmi Delphi Code Creator
//     Version: 1.0.0.1
//
//
//
//     LIABILITY DISCLAIMER
//     THIS GENERATED CODE IS DISTRIBUTED "AS IS". NO WARRANTY OF ANY KIND IS EXPRESSED OR IMPLIED.
//     YOU USE IT AT YOUR OWN RISK. THE AUTHOR NOT WILL BE LIABLE FOR DATA LOSS,
//     DAMAGES AND LOSS OF PROFITS OR ANY OTHER KIND OF LOSS WHILE USING OR MISUSING THIS CODE.
//
//
//------------------------------------------------------------------------------
program GetWMI_Info;

{$APPTYPE CONSOLE}

uses
  SysUtils,
  ActiveX,
  ComObj,
  Variants;

function VarArrayToStr(const vArray: variant): string;

    function _VarToStr(const V: variant): string;
    var
    Vt: integer;
    begin
    Vt := VarType(V);
        case Vt of
          varSmallint,
          varInteger  : Result := IntToStr(integer(V));
          varSingle,
          varDouble,
          varCurrency : Result := FloatToStr(Double(V));
          varDate     : Result := VarToStr(V);
          varOleStr   : Result := WideString(V);
          varBoolean  : Result := VarToStr(V);
          varVariant  : Result := VarToStr(Variant(V));
          varByte     : Result := char(byte(V));
          varString   : Result := String(V);
          varArray    : Result := VarArrayToStr(Variant(V));
        end;
    end;

var
i : integer;
begin
    Result := '[';
     if (VarType(vArray) and VarArray)=0 then
       Result := _VarToStr(vArray)
    else
    for i := VarArrayLowBound(vArray, 1) to VarArrayHighBound(vArray, 1) do
     if i=VarArrayLowBound(vArray, 1)  then
      Result := Result+_VarToStr(vArray[i])
     else
      Result := Result+'|'+_VarToStr(vArray[i]);

    Result:=Result+']';
end;

function VarStrNull(const V:OleVariant):string; //avoid problems with null strings
begin
  Result:='Null';
  if not VarIsNull(V) then
  begin
    if VarIsArray(V) then
       Result:=VarArrayToStr(V)
    else
    Result:=VarToStr(V);
  end;
end;

function GetWMIObject(const objectName: String): IDispatch; //create the Wmi instance
var
  chEaten: Integer;
  BindCtx: IBindCtx;
  Moniker: IMoniker;
begin
  OleCheck(CreateBindCtx(0, bindCtx));
  OleCheck(MkParseDisplayName(BindCtx, StringToOleStr(objectName), chEaten, Moniker));
  OleCheck(Moniker.BindToObject(BindCtx, nil, IDispatch, Result));
end;

//
procedure  GetWin32_ShareInfo;
var
  objWMIService : OLEVariant;
  colItems      : OLEVariant;
  colItem       : OLEVariant;
  oEnum         : IEnumvariant;
  iValue        : LongWord;
begin;
  objWMIService := GetWMIObject('winmgmts:\\localhost\root\CIMV2');
  colItems      := objWMIService.ExecQuery('SELECT * FROM Win32_Share','WQL',0);
  oEnum         := IUnknown(colItems._NewEnum) as IEnumVariant;
  while oEnum.Next(1, colItem, iValue) = 0 do
  begin
    Writeln(Format('AccessMask                     %s',[VarStrNull(colItem.AccessMask)]));
    Writeln(Format('AllowMaximum                   %s',[VarStrNull(colItem.AllowMaximum)]));
    Writeln(Format('Caption                        %s',[VarStrNull(colItem.Caption)]));
    Writeln(Format('Description                    %s',[VarStrNull(colItem.Description)]));
    Writeln(Format('InstallDate                    %s',[VarStrNull(colItem.InstallDate)]));
    Writeln(Format('MaximumAllowed                 %s',[VarStrNull(colItem.MaximumAllowed)]));
    Writeln(Format('Name                           %s',[VarStrNull(colItem.Name)]));
    Writeln(Format('Path                           %s',[VarStrNull(colItem.Path)]));
    Writeln(Format('Status                         %s',[VarStrNull(colItem.Status)]));
    Writeln(Format('Type                           %s',[VarStrNull(colItem.Type)]));
    Writeln('');
  end;
end;

begin
 try
    CoInitialize(nil);
    try
      GetWin32_ShareInfo;
      Readln;
    finally
    CoUninitialize;
    end;
 except
    on E:Exception do
    begin
        Writeln(E.Classname, ':', E.Message);
        Readln;
    end;
  end;
end.

Download from here.

Notice for Windows Vista, Windows 7 and Windows 2008  users, this application requires  run as administrator.

All your comments, suggestions and criticisms are very welcome.

About these ads

Author: Rodrigo

Just another Delphi guy.

12 thoughts on “WMI Delphi Code Creator

  1. Hi,

    Such a great tool! Thanks for sharing it with us.

    I played with it a little, and to me, a few tweaks would make it even a better tool. First of all, save button always saves the console app. It is not a big deal, but I think it is better that you save the console app when console tab is active, and a PAS file when Delphi Procedure tab is active.

    Second, it would have been better if user could have selected the properties he/she wants. Now he/she has to either select one or all of them.

    Third, I hope you add an option to change code viewer style. The current style somehow bothers my eyes.

    Once again, thanks for the nice tool.

    Best Regards,
    Ali

  2. This is so freaking cool!!! Great work!

  3. Hola Rodrigo.
    Tiene muy buena pinta la utilidad. Muy útil y muy bien pulida en todos los aspectos.

    Personalmente me gusta el tema de WMI y he trabajado algo con ella. ¿Es posible acceder al código fuente o estás pensando em comercializarla? Hay algunos aspectos que me gustaría saber cómo los has resuelto.

    Gran trabajo.

    Un saludo.

    _______________________
    Germán.

    • Hola German, gracias por tus comentarios. en primera instancia quiero publicar el codigo fuente completo y hacerla open-source, pero antes de que ocurra eso quiero estabilizar bien la aplicacion y terminar todas las funcionalidades pendientes. si deseas por el momento te puedo enviar la libreria que escribi para manejar la WMI, usando solo late-binding.

  4. Pingback: /*Prog*/ Delphi-Neftalí /*finProg*/ » Una quincena más (19/11/2010)

  5. Grande trabalho

  6. The link seems to be dead :(

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 652 other followers