Link to home
Start Free TrialLog in
Avatar of mathes
mathes

asked on

Running an external *.exe from a Delphi application

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
ASKER CERTIFIED SOLUTION
Avatar of itamar
itamar

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
Avatar of itamar
itamar

Ha !

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

Itamar
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

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
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
Avatar of mathes

ASKER

hi,

thank you all for your help and comments.

with kind regards

christian