Like killing lawsuits with Delphi?

Asked

Viewed 3,731 times

3

I need to make an application in Delphi that finalizes processes through the image name, if this is possible tell me how.

2 answers

4


Use this function:

   function KillTask(ExeFileName: string): Integer;
    const
      PROCESS_TERMINATE = $0001;
    var
      ContinueLoop: BOOL;
      FSnapshotHandle: THandle;
      FProcessEntry32: TProcessEntry32;
    begin
      Result := 0;
      FSnapshotHandle := CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
      FProcessEntry32.dwSize := SizeOf(FProcessEntry32);
      ContinueLoop := Process32First(FSnapshotHandle, FProcessEntry32);
      while Integer(ContinueLoop) <> 0 do
      begin
        if ((UpperCase(ExtractFileName(FProcessEntry32.szExeFile)) =
          UpperCase(ExeFileName)) or (UpperCase(FProcessEntry32.szExeFile) =
          UpperCase(ExeFileName))) then
          Result := Integer(TerminateProcess(
                            OpenProcess(PROCESS_TERMINATE,
                                        BOOL(0),
                                        FProcessEntry32.th32ProcessID),
                                        0));
         ContinueLoop := Process32Next(FSnapshotHandle, FProcessEntry32);
      end;
      CloseHandle(FSnapshotHandle);
    end;

To use:

  KillTask('calc.exe');

Declare: Tlhelp32

  • Must be missing some statement, because Delphi is finding errors.

  • Declare: Tlhelp32. Thank you.

-1

Hello!

I needed to finish a process for your PID. For example, if I had two MS Excel spreadsheets open, I would like to finish just one of them, specific by PID.

In the task manager:

MSEXCEL 8345

MSEXCEL 9601 <- this is related to my application

To end, for example, only the PID 9601 process, I used something like:

var planilha: OleVariant; ProcIdXL: cardinal; xlHWND: HWND; sucesso: boolean;
begin
  planilha := CreateoleObject('Excel.Application');
  try
  // armazena informação do processo
  xlHWND := planilha.hwnd;
  GetWindowThreadProcessId(xlHWND, ProcIdXL);  // vai pegar o 9601
  ...
  // executa o meu código
  sucesso := true;
  ...
  finally
  ...      
  if (not sucesso) AND (not VarIsEmpty(planilha)) then begin
    planilha := Unassigned;
    if (ProcIdXL > 0) then
      TerminateProcess(OpenProcess(PROCESS_TERMINATE, BOOL(0), ProcIdXL), 0);
    end;
  ...
  end;
end;

Just adapt to your need...

Browser other questions tagged

You are not signed in. Login or sign up in order to post.