Solved

Running an external *.exe from a Delphi application

Posted on 1998-07-29
6
310 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 4

Accepted Solution

by:
itamar earned 100 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

[Webinar] Learn How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …

707 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