Author: Jonas Bilinkevicius
This executes a console application, but if your application is not a console, just
remove CREATE_NEW_CONSOLE.
1
2 function RunApp(const aCmd: string; aWait: boolean; aShowMode: integer): DWORD;
3 var
4 StartUpInfo: TStartUpInfo;
5 ProcessInfo: TProcessInformation;
6 WaitCode: DWORD;
7 begin
8 Result := 0;
9 ZeroMemory(@StartupInfo, SizeOf(TStartupInfo));
10 StartUpInfo.cb := SizeOf(StartUpInfo);
11 StartUpInfo.wShowWindow := aShowMode;
12 StartUpInfo.dwFlags := STARTF_USESHOWWINDOW;
13 ZeroMemory(@ProcessInfo, SizeOf(TProcessInformation));
14 Win32Check((CreateProcess(nil, PChar(aCmd), nil, nil, False, CREATE_NEW_CONSOLE +
15 NORMAL_PRIORITY_CLASS, nil, nil, StartUpInfo, ProcessInfo)));
16 try
17 if aWait then
18 begin
19 repeat
20 WaitCode := WaitForSingleObject(ProcessInfo.hProcess, 10000);
21 Win32Check(WaitCode < > WAIT_FAILED);
22 if WaitCode = WAIT_TIMEOUT then
23 begin
24 if MessageDlg('This is a test', mtWarning, [mbYes, mbNo], 0) < > mrYes
25 then
26 Break;
27 end
28 else
29 Break;
30 until
31 False;
32 Win32Check(GetExitCodeProcess(ProcessInfo.hProcess, Result));
33 end;
34 finally
35 CloseHandle(ProcessInfo.hThread);
36 CloseHandle(ProcessInfo.hProcess);
37 end;
38 end;
|