About Run External App - Forum

Forum Navigation
You need to log in to create posts and topics.

About Run External App

I'm using VisualNeoWin to run an external app and perform certain tasks. I'm curious about the method employed to run the app, ensuring it waits to finish loading before continuing and I saw VisualNeoWin, created by Delphi.

Sometimes, I use Delphi for my projects. In Delphi, when waiting to exit, TProcess.WaitOnExit is commonly used. However, I haven't found a comparable method for waiting until the app completes its loading.

Are you familiar with any such method or approach in Delphi? I would greatly appreciate any insights or suggestions. Thanks!

@victor

The Run command has a parameter that allows you to pause script execution before the external application is loaded.
https://winhelp.visualneo.com/Applications.html

Is this what is required?

 

yes but i looking for that method to use in delphi .  since visualneo created by delphi i thought this method must be done in delphi . its usfull method and some time want to use in delphi

@victor

Perhaps these hints will help:

procedure TForm1.Button1Click(Sender: TObject);
var HW:hwnd;
begin
ShellExecute(Handle,'open','C:\windows\notepad.exe',nil,nil,SW_SHOW); //run notepad
sleep(100); //sleep, or there won't be a window handel
hw:== FindWindow(vbNullString, "Calculator") // find the handle of the desired window (Use vbNullString to ignore the window class)
Windows.SetParent(hw,Handle); //make it a child window of your application
end;

 

***

 

The example shows how to start an external application from your program and wait for it to complete.

function ExecAndWait(const FileName,
Params: ShortString;
const WinState: Word): boolean; export;
var
StartInfo: TStartupInfo;
ProcInfo: TProcessInformation;
CmdLine: ShortString;
begin
{ Put the file name between quotation marks, respecting all spaces in Win9x names }
CmdLine := '"' + Filename + '" ' + Params;
FillChar(StartInfo, SizeOf(StartInfo), #0);
with StartInfo do
begin
cb := SizeOf(SUInfo);
dwFlags := STARTF_USESHOWWINDOW;
wShowWindow := WinState;
end;
Result := CreateProcess(nil, PChar( String( CmdLine ) ), nil, nil, false,
CREATE_NEW_CONSOLE or NORMAL_PRIORITY_CLASS, nil,
PChar(ExtractFilePath(Filename)),StartInfo,ProcInfo);
{ Waiting for the application to complete }
if Result then
begin
WaitForSingleObject(ProcInfo.hProcess, INFINITE);
{ Free the Handles }
CloseHandle(ProcInfo.hProcess);
CloseHandle(ProcInfo.hThread);
end;
end;

And here is an example of calling this function:

ExecAndWait( 'C:\windows\calc.exe', '', SH_SHOWNORMAL)
Parameter FileName =Name of the external program.
Parameter Params = Parameters required to run an external program
Parameter WinState = Specifies how the window will be displayed:
We can also use the following constants for this parameter: SW_HIDE, SW_MAXIMIZE, SW_MINIMIZE, SW_SHOWNORMAL
PS: This code was tested in delphi versions 3, 4 and 5.

It works in 7 just needs to be replaced:
cb := SizeOf(SUInfo);
на
cb := SizeOf(StartInfo);

 

***

 

The example shows how to start an external application from your program and wait for it to complete.

01
function ExecAndWait(const FileName,
02
Params: ShortString;
03
const WinState: Word): boolean; export;
04
var
05
StartInfo: TStartupInfo;
06
ProcInfo: TProcessInformation;
07
CmdLine: ShortString;
08
begin
09
{ Put the file name between quotation marks, respecting all spaces in Win9x names }
10
CmdLine := '"' + Filename + '" ' + Params;
11
FillChar(StartInfo, SizeOf(StartInfo), #0);
12
with StartInfo do
13
begin
14
cb := SizeOf(SUInfo);
15
dwFlags := STARTF_USESHOWWINDOW;
16
wShowWindow := WinState;
17
end;
18
Result := CreateProcess(nil, PChar( String( CmdLine ) ), nil, nil, false,
19
CREATE_NEW_CONSOLE or NORMAL_PRIORITY_CLASS, nil,
20
PChar(ExtractFilePath(Filename)),StartInfo,ProcInfo);
21
{ Waiting for the application to complete }
22
if Result then
23
begin
24
WaitForSingleObject(ProcInfo.hProcess, INFINITE);
25
{ Free the Handles }
26
CloseHandle(ProcInfo.hProcess);
27
CloseHandle(ProcInfo.hThread);
28
end;
29
end;

And here is an example of calling this function:
1
ExecAndWait( 'C:windowscalc.exe', '', SH_SHOWNORMAL)
Parameter FileName = Name of the external program. Parameter Params = Parameters required to run an external program. Parameter WinState = Specifies how the window will be displayed: For this parameter we can also use the following constants: SW_HIDE, SW_MAXIMIZE, SW_MINIMIZE, SW_SHOWNORMAL

victor has reacted to this post.
victor

thank you my friend . i will test and hope to work . thanks again :)

Vadim has reacted to this post.
Vadim

Don't forget to include the ShellAPI module in the USES section

as3856

Vadim and victor have reacted to this post.
Vadimvictor

@vadim

i tested code . but it seem it will wait to external app exit then continue . :(

@victor

Attach in the archive an isolated code that should solve the problem but does not work. Those who can evaluate it and look at it will be able to give hints.