Board index » delphi » Launching external applications from a delphi application

Launching external applications from a delphi application

What is the code to launch an external application from within a delphi
application ?

How do I start for example Windows' Calculator from my program ?

Thanks in advance,

Chris

 

Re:Launching external applications from a delphi application


Quote
Chris Gheysen wrote:
>What is the code to launch an external application from within a delphi
>application ?

To run an external application from Delphi you need to use the following
API command.

    WinExec(lpCmdLine: PChar; uCmdShow: Integer);

Quote
>How do I start for example Windows' Calculator from my program ?

So, to run the calculator you write

    WinExec('calc.exe', SW_SHOW);

The SW_SHOW constant will launch the application in its default state.

Quote
>Thanks in advance,

You're welcome.

Joachim Calvert (jcalv...@hem.passagen.se)

Re:Launching external applications from a delphi application


Lookup WINEXEC in the help
GL

Re:Launching external applications from a delphi application


I would use CreateProcess if you're using Delphi 3. With CreateProcess you
have capability to for example start an external program and wait until
that program is finished before doing anything else.

Here's an example of CreateProcess and how to wait for the process to end.

// This function is run as a thread...
function ThreadFunc(info : pointer): integer; stdcall;
begin
  WaitForSingleObject(ExecProcessInfo.hProcess,INFINITE);
  CloseHandle(ExecProcessInfo.hProcess);
  // Here U set a flag or do something else to mark that the propcess has been killed..
end;

var
  ExecStartUpInfo: TStartUpInfo;
  ThreadID : DWORD;
  ThreadHandle : THandle;
  ExecProcessInfo : TProcessInformation;
begin
  FillChar(ExecStartupInfo, SizeOf(TStartupInfo), 0);
  with ExecStartupInfo do
  begin
    cb := SizeOf(TStartupInfo);
    dwFlags := STARTF_USESHOWWINDOW;
    wShowWindow := SW_SHOWNORMAL;
  end;

  if not CreateProcess(nil,PChar('c:\windows\notepad.exe'),nil,nil,false,NORMAL_PRIORITY_CLASS,nil,nil,ExecStartupInfo,ExecProcessInfo) then
    messagedlg('Error creating process',mtWarning,[mbOK],0);
  else begin
    ThreadHandle := CreateThread(nil,0,@ThreadFunc, nil, 0 , ThreadID);
    //ThreadFunc is a function which check if notepad is still running...
    repeat
      Application.ProcessMessages;
    until SomeFlagIsSet
    //When U are here notepad has been closed...
  end;
end;

Regards, Stefan Linden

Quote
On Sun, 10 May 1998, Geraint Letherby wrote:
> Lookup WINEXEC in the help
> GL

Other Threads