Welcome, Guest
Username: Password: Remember me
Discussions for CodeTyphon Object Pascal Programming Language
  • Page:
  • 1

TOPIC:

Looking for TerminateProcessByID 1 day 22 hours ago #19076

  • W. Peterson
  • W. Peterson 's Avatar Topic Author
  • Offline
  • New Member
  • New Member
  • Posts: 7
  • Thank you received: 0
I need to use the TerminateProcessByID instruction, but it is not showing up in CT.  It was supposed to be in Windows and/or SysUtils.  Anyone know if this instruction is somewhere in CT?
Thanks

Routine:
procedure TForm1.RunningProcesses(ProcessName : string);
var x : integer; Snapshot: THandle; pe: TProcessEntry32;
begin
     Snapshot := CreateToolhelp32Snapshot(TH32CS_SNAPALL, 0);
     try
       pe.dwSize := SizeOf(pe);
       
       if Process32First(Snapshot, pe) then 
          while Process32Next(Snapshot, pe) do 
                if uppercase(ProcessName) = uppercase(pe.szExeFile) then
                   TerminateProcessByID(pe.th32ProcessID);
     finally
       CloseHandle(Snapshot);
     end;
end;

Error:
Unit1.pas(191,21) Error: Identifier not found "TerminateProcessByID"

Please Log in or Create an account to join the conversation.

Last edit: by W. Peterson .

Looking for TerminateProcessByID 1 day 21 hours ago #19077

  • LuZZZZi
  • LuZZZZi's Avatar
  • Offline
  • Junior Member
  • Junior Member
  • Posts: 94
  • Thank you received: 1
I have found this example at:
andydunkel.net/2022/07/13/lazarus-terminate-windows-process/
I haven't tested code.

Please Log in or Create an account to join the conversation.

Looking for TerminateProcessByID 1 day 19 hours ago #19078

  • W. Peterson
  • W. Peterson 's Avatar Topic Author
  • Offline
  • New Member
  • New Member
  • Posts: 7
  • Thank you received: 0
Well, my stupid.  It was the name of a function that was misplaced below my main procedure.  Thus not found.  I thought that it was some kind of parameter like SysUtils.ExecuteProcess(...).
Not all was lost because it caused me to dig and find another way using taskkill.

//Terminate process by processID
function TerminateProcessByID(ProcessID: Cardinal): Boolean;
var hProcess : THandle;
begin
     Result := False;

     hProcess := OpenProcess(PROCESS_TERMINATE, True, ProcessID);

     if hProcess > 0 then
        try
        Result := Win32Check(Windows.TerminateProcess(hProcess,0));
        finally
        CloseHandle(hProcess);
        end;
end;

//Get running processes list and kill a process if running
procedure TForm1.TerminateProcess(ProcessName : string);
var Snapshot: THandle; pe: TProcessEntry32;
begin
     Snapshot := CreateToolhelp32Snapshot(TH32CS_SNAPALL, 0);
     try
        pe.dwSize := SizeOf(pe);

        if Process32First(Snapshot, pe) then
           while Process32Next(Snapshot, pe) do begin
                 if uppercase(ProcessName) = uppercase(pe.szExeFile) then begin
                    TerminateProcessByID(pe.th32ProcessID);
                 end;
           end;
     finally
        CloseHandle(Snapshot);
     end;
end;  


procedure TForm1.ProcessRtn(ProcessLine, Param: string);
var AProcess: TProcess;
begin
     AProcess := TProcess.Create(nil);
   try
     AProcess.ShowWindow := swoHIDE;
     AProcess.CommandLine := ProcessLine + ' ' + Param;  //'Taskkill /F /PID 4612';
     AProcess.Execute();
     Application.ProcessMessages;
     sleep(3000);
   finally
     AProcess.Free();
   end;

//tasklist > tasklist.txt
//ProcessRtn('taskkill',  ' /F /PID 5288');
end;  

I know that  AProcess.CommandLine is depreciated, but it shouldn't be in my opinion.  It is the only way to successfully to kill a process.

Please Log in or Create an account to join the conversation.

Last edit: by W. Peterson .
  • Page:
  • 1