SHARE
TWEET

Running external program and wait for its completion

appo Dec 27th, 2013 330 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. unit Unit2;
  2.  
  3. interface
  4.  
  5. uses
  6.   Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  7.   Vcl.Controls, Vcl.Forms, Vcl.Dialogs;
  8.  
  9. type
  10.   TForm2 = class(TForm)
  11.     procedure FormCreate(Sender: TObject);
  12.   private
  13.     { Private declarations }
  14.   public
  15.     { Public declarations }
  16.   end;
  17.  
  18. var
  19.   Form2: TForm2;
  20.  
  21. implementation
  22.  
  23. {$R *.dfm}
  24.  
  25. function ExecAndWait(const FileName,
  26.                      Params: ShortString;
  27.                      const WinState: Word): boolean; export;
  28. var
  29.   StartInfo: TStartupInfo;
  30.   ProcInfo: TProcessInformation;
  31.   CmdLine: ShortString;
  32. begin
  33.   { Put the file name between the quotation marks, with all spaces in names Win9x }
  34.   CmdLine := '"' + Filename + '" ' + Params;
  35.   FillChar(StartInfo, SizeOf(StartInfo), #0);
  36.   with StartInfo do
  37.   begin
  38.     cb := SizeOf(StartInfo);
  39.     dwFlags := STARTF_USESHOWWINDOW;
  40.     wShowWindow := WinState;
  41.   end;
  42.   Result := CreateProcess(nil, PChar( String( CmdLine ) ), nil, nil, false,
  43.                           CREATE_NEW_CONSOLE or NORMAL_PRIORITY_CLASS, nil,
  44.                           PChar(ExtractFilePath(Filename)),StartInfo,ProcInfo);
  45.   { Forward to completing the application }
  46.   if Result then
  47.   begin
  48.     WaitForSingleObject(ProcInfo.hProcess, INFINITE);
  49.     { Free the Handles }
  50.     CloseHandle(ProcInfo.hProcess);
  51.     CloseHandle(ProcInfo.hThread);
  52.   end;
  53. end;
  54.  
  55. procedure TForm2.FormCreate(Sender: TObject);
  56. var
  57. Drum:string;
  58. begin
  59. Drum:='C:\Project1.exe';
  60. ExecAndWait( Drum, '', SW_HIDE);
  61. application.terminate;
  62. end;
  63. end.
  64.  
  65. // Coded by Appo //
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top