Hi experts,
there are several ways to start an external executable from the main program.
I created a 32 Bit main program from which I want to launch another 32 Bit
application. I need a solution which works both under WIN95 and WIN98 and WIN NT.
For this purpose, several FAQ's recommend the usage of:
ShellExecute(Application.MainForm.Handle, 'print', , '', '',
SW_SHOWNORMAL);
I wanted to try it out, but I don't understand the parameter
"Application.Mainform.Handle".
Does it represent the external executable to be launched?
Moreover I don't know which unit I will have to include in the
uses clause of my demo program, so that my own unit can find
ShellExecute.
I simply want to do something like:
procedure TfrmMain.Button1Click(Sender: TObject);
begin
ShellExecute('notepad.exe c:\autoexec.bat');
end;
What is the correct syntax of this ShellExecute statement with all parameters?
Unfortunately, my collection of FAQ's don't provide me with concrete examples,
so I don't understand how I can use this function.
Do you agree that ShellExecute is really the best way to launch another program?
Or should I rather use alternatives like WinExec,ExecAndWait, ShellExecute, CreateProcess.
What is the correct syntax of WinExec,ExecAndWait, ShellExecute, CreateProcess, if I want
to load my autoexec.bat into notepad.exe with the help of these functions.
BTW: I am meanwhile using Delpi 4.0 , standard edition, if that counts.
With kind regards
Mathes
ShellExecute is the best method in Win 95/98/NT to execute any file. The benefit is that it doesn't have to be an EXE file. It can be DOC, BMP, etc.
The syntax is:
(To answer your question: you have to include the "ShellApi" unit.)
ShellExecute(Self.Handle, 'open', PChar('C:\Windows\Notepad.
I use Self.Handle, where Self is the main form, so this is the same as "Application.MainForm.Hand
Another useful way of starting an app is CreateProcess:
var
ProcessInfo: TProcessInformation;
StartUpInfo: TStartUpInfo;
begin
ZeroMemory(@StartUpInfo, SizeOf(TStartUpInfo));
with StartUpInfo do
begin
cb := SizeOf(TStartUpInfo);
dwFlags := STARTF_USESHOWWINDOW or STARTF_USESTDHANDLES;
wShowWindow := SW_SHOWNORMAL;;
end;
CreateProcess(nil, PChar('C:\Windows\Notepad.
end;
The benefits of CreateProcess are as follows:
(1) You can close Notepad politely:
function ThreadWndProc(hWnd: HWND; lParam: LParam): Bool; stdcall;
begin
// lParam is WM_CLOSE, because that's what we send in when
// we call EnumThreadWindows (below).
// Thus, the following will send WM_CLOSE to the window.
SendMessage(hWnd, lParam, 0, 0);
// Return True to continue enumeration.
Result := True;
end;
if not (EnumThreadWindows(Process
ShowMessage('EnumThreadWin
(2) You can force Notepad to close (unsaved changes in Notepad are lost):
TerminateProcess(ProcessIn
(3) You can execute Notepad & wait for it to terminate:
ZeroMemory(@StartUpInfo, SizeOf(TStartUpInfo));
with StartUpInfo do
begin
cb := SizeOf(TStartUpInfo);
dwFlags := STARTF_USESHOWWINDOW or STARTF_USESTDHANDLES;
wShowWindow := SW_SHOWNORMAL;;
end;
if (CreateProcess(nil, PChar('C:\Windows\Notepad.
begin
WaitForSingleObject(Proces
ShowMessage('Finished');
end;
I usually use ShellExecute because it's pretty simple & can be used on any file (if the file extension is registered). You can use WinExec, but it's maintained only for compatibility with older versions of Windows. WinExec will call CreateProcess anyway.
Cheers,
JB