1
2 function TForm1.RunProgramWait ( ProgramName, CommandLine : string;
3 dwCreationFlags: dWord = 0; HowToShowWindow :dWord=SW_SHOWNORMAL) : integer;
4 var
5 StartupInfo : TStartupInfo;
6 ProcessInfo : TProcessInformation;
7 Code : Cardinal;
8 begin
9 FillChar(StartupInfo, sizeof(TStartupInfo),0)
10 with StartupInfo do //recreates record for start
11 begin
12 cb := SizeOf( StartupInfo);
13 dwFlags := STARTF_USESHOWWINDOW;
14 wShowWindow := HowToShowWindow;
15 end;
16
17 Code := 0;
18
19 Application.Hint := 'Starting ' + ProgramName + '...';
20 if CreateProcess(nil, PChar(ProgramName + ' ' + CommandLine),
21 nil, nil, False, dwCreationFlags, nil, PChar(ExtractFilePath(ProgramName)),
22 StartupInfo, ProcessInfo) then //starts the application
23 begin
24 caption := ProgramName + ' -Active';
25 //loop to check if application is terminated or not
26 while GetExitCodeProcess(ProcessInfo.hProcess, Code) and (Code =
27 STILL_ACTIVE) do
28 begin
29 Application.ProcessMessages;
30 Sleep(1000); //to reduce CPU load the higher the better.
31 end;
32 Result := Code;
33 end
34 else
35 Result := 99;
36
37 caption := ProgramName + ' -NOT Active';
38 end;
39
40 procedure TForm1.Button1Click(Sender: TObject);
41 var
42 i: integer;
43 begin
44 //you can use SW_SHOWNORMAL or SW_HIDE
45 i:=RunProgramWait('c:\WINNT\system32\cmd.exe', '', 0, SW_SHOWNORMAL);
46
47 if i=0 then
48 showmessage('Normal Termination')
49 else
50 showmessage('Abnormal Termination')
51 end;
|