Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Running an external *.exe from a Delphi application

Posted on 1998-07-29
6
Medium Priority
?
321 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 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
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

What’s Wrong with Your Cloud Strategy ?

Even as many CIOs are embracing a cloud-first strategy, the reality is that moving to the cloud is a lengthy process and the end-state is likely to be a blend of multiple clouds—public and private. Learn why multicloud solutions matter in this webinar by Nimble Storage.

Question has a verified solution.

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

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Suggested Courses

636 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