Board index » delphi » How to execute from console app?

How to execute from console app?

Hi,

I'm writing an {$APPTYPE CONSOLE} program, and need to execute
occasionally an external (also console type) .exe file usually
with some parameters.

Using ShellExecute is always trying to open another window...

First, is there some way to run that external utility so that it
runs in the same console.

Second, how do I capture the exit code that this external
utility is leaving behind?

Thanks in advance for any help on this,

Aavo

--
== Aavo Tambur ==============================================
| Email: a...@delfi.ee             | Programmer             |
| URL: http://home.delfi.ee/aavo/  | Union Bank of Estonia  |
=============================================================

 

Re:How to execute from console app?


The following function will:
- Run an external application
  (for console apps within the same window)
- Wait for the application to finish
- Return its exit code (or return -1 if failed)

function RunCommand(CmdLine: String): DWORD;
var
  StartupInfo: TStartupInfo;
  ProcessInfo: TProcessInformation;
begin
  GetStartupInfo(StartupInfo);
  if not CreateProcess(nil, PChar(CmdLine), nil, nil, IsConsole,
    NORMAL_PRIORITY_CLASS, nil, nil, StartupInfo, ProcessInfo) or
    (WaitforSingleObject(ProcessInfo.hProcess, INFINITE) <> WAIT_OBJECT_0)
or
    not GetExitCodeProcess(ProcessInfo.hProcess, Result) then
    Result := DWORD(-1);
end;

"Aavo Tambur" <aavo.tam...@eyp.ee> schreef in bericht
news:39113A13.4C29657F@eyp.ee...

Quote
> Hi,

> I'm writing an {$APPTYPE CONSOLE} program, and need to execute
> occasionally an external (also console type) .exe file usually
> with some parameters.

> Using ShellExecute is always trying to open another window...

> First, is there some way to run that external utility so that it
> runs in the same console.

> Second, how do I capture the exit code that this external
> utility is leaving behind?

> Thanks in advance for any help on this,

> Aavo

> --
> == Aavo Tambur ==============================================
> | Email: a...@delfi.ee             | Programmer             |
> | URL: http://home.delfi.ee/aavo/  | Union Bank of Estonia  |
> =============================================================

Re:How to execute from console app?


Look at "CreateProcess" and check out the Pipe options.
Quote
Aavo Tambur wrote:
> Hi,

> I'm writing an {$APPTYPE CONSOLE} program, and need to execute
> occasionally an external (also console type) .exe file usually
> with some parameters.

> Using ShellExecute is always trying to open another window...

> First, is there some way to run that external utility so that it
> runs in the same console.

> Second, how do I capture the exit code that this external
> utility is leaving behind?

> Thanks in advance for any help on this,

> Aavo

> --
> == Aavo Tambur ==============================================
> | Email: a...@delfi.ee             | Programmer             |
> | URL: http://home.delfi.ee/aavo/  | Union Bank of Estonia  |
> =============================================================

Re:How to execute from console app?


Great many thanks!!!
It works just the way it should.

Quote
"M.H. Avegaart" wrote:

> The following function will:
> - Run an external application
>   (for console apps within the same window)
> - Wait for the application to finish
> - Return its exit code (or return -1 if failed)

> function RunCommand(CmdLine: String): DWORD;
> var
>   StartupInfo: TStartupInfo;
>   ProcessInfo: TProcessInformation;
> begin
>   GetStartupInfo(StartupInfo);
>   if not CreateProcess(nil, PChar(CmdLine), nil, nil, IsConsole,
>     NORMAL_PRIORITY_CLASS, nil, nil, StartupInfo, ProcessInfo) or
>     (WaitforSingleObject(ProcessInfo.hProcess, INFINITE) <> WAIT_OBJECT_0)
> or
>     not GetExitCodeProcess(ProcessInfo.hProcess, Result) then
>     Result := DWORD(-1);
> end;

> "Aavo Tambur" <aavo.tam...@eyp.ee> schreef in bericht
> news:39113A13.4C29657F@eyp.ee...
> > Hi,

> > I'm writing an {$APPTYPE CONSOLE} program, and need to execute
> > occasionally an external (also console type) .exe file usually
> > with some parameters.

> > Using ShellExecute is always trying to open another window...

> > First, is there some way to run that external utility so that it
> > runs in the same console.

> > Second, how do I capture the exit code that this external
> > utility is leaving behind?

--
== Aavo Tambur ==============================================
| Email: aavo.tam...@eyp.ee        | Programmer             |
| URL: http://home.delfi.ee/aavo/  | Union Bank of Estonia  |
| GSM: (+37) 255 32234             | Tel: (+372) 6 655 494  |
=============================================================

Other Threads