Einzelnen Beitrag anzeigen

Benutzerbild von KodeZwerg
KodeZwerg

Registriert seit: 1. Feb 2018
3.685 Beiträge
 
Delphi 11 Alexandria
 
#3

AW: CreateProcess & TerminateProcess schlägt fehl

  Alt 19. Feb 2021, 19:59
Ich habe Deinen Code nicht getestet sondern einfach mal was getippst, ausgeführt und war alles gut.

Vielleicht hilft es dir.

Delphi-Quellcode:
unit Unit1;

interface

uses

Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,

Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.ExtCtrls;

type

 TForm1 = class(TForm)

 Panel1: TPanel;
 Panel2: TPanel;
 Label1: TLabel;
 edtFilename: TEdit;
 Panel3: TPanel;
 Panel4: TPanel;
 Label2: TLabel;
 edtPH: TEdit;
 Panel5: TPanel;
 Label3: TLabel;
 edtTH: TEdit;
 Panel6: TPanel;
 Label4: TLabel;
 edtPI: TEdit;
 Panel7: TPanel;
 Button1: TButton;
 btnTerminate: TButton;
 Panel8: TPanel;
 Label5: TLabel;
 edtTI: TEdit;
 procedure Button1Click(Sender: TObject);
 procedure btnTerminateClick(Sender: TObject);
 procedure FormDestroy(Sender: TObject);

 private

 { Private declarations }
 ProcessInfo: TProcessInformation;
 procedure ResetGUI;

 public

 { Public declarations }

end;

var

 Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.ResetGUI;
begin
 edtPH.Text := '';
 edtTH.Text := '';
 edtPI.Text := '';
 edtTI.Text := '';
 btnTerminate.Enabled := False;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
 StartUpInfo: TStartUpInfo;
begin
  if (not FileExists(edtFilename.Text)) then
    begin
      ResetGUI;
      Exit;
    end;
  FillMemory(@StartUpInfo, SizeOf(StartUpInfo), 0);
  StartUpInfo.cb := SizeOf(StartUpInfo);
  if CreateProcess(nil, PChar(edtFilename.Text), nil, nil, False, NORMAL_PRIORITY_CLASS, nil, nil, StartUpInfo, ProcessInfo) then
  begin
    edtPH.Text := IntToStr(ProcessInfo.hProcess);
    edtTH.Text := IntToStr(ProcessInfo.hThread);
    edtPI.Text := IntToStr(ProcessInfo.dwProcessId);
    edtTI.Text := IntToStr(ProcessInfo.dwThreadId);
    btnTerminate.Enabled := True;
  end;
end;

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;

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;

end.
/edit
Dies ist nur eine Vorlage zum testen.
Argumente müssen noch nachgetragen werden.
Kann man auch als function mit rückgabe des handles schreiben.

Viel Erfolg
Gruß vom KodeZwerg

Geändert von KodeZwerg (19. Feb 2021 um 20:36 Uhr) Grund: Copy-paste fehler
  Mit Zitat antworten Zitat