views:

138

answers:

3

Hello,

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, Nick

A: 

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

500 - Internal Server Error
No. The only flags specified are CREATE_NEW_PROCESS_GROUP + NORMAL_PRIORITY_CLASS + DEBUG_PROCESS.
Nick Ring
+1  A: 

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

Creating a Child Process with Redirected Input and Output

Remy Lebeau - TeamB
A: 

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;
A.Bouchez