?
Solved

Running an external *.exe from a Delphi application

Posted on 1998-07-29
6
Medium Priority
?
329 Views
Last Modified: 2010-04-04
Hi experts,

I am new to Delphi. I have some experience with Borland Pascal and Borland c++

From my Delphi application, I would like to run another application, let us say notepad.exe for example.

In  C/Pascal , I would try it like this:

exec(notpad.exe)

Unfportunately there is no exec statement in Delphi. So how can I start another *.exe file from my program ?

With kind regards

Christian
0
Comment
Question by:mathes
6 Comments
 
LVL 4

Accepted Solution

by:
itamar earned 400 total points
ID: 1359027
Hi mathes,

you must use WinExec API call like this:

begin
   if WinExec(PChar('calc.exe'),SW_SHOW)<32 then begin
      messagedlg('Could not run application.', mtInformation,[mbOk], 0);
end;

IHTH,
Itamar
0
 
LVL 4

Expert Comment

by:itamar
ID: 1359028
Ha !

You can also use CreateProcess but it´s a little cumbersome ;)

Itamar
0
 
LVL 4

Expert Comment

by:erajoj
ID: 1359029
CreateProcess example:

function ExecuteAndWait(ExeFileName, CommandLine: string; Hidden: Boolean): Integer;
 { returns -1 if the Exec failed, otherwise returns the process' exit
   code when the process terminates }
var
  Command    : string;
  StartupInfo: TStartupInfo;
  ProcessInfo: TProcessInformation;
begin
  Command := ExeFilename+' '+CommandLine;
  FillChar(StartupInfo, Sizeof(StartupInfo),#0);
  StartupInfo.cb := Sizeof(StartupInfo);
  StartupInfo.dwFlags := STARTF_USESHOWWINDOW;
  if Hidden
  then StartupInfo.wShowWindow := SW_HIDE
  else StartupInfo.wShowWindow := SW_SHOWNORMAL;
  if not CreateProcess(nil,
      PChar(Command),                { pointer to command line string }
      nil,                           { pointer to process security attributes }
      nil,                           { pointer to thread security attributes }
      false,                         { handle inheritance flag }
      CREATE_NEW_CONSOLE or          { creation flags }
      NORMAL_PRIORITY_CLASS,
      nil,                           { pointer to new environment block }
      nil,                           { pointer to current directory name }
      StartupInfo,                   { pointer to STARTUPINFO }
      ProcessInfo) then              { pointer to PROCESS_INF }
    Result := -1
  else begin
    WaitforSingleObject(ProcessInfo.hProcess,INFINITE);
    GetExitCodeProcess(ProcessInfo.hProcess,Result);
  end;
end;

Usage:
  ExecuteAndWait( 'notepad', 'readme.txt', True );
  This will execute notepad and suspend your app until notepad terminates.

/// John

0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 4

Expert Comment

by:BoRiS
ID: 1359030
mathes

the best way I found to do it is to use the ShellExecute API call...

ShellExecute(Handle, 'open'//or print, 'notepad.exe', 'windows.txt', 'c:\windows', SW_SHOWNORMAL);

were...

Handle : is handle to parent window

'open or print' : to open or print a file

notepad.exe : the file required to open

windows.txt : the parameter to on in notepad //this may be nil if you don't what to open a file in the exe file chossen

c:\windows : the directory where the file is found //this may be nil if you put a path in the file required to open field...

SW_SHOWNORMAL : is the show command...

Later
BoRiS
0
 
LVL 4

Expert Comment

by:BoRiS
ID: 1359031
another example with nil fields...

ShellExecute(Handle, 'open'//or print, 'c:\windows\notepad.exe', nil, nil, SW_SHOWNORMAL); //95 only 98 requires the path in the path field...

Sorry the most important remember to a the ShellAPI to your uses clause...

Later
BoRiS
0
 

Author Comment

by:mathes
ID: 1359032
hi,

thank you all for your help and comments.

with kind regards

christian
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Suggested Courses
Course of the Month15 days, 14 hours left to enroll

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question