Terminating processes with delphi

Windows processes can be terminated from a delphi application using Win32 API calls. To terminate processes not owned by the current user the SE_DEBUG_NAME privilege must be set for the current process.

All sample code must include unit TlHelp32.

Sample code for activating SE_DEBUG_NAME privilege

function NTSetPrivilege(sPrivilege: string; bEnabled: Boolean): Boolean;
var
  hToken: THandle;
  TokenPriv: TOKEN_PRIVILEGES;
  PrevTokenPriv: TOKEN_PRIVILEGES;
  ReturnLength: Cardinal;
begin
  Result := True;
  // Only for Windows NT/2000/XP and later.
  if not (Win32Platform = VER_PLATFORM_WIN32_NT) then
    Exit;

  Result := False;

  // obtain the processes token
  if OpenProcessToken(GetCurrentProcess(),TOKEN_ADJUST_PRIVILEGES or TOKEN_QUERY, hToken) then
  begin
    try
      // Get the locally unique identifier (LUID) .
      if LookupPrivilegeValue(nil, PChar(sPrivilege),TokenPriv.Privileges[0].Luid) then
      begin
        TokenPriv.PrivilegeCount := 1; // one privilege to set

        case bEnabled of
          True: TokenPriv.Privileges[0].Attributes  := SE_PRIVILEGE_ENABLED;
          False: TokenPriv.Privileges[0].Attributes := 0;
        end;

        ReturnLength := 0; // replaces a var parameter
        PrevTokenPriv := TokenPriv;

        // enable or disable the privilege
        AdjustTokenPrivileges(hToken, False, TokenPriv, SizeOf(PrevTokenPriv),PrevTokenPriv, ReturnLength);
      end;
    finally
      CloseHandle(hToken);
    end;
  end;

  // test the return value of AdjustTokenPrivileges.
  Result := GetLastError = ERROR_SUCCESS;
  if not Result then
    raise Exception.Create(SysErrorMessage(GetLastError));
end;
<span style="text-decoration: underline;">Sample code for terminating processes by name of executable file</span>
<pre lang="delphi">procedure Killprocess(Name:String);
var
  PEHandle,hproc: cardinal;
  PE: ProcessEntry32;
begin
  NTSetPrivilege(SE_DEBUG_NAME,True);
  PEHandle := CreateTOOLHelp32Snapshot(TH32cs_Snapprocess,0);
  if PEHandle <> Invalid_Handle_Value then
  begin
    PE.dwSize := Sizeof(ProcessEntry32);
    Process32first(PEHandle,PE);

    repeat
      if Lowercase(PE.szExeFile) = Lowercase(Pchar(Name)) then
      begin
        hproc := openprocess(Process_Terminate,false,pe.th32ProcessID);
        TerminateProcess(hproc,0);
        closehandle(hproc);
      end;
    until Process32next(PEHandle,PE)=false;
  end;
  closehandle(PEHandle);
end;

Delphi 7 components

Delphi Library path

$(DELPHI)\Components\Rx\Lib
$(DELPHI)\Components\LotusNotes.bak\lib
$(DELPHI)\Components\SynEdit\Lib
$(DELPHI)\Components\Tbx2k\Lib
$(DELPHI)\Components\ehs\Lib
$(DELPHI)\Components\php4delphi
$(DELPHI)\Components\kalender\lib
$(DELPHI)\Components\InstantObjects\lib
$(DELPHI)\Components\Sysinfo\D7
$(DELPHI)\Components\vnc
C:\Programme\SqlDir\D7
$(DELPHI)\Components\FastReport\LibD7
$(DELPHI)\Components\FastReport252\Lib
$(DELPHI)\Components\PsFr\Lib
$(DELPHI)\Components\winapi\Lib
$(DELPHI)\Components\GLScene\Lib
$(DELPHI)\Components\dunit\src
$(DELPHI)\Components\dunit\Contrib\DUnitWizard\Lib
$(DELPHI)\Components\DxPack\Lib
$(DELPHI)\Components\Cooltray\Lib
$(DELPHI)\Components\Crw\Lib
$(DELPHI)\Components\mxProtector\Lib
$(DELPHI)\Components\Pngimage\lib
$(DELPHI)\Components\mxNativeExcel\Lib
$(DELPHI)\Components\XP Theme Manager\lib
$(DELPHI)\Components\zeosdbo\packages\delphi7\build
$(DELPHI)\Components\FibPlus61\Lib
C:\Progra~1\Borland\jcl\lib\D7
C:\Programme\dxgettext\D7
$(DELPHI)\Source\ToolsAPI
$(DELPHI)\Components\ics\lib
$(DELPHI)\Components\midware\lib
$(DELPHI)\Components\Passcript\Dcu\D7
$(DELPHI)\Components\Passcript\Source
$(DELPHI)\Components\
$(DELPHI)\Components\ubmock\lib
$(DELPHI)\Components\dimime\lib
$(DELPHI)\Components\xmlparser\lib
$(DELPHI)\Components\Formedit
$(DELPHI)\Components\extlib\sources
$(DELPHI)\Components\ScripterStudio\Lib
$(DELPHI)\Components\SQLMemTable\Lib\Delphi7
$(DELPHI)\Components\cipher\source
$(DELPHI)\Components\FastMM\Lib
$(DELPHI)\Components\Orcka\Component Suite\Lib
$(DELPHI)\Components\mxPluginLoader\Lib
$(DELPHI)\Components\Greatis\Gup\Lib
$(DELPHI)\Components\SUIPack\Lib
$(DELPHI)\Components\vclskin\d7
$(DELPHI)\Components\SuiSkin\Source\Delphi7
$(DELPHI)\Components\tb2merge\Lib
$(DELPHI)\Components\dcpcrypt\Lib
$(DELPHI)\Components\sap\lib
$(DELPHI)\Components\bsskin\bsfd7
C:\PROGRA~1\Borland\jcl\source
C:\Programme\Borland\jvcl\common
C:\Programme\Borland\jvcl\lib\D7
(DELPHI)\Components\AsyncPro\Lib
$(DELPHI)\Components\Toolbar2k\lib

Installed components

Cool Tray Icon
DCPcrypt
EC Software Help Suite
Fast Report 2.52
Fast Report 3.0
FIBPlus
GLScene
Greatis Ultimate Pack
Help & Manual Project wizard
InstantObjects
ICS
JCL
JVCL
Max’s Native Excel Component
Max’s Plug-In Component
Max’s Protector Component
MiTeC System Information Component
php4delphi
Pragnaan FastReport Export Filters
RemObjects Pascal Script
RX Library
SQL Direct
Sunisoft SUIPack
Synedit
Toolbar 2000
TBX2k
TMS Component Pack
Tsc Excel Export Pack
TurboPower Async Pro
VclSKin
VNC
XML Parser Components
ZEOS Components