yes, add "PsApi" in your uses list....
an example code...
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
{ We have a valid handle }
if not DoTerminateProcess(ProcessHandle) then
begin
{ Termination failed }
ReportTerminationFailure(ProcessIds[I], GetLastError);
end;
CloseHandle(ProcessHandle);
end else
begin
{ Nope, we cannot terminate this process }
ReportTerminationFailure(ProcessIds[I], GetLastError);
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....
Bookmarks