D5:Src:Killing named processes from another app.

Here's a sample code I've written to terminate processes using D5.

I first found part of the code at http://pics.webset.de/faq08.htm, but
than adapted it to D5 as a console application.

I can easily be modified to use with command line options or even be
glued to a GUI.

Hope this code can help.
---------------------------------------------------------------------
program gg_clean;
{$APPTYPE CONSOLE}
uses Windows, Messages, SysUtils, Classes, Graphics, Controls,
StdCtrls,TlHelp32, ShellApi;

type TPIDlist = array[0..1000] of DWORD;

function EnumProcesses (pidList : PInteger; cb : Integer; var cbNeeded :
 Integer): boolean; stdcall; external 'psapi.dll';

function GetModuleBaseName (hProcess : longint; module : longint;
        BaseName : Pchar; size : Integer) : Integer; stdcall;external
        'psapi.dll' name 'GetModuleBaseNameA';

Function GetExitCodeProcess(hProcess:longint;var
        exitcode:longint):longint;
        stdcall; external 'kernel32.dll';

Function TerminateProcess(hProcess:longint;uExitCod:Longint): longint;
 stdcall;external 'kernel32.dll';

{
    This function creates an array containing all processes currently
    running and stores it in PIDList.

Quote
}

function GetProcessList(var PIDlist : TPIDlist): integer;

var cb, cbNeeded : integer;

begin
  cbNeeded := 0;
  cb := SizeOf(PIDlist);
  FillChar(PIDlist, cb, 0);
  if not EnumProcesses(@PIDlist, cb, cbNeeded) then
    cbNeeded := 0
  else
    cbNeeded := cbNeeded div SizeOf(DWord);
  Result := cbNeeded;
end;

{
    This is the main code of the application.
    It simply retrieves a process list using GetProcessList and
    terminates each process containing the strings "portmap" and "gltp".

    This part can be modified to work with a command line and
    terminate specific processes.

Quote
}

var
    numProcesses : integer; // How many processes
    ProcHandle   : longint; // A pointer to the process handle
    PIDList     : TPIDList; // The list of process ids
    i : Integer;            // A counter
    szName : PChar;         // A pointer to store the name of the process
    success,gotcha,exitcode:longint; // Some pointers
    inheritance:longbool;   // A flag
begin
  numProcesses := GetProcessList(PIDList); // Get the process list
  for i := 0 to numProcesses-1 do begin  // For each process in list
    inheritance := True; // Set inheritance to True
    // Get the handle by opening a channel to the process.
    ProcHandle := OpenProcess(PROCESS_ALL_ACCESS, inheritance,
PIDList[i]);
    if ProcHandle <> 0 then begin // If succeded, not 0 (null)
        try
            szName := StrAlloc(254); //Allocate space for PChar
            success := GetModuleBaseName(ProcHandle,0,szName, 254); //
Get the name of the process
            if  success > 0 then Begin // if returned size greater than 0
                // This is the part that could be modified.
                if (Pos('gltp',String(szName)) > 0) Then Begin
                    exitcode := 0;
                    // Get the exit code of the process
                    gotcha := GetExitCodeProcess(ProcHandle,exitcode);
                    // Terminate it
                    TerminateProcess(ProcHandle,exitcode);
                End;
                if (Pos('portmap.exe',String(szName)) > 0) Then Begin
                    exitcode := 0;
                    gotcha := GetExitCodeProcess(ProcHandle,exitcode);
                    TerminateProcess(ProcHandle,exitcode);
                End;

           end;
        finally
            StrDispose(szName);
            CloseHandle(ProcHandle)
        end;
    end;
  end;
End.