Online-Admin.com |
 |
Menu |
 |
 |
Quick search |
 |
 |
New version notify |
 |
|
 |
TWmiMethod component |
 |
TWmiMethod is a part of
WmiSet Component Collection for Delphi, C++Builder.
It executes the methods of WMI objects. This component cooperates with
TWmiQuery and
TWmiConnection to retrieve the objects whose
methods are to be called. The component provides the following
capabilities:
- Retrieves a WMI object from TWmiQuery or TWmiConnection;
- Retrieves a list of methods supported by the WMI object;
- Automatically detects the input parameters of the method;
- Allows to access parameters by name (use ParamByName method);
- Automatically detects and requests the privileges required to
execute the method;
- Executes static and dynamic methods of objects;
- Automatically creates the output parameters that keep the
results of method execution;
- Analyzes the output parameters and retrieves the error code and
description.
- Retrieves the WMI method qualifiers that report the various
information about the object's method: description,
required privileges, possible return value etc.
|
|
Code Example: TWmiMethod and TWmiQuery
|
This 40-line long program is a Delphi 6 example that shows how to
use TWmiMethod and TWmiQuery to start the service. It properly handles
the errors. The name of the service to start should be the first
command line parameter.
program StartService;
{$APPTYPE CONSOLE}
uses
Variants, SysUtils, WmiDataSet,
WmiConnection, WmiMethod;
var
vQuery: TWmiQuery;
vConnection: TWmiConnection;
vMethod: TWmiMethod;
i: integer;
vName: string;
begin
if ParamCount > 0 then vName := ParamStr(1) else Exit;
vQuery := TWmiQuery.Create(nil);
vConnection := TWmiConnection.Create(nil);
vMethod := TWmiMethod.Create(nil);
try
vQuery.Connection := vConnection;
vMethod.WmiObjectSource := vQuery;
vQuery.WQL.Text := 'select * from Win32_Service';
vQuery.Open;
if vQuery.Locate('Name', vName, []) then
begin
vMethod.WmiMethodName := 'StartService';
if vMethod.Execute = 0 then writeln('Ok')
else writeln(vMethod.LastWmiErrorDescription);
end else writeln('Service '+ vName + ' not found.');
finally
vMethod.Free;
vQuery.Free;
vConnection.Free;
end;
end.
|
|
Code Example: Using ParamByName.
|
This piece of Delphi code shows how to
assign the input parameters for a static WMI method call.
This example schedules a task for execution. It assumes
that TWmiConnection and TWmiMethod components are
dropped on the form at design time, properly linked and
connected to the destination computer.
procedure TForm1.CreateJob(Command: string;
StartTime: TDateTime);
begin
WmiConnection1.ObjectPath := 'Win32_ScheduledJob';
with WmiMethod1 do
begin
WmiMethodName := 'Create';
with InParams do
begin
ParamByName('Command').AsString := Command;
ParamByName('StartTime').AsTime := StartTime;
ParamByName('RunRepeatedly').AsBoolean := true;
ParamByName('InteractWithDesktop').AsBoolean := true;
end;
if Execute <> 0 then
raise Exception.Create(LastWmiErrorDescription);
end;
end;
|
|
More examples.
|
WmiSet component collection comes with a number of examples
that are using TWmiMethod component. Look in the
examples\WmiMethods directory. Click on the image do download
the demo application that can start, stop, pause, resume services
on a local or remote computer.
|
The example project below changes the IP protocol settings on the
local or network computers. It can set the IP address to be static, or
enable DHCP. The Delphi source code is included. C++Builder version
is also available. Click on the image to start download.
|
|