Solved

Running an external *.exe from a Delphi application

Posted on 1998-07-29
6
292 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 100 total points
Comment Utility
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
Comment Utility
Ha !

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

Itamar
0
 
LVL 4

Expert Comment

by:erajoj
Comment Utility
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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
LVL 4

Expert Comment

by:BoRiS
Comment Utility
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
Comment Utility
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
Comment Utility
hi,

thank you all for your help and comments.

with kind regards

christian
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

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…
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…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

771 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now