Einzelnen Beitrag anzeigen

Benutzerbild von himitsu
himitsu

Registriert seit: 11. Okt 2003
Ort: Elbflorenz
43.163 Beiträge
 
Delphi 12 Athens
 
#4

AW: CreateProcess & TerminateProcess schlägt fehl

  Alt 19. Feb 2021, 22:55
Zitat:
Delphi-Quellcode:
procedure TForm1.FormDestroy(Sender: TObject);
begin
  if ((ProcessInfo.hProcess <> 0) and (ProcessInfo.hProcess <> INVALID_HANDLE_VALUE)) then
    CloseHandle(ProcessInfo.hProcess);
  if ((ProcessInfo.hThread <> 0) and (ProcessInfo.hThread <> INVALID_HANDLE_VALUE)) then
    CloseHandle(ProcessInfo.hThread);
end;
Da jemand keine Rückgaben der API "CloseHandle" prüft .... wozu dann die Eingaben prüfen?
Delphi-Quellcode:
procedure TForm1.FormDestroy(Sender: TObject);
begin
  CloseHandle(ProcessInfo.hProcess);
  CloseHandle(ProcessInfo.hThread);
end;
Zitat:
Delphi-Quellcode:
procedure TForm1.btnTerminateClick(Sender: TObject);
begin
  if ((ProcessInfo.hProcess <> 0) and (ProcessInfo.hProcess <> INVALID_HANDLE_VALUE)) then
  if TerminateProcess(ProcessInfo.hProcess, 0) then
    begin
      if ((ProcessInfo.hProcess <> 0) and (ProcessInfo.hProcess <> INVALID_HANDLE_VALUE)) then
        CloseHandle(ProcessInfo.hProcess);
      if ((ProcessInfo.hThread <> 0) and (ProcessInfo.hThread <> INVALID_HANDLE_VALUE)) then
        CloseHandle(ProcessInfo.hThread);
      btnTerminate.Enabled := False;
    end;
end;
Aber egal ob du oder Windows die Werte prüfen, wie soll eine Prüfung funktionieren, wenn sie ungültige oder gar total "falsche" Daten bekommt?
Delphi-Quellcode:
procedure TForm1.btnTerminateClick(Sender: TObject);
begin
  if ((ProcessInfo.hProcess <> 0) and (ProcessInfo.hProcess <> INVALID_HANDLE_VALUE)) then
  if TerminateProcess(ProcessInfo.hProcess, 0) then
    begin
      CloseHandle(ProcessInfo.hProcess);
      CloseHandle(ProcessInfo.hThread);
      ProcessInfo.hProcess := INVALID_HANDLE_VALUE; // oder 0
      ProcessInfo.hThread := INVALID_HANDLE_VALUE;
      btnTerminate.Enabled := False;
    end;
end;
Denn ratet mal was passiert, wenn das Handle in der Zwischenzeit schonwieder durch was Anderes belegt ist?
-> Genau, anstatt nichts zu machen, wird "irgendwas" Anderes geschlossen.

Also nochmal auf den Knopf drücken (OK, btnTerminate.Enabled:=False) oder spätstens beim FormDestroy.
Garbage Collector ... Delphianer erzeugen keinen Müll, also brauchen sie auch keinen Müllsucher.
my Delphi wish list : BugReports/FeatureRequests

Geändert von himitsu (19. Feb 2021 um 22:59 Uhr)
  Mit Zitat antworten Zitat