Board index » delphi » Start/stop processes

Start/stop processes

Hello!

I want to start an external program (.exe) from my Delphi-program. How
do I do that and how do I kill this process later?

Would be great if you mailed a copy of your answer to
pba...@hem.passagen.se !

Thanks in advance, Peter Bayer

 

Re:Start/stop processes


checkout CreateProcess() and TerminateProcess() WIN32 API functions.

I can throw together a sample for you tonight when I have time if you don't
come up with something by the.  I do have a couple samples on my Delphi Tip
of the Day webpage that use CreateProcess().  It is at
http://members.truepath.com/delphi

If you need something later, email me at lewishow...@yahoo.com tonight

--
--
Lewis Howell
lewishow...@yahoo.com
Lou's Delphi Tip of the Day:
http://members.truepath.com/delphi
Lou's personal webpage:
http://members.truepath.com/LewisHowell

Quote
Peter Bayer wrote in message <37147C9D.3...@student.hk-r.se>...
>Hello!

>I want to start an external program (.exe) from my Delphi-program. How
>do I do that and how do I kill this process later?

>Would be great if you mailed a copy of your answer to
>pba...@hem.passagen.se !

>Thanks in advance, Peter Bayer

Re:Start/stop processes


Here is the Solution:

This Example is able to start a program and kill it. It reacts on a
command which comes over a socket connection:

unit Start_Server_Unit;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms,
Dialogs,
  ScktComp, StdCtrls;

type
  TForm1 = class(TForm)
    Server: TServerSocket;
    Label1: TLabel;
    procedure FormCreate(Sender: TObject);
    procedure ServerClientRead(Sender: TObject; Socket:
TCustomWinSocket);
  private
    { Private-Deklarationen }
  public
    { Public-Deklarationen }
  end;

var
  Form1: TForm1;
  StartupInfo : TStartupInfo;
  ProcessInfo : TProcessInformation;
  Buf         : String;
  ExitCode    : Integer;
  //gestartet   : BOOLEAN;

implementation

{$R *.DFM}

procedure TForm1.FormCreate(Sender: TObject);
begin
     Server.Active := TRUE;
     FillChar (StartupInfo, SizeOf (TStartupInfo), 0);
     StartupInfo.cb := SizeOf (TStartupInfo);
     Label1.Caption := '';
end;

procedure TForm1.ServerClientRead(Sender: TObject;
  Socket: TCustomWinSocket);
begin
     //gestartet := FALSE;
     buf := socket.ReceiveText;
     if (buf = 'start') then
     begin
          if not CreateProcess (nil,
                                'Proj_ServerCheckTool.exe',
                                nil,
                                nil,
                                TRUE,
                                NORMAL_PRIORITY_CLASS,
                                nil,
                                nil,
                                StartupInfo,
                                ProcessInfo) then
          begin
               //Socket.SendText ('ERROR');
               //gestartet := FALSE;
          end
          else
          begin
               //Socket.SendText ('OK');
               Label1.Caption := 'Gestartet';
               //gestartet      := TRUE;
          end;
     end;
     if (buf = 'stop') then
     begin
        Label1.Caption := 'Gestoppt';
        GetExitCodeProcess (ProcessInfo.hProcess, ExitCode);
        TerminateProcess (ProcessInfo.hProcess, ExitCode);
        //ExitProcess (ExitCode);
        //Socket.SendText ('OK');
     end;
end;

end.

Peter Bayer schrieb:

Quote

> Hello!

> I want to start an external program (.exe) from my Delphi-program. How
> do I do that and how do I kill this process later?

> Would be great if you mailed a copy of your answer to
> pba...@hem.passagen.se !

> Thanks in advance, Peter Bayer

--
Bastian.Trompet...@ADPAG.DE

Other Threads