unit Unit1;

interface

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

type
  TForm1 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    Function ExecuteAndWait(sExecutableFile : String; wWindowState : Word = SW_SHOWNORMAL) : Boolean;
  private
    { Private declarations }
  public
    { Public declarations }
    handleexe:HWND;
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}


Function tform1.ExecuteAndWait(sExecutableFile : String; wWindowState : Word = SW_SHOWNORMAL) : Boolean;
var
         siInfo  : TStartUpInfo;
         piInfo  : TProcessInformation;
begin
         FillChar(siInfo, SizeOf(siInfo), #0);

         with siInfo do begin
              cb          := SizeOf(siInfo);
              dwFlags     := STARTF_USESHOWWINDOW;
              wShowWindow := wWindowState;
         end;
         CreateProcess(NIL,pChar(sExecutableFile) , NIL, NIL, FALSE, CREATE_NEW_CONSOLE or NORMAL_PRIORITY_CLASS, NIL, pchar(extractfilepath(sExecutableFile)+'..\config'),siInfo, piInfo);
//         if Result then
//            WaitForSingleObject(piInfo.hprocess,INFINITE);

//           sleep(500);
showmessage('provo');

            terminateprocess(piInfo.hprocess,0);

end;



procedure TForm1.Button1Click(Sender: TObject);
begin
//handleexe:=ShellExecute(Handle, 'open', pchar('c:\windows\notepad.exe'), nil, nil, SW_SHOW);
//showmessage(floattostr(handleexe));

//ExecuteAndWait('c:\windows\notepad.exe',SW_SHOWNORMAL);
ExecuteAndWait('C:\Programmi\OpenVPN\bin\openvpn.exe --pause-exit --config client.ovpn',SW_HIDE);
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
//showmessage(floattostr(handleexe));
//terminateprocess(handleexe,0);
SendMessage(handleexe, WM_CLOSE, 0, 0);

end;

end.
