TWmiConnection is a part of
WmiSet Component Collection for Delphi, C++Builder.
This VCL component serves as a connection for
TWmiQuery component.
It may connect any number of TWmiQuery components to a Windows Management
Instrumentation (WMI) service on the remote computer. The component provides
the following connection capabilities:
Connect to a local or remote computer using NetBIOS name
or IP address;
Select WMI name space to connect to.
May use credentials of currently logged user, or alternative
user name and password.
List WMI classes registered in a name space on a remote computer.
This is important when working in the environment where
different operating system are installed on the network
computers.
Generate various events when the connection state changes.
Code Example: complete program
This example shows how to use TWmiConnection and TWmiQuery
in a Delphi console application. It lists the user accounts
registered on a local computer. For demo purpose, this code
has minimum error checks.
program ConsoleWmiQuery;
{$APPTYPE CONSOLE}
uses
SysUtils, WmiDataSet, WmiConnection;
var
vQuery: TWmiQuery;
vConnection: TWmiConnection;
i: integer;
begin
vQuery := TWmiQuery.Create(nil);
vConnection := TWmiConnection.Create(nil);
try
vQuery.Connection := vConnection;
vQuery.WQL.Text := 'select * from Win32_Account';
vQuery.Open;
whilenot vQuery.EOF dobeginfor i := 0 to vQuery.Fields.Count - 1 dobegin
write(vQuery.Fields[i].FieldName+ ': ');
writeln(vQuery.Fields[i].AsString);
end;
vQuery.Next;
writeln;
end;finally
vQuery.Free;
vConnection.Free;
end;end.
Code Example: advanced features.
This Delphi code shows how to connect to a remote computers
using alternative credentials. It then retrieves the existing
classes in a provided name space and prints them out.
program ConsoleWmiConnection;
{$APPTYPE CONSOLE}
uses
SysUtils, WmiConnection, Classes;
var
vConnection: TWmiConnection;
vList: TStringList;
i: integer;
begin
vConnection := TWmiConnection.Create(nil);
vList := TStringList.Create;
with vConnection dotry
MachineName := 'moonserv';
Credentials.UserName := 'moon\jhonsmith';
Credentials.Password := 'password';
NameSpace := 'root\default';
Connected := true;
ListClasses(vList);
for i := 0 to vList.Count - 1 do
writeln(vList[i]);
finally
vConnection.Free;
vList.Free;
end;end.