Mega Code Archive

 
Categories / Delphi / Examples
 

Kill an active program

Title: Kill an active program Question: Needed a routine to kill an active program based on classname or window title. I came up with the following routine Answer: procedure KillProgram(Classname : string; WindowTitle : string); const PROCESS_TERMINATE = $0001; var ProcessHandle : THandle; ProcessID: Integer; TheWindow : HWND; begin TheWindow := FindWindow(Classname, WindowTitle); GetWindowThreadProcessID(TheWindow, @ProcessID); ProcessHandle := OpenProcess(PROCESS_TERMINATE, FALSE, ProcessId); TerminateProcess(ProcessHandle,4); end;