Link to home
Start Free TrialLog in
Avatar of luf
luf

asked on

Run Other Program?

How to run the other program or EXE file in my Application? The program do not infect my Application.
Avatar of Motaz
Motaz

What did you mean by infect my application ?

If you want to run any application or open any document use ShellExecute:

  ShellExecute(handle, 'open', FileName, nil, nil, 0);

do not miss to include ShellApi in your uses clause

Motaz
shellexecute will work, but I tought using CreateProcess is better, at least it gives some more functionality. Check Delphi help and previous answer in this group containing an example.

From help:
"The WinExec function runs the specified application.

This function is provided for compatibility with earlier versions of Windows. For Win32-based applications, use the CreateProcess function."

Floris.

Hmm.. Perhaps you should read your comment again floris!

Cheers,

Raymond.
What is wrong with using ShellExecute, I use it for a long time...

Motaz
yeah, what's wrong with shellexecute?
(I don't know)...

rwilson, the English or the content?

F.
I think you must use CreateProcess to track termination of executed application, but if you do not care about termination of executed exe's you can use ShellExecute

Motaz
Hi Motaz,

you can also use ShellExecuteEx if you need to wait for the termination of the process after starting it.

Regards, Madshi.
Floris: Its just that your comment seems to treat ShellExecute = WinExec (which it isn't).

Actually WinExec is now just a wrapper for ShellExecute...

Hmm... ;-]

Cheers,

Raymond.
hey dont forget ExecuteFile()
(add to uses "fmxutils")

        :-)
Hmmm.

?-)
ASKER CERTIFIED SOLUTION
Avatar of nicola65
nicola65

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
just some advice to nicola65
 please only leave comments if other people have left comment as motaz answered this with his first comment and it is unfair to propose your comment as the answer.
just makes for a friendlier environment ;-)
Hi luf,
Here is the function I use in my app's.
I see Nicola's function is very similar, but the similarity is ocasional (I think) because I have answered this question 2 times before and the answer was the same :-).

function ExecApplication(APPName, CmdLine: String; ShowMode: DWord; WaitToExit: Boolean): DWord;
//executes as well WIN and DOS application
  var StartInfo: TStartupInfo;
      ProcInfo: TProcessInformation;
  begin
    StartInfo.cb:=SizeOf(StartInfo);
    FillChar(StartInfo, SizeOf(StartInfo), 0);
    StartInfo.dwFlags:=STARTF_USESHOWWINDOW;
    StartInfo.wShowWindow:=ShowMode;
    if AppName<>''
      then CreateProcess(PChar(APPName), PChar(CmdLine), nil, nil, False, 0,
                         nil, nil, StartInfo, ProcInfo)
      else CreateProcess(nil, PChar(CmdLine), nil, nil, False, 0,
                         nil, nil, StartInfo, ProcInfo);
      if WaitToExit then WaitForSingleObject(ProcInfo.hProcess, INFINITE);
    GetExitCodeProcess(ProcInfo.hProcess, Result);
  end;

Jo.
Hi Fatman, please don't forget to close the handles!!

CloseHandle(ProcInfo.hProcess);
CloseHandle(ProcInfo.hThread);

Regards, Madshi.
Thanks, Madshi!
Y're right, as ever ;-)
Jo.