| How to terminate a process on a local or remote host? |
|
This page contains the two Delphi examples of how to kill processes.
They both produce the same result and differ only in a number of lines of code.
The first example is based on
TWmiQuery,
TWmiMethod and
TWmiConnection components from
WmiSet component collection. TWmiQuery is used
to retrieve the Win32_Process objects. TWmiMethod component executes
methods of the found object. Look at
MSDN web site
for more information about Win32_Process class.
The second example utilizes TWmiProcessControl component from WmiSet component collection. This component is specialized for process management, while TWmiQuery and TWmiMethod are the universal components that may be used for many purposes. The source code below creates and destroys the components on the fly, so these functions may be used in UI and console type applications. |
How to terminate a process with TWmiMethod?
Download
|
// this function terminates specified process.
// It may be used for local or remote host. When used for local host
// TargetHost, UserName, Password parameters must be empty.
// For remote computer the parameters must be:
// TargetHost: name or IP address of the remote host, like '10.8.36.54'
// UserName: name of the user, may include domain, like 'MOON\Administrator';
// Password: the user's password.
// AProcess: either a name (as returned by process_enumerate_query.exe example)
// or a handle of process (integer value);
procedure TerminateProcess(TargetHost, UserName, Password, AProcess: string);
var
Connection: TWmiConnection;
Query: TWmiQuery;
Method: TWmiMethod;
vProcessFound: boolean;
s: string;
begin
Query := TWmiQuery.Create(nil);
Method := TWmiMethod.Create(nil);
Connection := TWmiConnection.Create(nil);
try
Query.Connection := Connection;
Query.WQL.Text := 'select * from Win32_Process';
Method.WmiObjectSource := Query;
Connection.MachineName := TargetHost;
Connection.Credentials.UserName := UserName;
Connection.Credentials.Password := Password;
// exception may happen on Win9x, WinNT if WMI core is not installed;
// The provided credentials may also be invalid.
Connection.Connected := true;
Query.Open;
if (StrToIntDef(AProcess, -1) <> -1) then
vProcessFound := Query.Locate('Handle', AProcess, [])
else vProcessFound := Query.Locate('Name', AProcess, []);
if vProcessFound then
begin
Method.WmiMethodName := 'Terminate';
if Method.Execute <> 0 then
begin
s := Method.LastWmiErrorDescription;
if s = '' then s := IntToStr(Method.LastWmiError);
raise Exception.Create(s);
end;
end else
begin
raise Exception.Create('Specified process not found.');
end;
finally
Method.Free;
Query.Free;
Connection.Free;
end;
end;
|
How to terminate a process with TWmiProcessControl?
Download
|
// this function terminates specified process.
// It may be used for local or remote host. When used for local host
// TargetHost, UserName, Password parameters must be empty.
// For remote computer the parameters must be:
// TargetHost: name or IP address of the remote host, like '10.8.36.54'
// UserName: name of the user, may include domain, like 'MOON\Administrator';
// Password: the user's password.
// AProcess: either a name (as returned by process_enumerate_query.exe example)
// or a handle of process (integer value);
procedure TerminateProcess(TargetHost, UserName, Password, AProcess: string);
var
ProcessControl: TWmiProcessControl;
i: integer;
vProcessHandle: cardinal;
vProcess: TWmiProcess;
begin
ProcessControl := TWmiProcessControl.Create(nil);
try
ProcessControl.MachineName := TargetHost;
ProcessControl.Credentials.UserName := UserName;
ProcessControl.Credentials.Password := Password;
// exception may happen on Win9x, WinNT if WMI core is not installed;
// The provided credentials may also be invalid.
ProcessControl.Active := true;
vProcessHandle := StrToIntDef(AProcess, -1);
for i := 0 to ProcessControl.Processes.Count - 1 do
begin
vProcess := ProcessControl.Processes[i];
if (vProcessHandle = vProcess.Handle) or
(AProcess = vProcess.Name) then
begin
vProcess.Terminate(1);
Exit;
end;
end;
raise Exception.Create('Specified process not found.');
finally
ProcessControl.Free;
end;
end;
|