Page 2 of 2 FirstFirst 12
Results 11 to 13 of 13

Thread: Process Management

  1. #11

    Process Management

    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
          &#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....

  2. #12

    Process Management

    why on earth will you attempt to close *all* running processes unless your beeing malicious

  3. #13

    Process Management

    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.
    Nicholas.
    <br />
    <br />Please join: http://holzstukka.proboards81.com

Page 2 of 2 FirstFirst 12

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •