Run Other Program?

How to run the other program or EXE file in my Application? The program do not infect my Application.
lufAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

MotazCommented:
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
0
florisbCommented:
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.

0
rwilson032697Commented:
Hmm.. Perhaps you should read your comment again floris!

Cheers,

Raymond.
0
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

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

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

rwilson, the English or the content?

F.
0
MotazCommented:
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
0
MadshiCommented:
Hi Motaz,

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

Regards, Madshi.
0
rwilson032697Commented:
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.
0
intheCommented:
hey dont forget ExecuteFile()
(add to uses "fmxutils")

        :-)
0
florisbCommented:
Hmmm.

?-)
0
nicola65Commented:
i use  this procedure that i have found time ago

{*****************************[ RUNPROCESS ] ***********************************
*
* Type: Function
* Use: To launch an application and optionally wait until the launched
* Application is terminated before running the rest of the code.
*
* PARAMETERS:
*
* AppPath: The full path and Application Name to run ie. c:\winnt\notepad.exe
*
* AppParams: Commandline params to send to the app.
*
* Visibility:
* Can have any of the following values:
*   Value      Meaning
*   SW_HIDE      Hides the window and activates another window.
*   SW_MAXIMIZE      Maximizes the specified window.
*   SW_MINIMIZE      Minimizes the specified window and activates the next top-level
*     window in the Z order.
*   SW_RESTORE      Activates and displays the window. If the window is minimized or
*     maximized, Windows restores it to its original size and position. An
*     application should specify this flag when restoring a minimized window.
*   SW_SHOW      Activates the window and displays it in its current size and position.
*   SW_SHOWDEFAULT      Sets the show state based on the SW_ flag specified in the
*     STARTUPINFO structure passed to the CreateProcess function by the program
*     that started the application.
*   SW_SHOWMAXIMIZED      Activates the window and displays it as a maximized window.
*   SW_SHOWMINIMIZED      Activates the window and displays it as a minimized window.
*   SW_SHOWMINNOACTIVE      Displays the window as a minimized window. The active
*     window remains active.
*   SW_SHOWNA      Displays the window in its current state. The active window remains
*     active.
*   SW_SHOWNOACTIVATE      Displays a window in its most recent size and position.
*     The active window remains active.
*   SW_SHOWNORMAL      Activates and displays a window. If the window is minimized or
*     maximized, Windows restores it to its original size and position. An
*     application should specify this flag when displaying the window for the
*     first time.
*
* MustWait: true if the code must be paused until the termination of the launched
*   Application. false if the code must run directly after launching the app.
*
********************************************************************************}
function TFPrincipale.RunProcess(const AppPath, AppParams: string;
  Visibility: Word; MustWait: Boolean): DWord;
var
  SI: TStartupInfo;
  PI: TProcessInformation;
  Proc: THandle;
begin
  FillChar(SI, SizeOf(SI), 0);
  SI.cb := SizeOf(SI);
  SI.wShowWindow := Visibility;
  listbox3.Items.Add(AppParams);
  if not CreateProcess(PChar(AppPath), PChar(AppParams), nil, nil, false, Normal_Priority_Class, nil, nil, SI, PI) then
    raise Exception.CreateFmt('Failed to excecute program. Error Code %d', [GetLastError]);
  Proc := PI.hProcess;
  CloseHandle(PI.hThread);
  if MustWait then
            if WaitForSingleObject(Proc, Infinite) <> Wait_Failed then
              GetExitCodeProcess(Proc, Result);
  CloseHandle(Proc);
end;
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
intheCommented:
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 ;-)
0
Fatman121898Commented:
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.
0
MadshiCommented:
Hi Fatman, please don't forget to close the handles!!

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

Regards, Madshi.
0
Fatman121898Commented:
Thanks, Madshi!
Y're right, as ever ;-)
Jo.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.