PDA

View Full Version : Process Management



Voltrox
12-09-2006, 01:13 AM
Hello,

I have been trying to figure out how to Terminate a process in Delphi, and I saw shockingly long of code which i don't think I can remember, and i don't even understand what it means.

Could someone please show me the simplest way (but not a bad way) in which i can terminate a process in Delphi?

and also start processes and check to see if they are running, and how to get a list of running processes.

Thank you.

tanffn
12-09-2006, 08:51 AM
Halt
Application.Terminate

Goto: http://www.swissdelphicenter.ch/torry/

Voltrox
13-09-2006, 08:24 PM
Application.Terminate only terminates my application...

AthenaOfDelphi
13-09-2006, 08:47 PM
In that case, the code you saw was probably long and complicated by a requirement to obtain the necessary privileges (I think) to be able to shut down other processes.

My question is why do you want to terminate other processes?

Robert Kosek
13-09-2006, 08:54 PM
The JCL has code functions to terminate another process, and retrieve a list of processes if that's what you're after. I messed with it once a few years back.

lordzero
14-09-2006, 03:28 AM
Hello,

I have been trying to figure out how to Terminate a process in Delphi, and I saw shockingly long of code which i don't think I can remember, and i don't even understand what it means.

Could someone please show me the simplest way (but not a bad way) in which i can terminate a process in Delphi?

and also start processes and check to see if they are running, and how to get a list of running processes.

Thank you.

hello

How to find processes and how to kill one?

see this http://www.pscode.com/vb/scripts/ShowZip.asp?lngWId=7&lngCodeId=756&strZipAccessCode=tp%2FL7562208

greets

Voltrox
14-09-2006, 03:41 AM
I want to know how to terminate processes because i'm making a suspicious process monitor, which will automatically terminate malicious processes.

Fran
14-09-2006, 10:57 PM
var
startup:_startupinfoa;
pi:_process_information;
begin
// Start application
zeromemory(@startup,sizeof(startup));
startup.cb:=sizeof(startup);

createprocess(nil,'program name and parameters',nil,nil,false,0,nil,nil,startup,pi);

// wait program terminates
waitforsingleobject(pi.hProcess,infinite);


To terminate a task, i suppose terminateprocess would be used. To enumerate the running tasks, i don't know... never had to use any of those 2 features. Go to http://msdn.microsoft.com/library look up terminateprocess and click on the link at the bottom of the page that lists all process functions, you should find what you're looking for there.

... just quickly checked, and it seems there's a function named enumprocesses... that should be what you want. As for the security thing, you likely don't have to bother with this if you're on your home machine since you should have all priviledges. If you're at work however... you might have to.

Setharian
15-09-2006, 07:44 AM
to enumerate all running processes, use EnumProcesses in PsApi.pas....just create a big longword array (it's not predictable how many processes are running at time when you invoke it)....it will fill the array with process ids.... to obtain a handle for the process, call OpenProcess....then you can do whatever you like with the process....

Voltrox
15-09-2006, 11:07 PM
To use PSapi I would have to say "uses PSapi" ?

but after using the EnumProcess, how do I terminate and so on?

Setharian
16-09-2006, 06:21 PM
yes, add "PsApi" in your uses list....

an example code...


unit ProcessTerminator;

interface

uses
Windows,
PsApi,
SysUtils;

procedure TerminateProcesses;

implementation

function EnumerateProcesses(const ProcessArray: array of Longword): Longword;
begin
EnumProcesses(@ProcessArray[0], Length(ProcessArray) * SizeOf(Longword), Result);
Result := Result div SizeOf(Longword); { Result holds the number of processes enumerated }
end;

const
sNoTermination = 'Process with ID %d could not be terminated. Reason: %s';

procedure ReportTerminationFailure(ProcessId: Longword; ErrorCode: Longword);
begin
Writeln(Format(sNoTermination, [ProcessId, SysErrorMessage(ErrorCode)]));
end;

function DoTerminateProcess(Process: THandle): Boolean;
begin
{ You may add here any additional stuff you want to do with the process handle,
check if you want to terminate it or not, etc. }
Result := TerminateProcess(Process, 0);
end;

procedure TerminateProcesses;
var
ProcessIds: array[0..1023] of Longword; { 1024 processes should be enough for any system :) }
Count: Integer;
I: Integer;
ProcessHandle: THandle;
begin
{ Optional, but good for debugging - the array contains random data at start }
FillChar(ProcessIds[0], SizeOf(ProcessIds), 0);
Count := EnumerateProcesses(ProcessIds);
for I := 0 to Count -1 do
begin
ProcessHandle := OpenProcess(PROCESS_QUERY_INFORMATION or PROCESS_TERMINATE,
False, ProcessIds[I]);
if ProcessHandle <> 0 then
begin
&#123; We have a valid handle &#125;
if not DoTerminateProcess&#40;ProcessHandle&#41; then
begin
&#123; Termination failed &#125;
ReportTerminationFailure&#40;ProcessIds&#91;I&#93;, GetLastError&#41;;
end;
CloseHandle&#40;ProcessHandle&#41;;
end else
begin
&#123; Nope, we cannot terminate this process &#125;
ReportTerminationFailure&#40;ProcessIds&#91;I&#93;, GetLastError&#41;;
end;
end;
end;

end.

calling the TerminateThreads procedure would shutdown all running processes...at least all for which it is possible....

PS: the constants used for OpenProcess (PROCESS_QUERY_INFORMATION, PROCESS_TERMINATE) can be replaced by your needs...for all access rights to the handle, use PROCESS_ALL_ACCESS, but there's a possibility more calls will fail because of not having needed priviledges....

tux
16-09-2006, 07:55 PM
why on earth will you attempt to close *all* running processes unless your beeing malicious???

Voltrox
17-09-2006, 12:24 AM
Thank you. :)

Do you know of a realy godo tutorial that would teach me all about managing process and retrieveing information about them?

i'm not terminating all processes, i'm only looking out for specific malicious ones and terminate those if they are found running.

t's simply a monitor.