1

I have a Delphi 7 (not 2007) application (lets call it App1.exe) that the IDE thinks is a GUI application but in the DPR, a compiler directive that makes it in to a console application. Example:

{$IFDE MAKE_CONSOLE}
  {$APPTYPE CONSOLE}
{$ENDIF MAKE_CONSOLE}

During the build process, MAKE_CONSOLE might be defined.

The problem I am having is that we have another console application (say, App2.exe) that runs App1.exe using the WinAPI CreateProcess. When is occurs, the output from App1.exe is nowhere to be seen :-( When App1.exe is ran straight from Commandline (cmd.exe), the output is shown in the Commandline window.

What I am guessing is that I need to redirect output from App1.exe in the CreateProcess, using the STARTUPINFO structure. I am not sure what I am meant to be doing here.

Other info: * 'dwCreationFlags' that are being used are: CREATE_NEW_PROCESS_GROUP + NORMAL_PRIORITY_CLASS + DEBUG_PROCESS (yes, App2 debugs App1)

  • 'bInheritHandles' is false (does this need to be changed?).

  • Both 'lpProcessAttributes' and 'lpThreadAttributes' are nil, as are 'lpEnvironment' and 'lpCurrentDirectory'.

Have I missed any information that is required to help me out?

Any pointers would be great!

Many thanks in advance.

2
  • Please note there's no such thing as a "GUI console" application. It's either one or the other. Since you've set the APPTYPE CONSOLE setting, your program is a console program. What the IDE shows in the project options is irrelevant; it's trumped by the code. Commented Aug 26, 2010 at 21:56
  • It seems that App1.exe had three build methods: Gui, Text output and XML file output and I was testing while it was built to produce a XML file output (thus, no output to the console). Thanks for everyone's suggestion! Commented Sep 13, 2010 at 20:02

4 Answers 4

1

Here is some code I use for calling command-line programs from Deplhi 7.

It can redirect to the current console (of the main calling exe), if you put the "Visibility" parameter to 0, instead of "SW_SHOWNORMAL".

function WinExecAndWait(const FileName: String; Visibility: integer): cardinal;
var StartupInfo: TStartupInfo;
    ProcessInfo: TProcessInformation;
    Options: cardinal;
begin
  FillChar(StartupInfo,Sizeof(StartupInfo),0);
  StartupInfo.cb := Sizeof(StartupInfo);
  StartupInfo.dwFlags := STARTF_USESHOWWINDOW;
  StartupInfo.wShowWindow := Visibility;
  if Visibility=0 then begin
    Flush(Output);
    Options := NORMAL_PRIORITY_CLASS;
  end else
    Options := CREATE_NEW_CONSOLE or NORMAL_PRIORITY_CLASS;
  if not CreateProcess(nil,
     pointer(FileName),             { pointer to command line string }
     nil,                           { pointer to process security attributes }
     nil,                           { pointer to thread security attributes }
     false,                         { handle inheritance flag }
     Options,                       { creation flags }
     nil,                           { pointer to new environment block }
     nil,                           { pointer to current directory name }
     StartupInfo,                   { pointer to STARTUPINFO }
     ProcessInfo) then              { pointer to PROCESS_INF }
    Result := cardinal(-1) else begin
    WaitforSingleObject(ProcessInfo.hProcess,INFINITE);
    GetExitCodeProcess(ProcessInfo.hProcess,Result);
  end;
end;
Sign up to request clarification or add additional context in comments.

Comments

0

Do you by any chance have DETACHED_PROCESS in the process creation flags? Inheriting the parent console should be the default.

1 Comment

No. The only flags specified are CREATE_NEW_PROCESS_GROUP + NORMAL_PRIORITY_CLASS + DEBUG_PROCESS.
0

This MSDN article discusses how App2 can redirect App1's output:

Creating a Child Process with Redirected Input and Output

Comments

0

ConsoleApp by Martin Lafferty works beautifully for redirecting the output of console programs. You get an event handler that's called for every line of console output.

I can't find the official home for the code, but here's Embarcadero's page:

http://cc.embarcadero.com/Item/14692

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.