Starting WMI Methods Topic



Hi All,

I'm quite interested in the potential offered by WMI, but hate how the
internet seems geared towards only offering help for people using bloody VB.
So I want to start a thread here so we can work together to figure out how
to use methods. Of course if someone has already figured it out, please post
your code so we can all enjoy it! :)

Here's what I've got so far (though it generates a "Generic Failure" on the
"ExecuteMethod_" line :( ), what it should do is connect to a remote
computer, grab the process list and run the GetOwner method on the
explorer.exe process. The returned "User" parameter should indicate what
user is logged in locally the remote machine.

Sorry the code's sloppy, its only designed to get the function working, not
for looking good! :)

Thanks for any help you can offer, if I figure it out in the meantime I'll
post the solution here.

-Matt


------------------------------
unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, WbemScripting_TLB, StdCtrls, ActiveX;

type
TForm1 = class(TForm)
Button1: TButton;
ListBox1: TListBox;
procedure Button1Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
private
{ Private declarations }
wbem: TSWbemLocator;
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
var
pServices:ISWbemServices;
WbemObject:ISWbemObject;
ObjectSet: ISWbemObjectSet;
prop: ISWbemProperty;
meth: ISWbemMethod;
outp, outp2: ISWbemObject;
Enum:IEnumUnknown;
v:OleVariant;
fetched: Cardinal;
begin
try
pServices := wbem.ConnectServer('remotemachine',
'ROOT\CIMV2',
'remotemachine\administrator',
'adminpassword', '', '', 0, nil);

ObjectSet := pServices.ExecQuery('SELECT * FROM Win32_Process', 'WQL',
wbemFlagReturnImmediately and
wbemFlagForwardOnly, nil);

ObjectSet._NewEnum.QueryInterface(IEnumVARIANT, Enum);

while (Enum.Next(1, v, @fetched) = S_OK) do
begin

WbemObject := IUnknown(v) As ISWbemObject;

prop := WbemObject.Properties_.Item('Name', 0);
v := prop.Get_Value;

if (VarIsStr(v)) then
begin
if Lowercase(v) = 'explorer.exe' then
begin
meth := WbemObject.Methods_.Item('GetOwner', 0);

outp := meth.OutParameters.SpawnInstance_(0);
outp2 := outp.ExecMethod_('GetOwner', nil, 0, nil);

v := outp2.Properties_.Item('User', 0).Get_Value;
if VarIsStr(v) then
Form1.Caption := v
else
Form1.Caption := 'Not String';
end;
end;
end;
finally
wbem.Disconnect;
end;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
wbem := TSWbemLocator.Create(Form1);
end;

procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
wbem.Free;
end;

end.


.