Skip to content
The Road to Delphi — Delphi, VCL, VCL Styles, UxTheme and WinAPI

WMI Delphi Code Creator

, ,

UPDATE

The new page of this project is hosted in Github.

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

[sourcecode language=”delphi” wraplines=”false”]
//——————————————————————————
// 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.
[/sourcecode]

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.

21 responses

  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

    1. Thanks for the comments, I will consider include some of your suggestions in future versions. for select multiples properties you must press ctrl + left click.

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

    1. Thanks for your comments.

  3. Love it, thank you sir!!

  4. 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.

    1. 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.

      1. Ok Rodirgo.
        Me parece fantástico.
        Realmente sí me gustaria ver cómo tratas algunas cuestiones, de todas formas si tienes previsto liberarla en breve puedo esperar. Como tú veas.

        Un saludo.

  5. […] hace un par de semanas que Rodrigo ha publicado en su Web en su web la aplicación WMI Delphi Code Creator. Viene a ser un generador de código delphi para acceder a las clases de WMI. La aplicación es […]

  6. Fernando Carvalho Avatar
    Fernando Carvalho

    Grande trabalho

  7. mark kc Avatar
    mark kc

    The link seems to be dead :(

  8. “The new page of this project is located here” point to same page. And download link is not working.

    1. Hi Alex, the link was fixed.

  9. Diego Di Guglielmo Avatar
    Diego Di Guglielmo

    The link seems to be dead :(

    1. Hi, the link was fixed.

  10. tornadohome Avatar
    tornadohome

    link to work

  11. Link are dead

  12. Alejandro G. Avatar
    Alejandro G.

    I’m looking for an example of how to pass a parameter that is an array to a WMI method. For example: win32-networkadapterconfiguration has a method “EnableDNS” some of the parameters are an array. Those array as they are passed to the method

    1. Try this https://theroadtodelphi.com/2011/10/30/wmi-tasks-using-delphi-%E2%80%93-networking/ check the sample code for the Win32_NetworkAdapterConfiguration class and the EnableStatic method.

Leave a Reply