Board index » delphi » Launching apps from within Delphi 4

Launching apps from within Delphi 4

Hi all

Can anyone tell me how to start another application running from within a
delphi 4 program.

--
Cheers

Colin
colic...@ozemail.com.au
colin.shortl...@jcu.edu.au

"Falling down is a mistake, not getting up is failure" Helen Keller

 

Re:Launching apps from within Delphi 4


Quote
"Colin Shortland" <colic...@mydesk.net.au> wrote in message

news:95v3v9$m4b$1@gnamma.connect.com.au...

Quote
> Hi all

> Can anyone tell me how to start another application running from within a
> delphi 4 program.

1. Check DSP for a component (http://delphi.icm.edu.pl/).

2. Simple roll your own:

uses ShellAPI;

ShellExecute (tForm1.Handle, 'open', pChar (theFileName), nil, nil,
sw_ShowNormal);

3. More complicated, more flexible:

// Run application and optionally wait for it to finish, return the exit
code
// code originally posted by M.H.Avegaart that I've modified (slightly) to
make the wait optional
// and display an error message, if any
//
function RunExecutable (const FileName : String; WaitForIt : boolean) :
DWORD;

var  StartupInfo    : TStartupInfo;
     ProcessInfo    : TProcessInformation;
     errMsg         : pChar;

begin
Result := STILL_ACTIVE;
GetStartupInfo(StartupInfo);
if CreateProcess(nil, PChar(FileName), nil, nil, IsConsole,
            NORMAL_PRIORITY_CLASS, nil, nil, StartupInfo, ProcessInfo)
then begin
     try
          if (not WaitForIt) or (WaitForSingleObject(ProcessInfo.hProcess,
INFINITE) = WAIT_OBJECT_0)
          then GetExitCodeProcess(ProcessInfo.hProcess, Result);
     finally
          CloseHandle(ProcessInfo.hThread);
          CloseHandle(ProcessInfo.hProcess);
          end;
     end
else begin
     if FormatMessage (Format_Message_Allocate_Buffer or
Format_Message_From_System,
                    nil, GetLastError, 0, @errMsg, 0, nil) <> 0
     then begin
          try
               raise Exception.Create ('CreateProcess failed with error "' +
                         String (errMsg) + '".');
          finally
               LocalFree (Cardinal (errMsg));
               end;
          end;
     end;
end;

Other Threads