Capturing and hiding console output!

Posted on 2004-04-30
Last Modified: 2007-12-19
Hello experts,

Im using this code to execute a console aplication from a Borland Builder Form :

      ZeroMemory( &si, sizeof(si) );
      si.cb = sizeof(si);
      ZeroMemory( &pi, sizeof(pi) );

      // Start the child process.
      if( !CreateProcess( file, // No module name (use command line).
                  params, // Command line.
                  NULL,             // Process handle not inheritable.
                  NULL,             // Thread handle not inheritable.
                  FALSE,            // Set handle inheritance to FALSE.
                  0,                // No creation flags.
                  NULL,             // Use parent's environment block.
                  dir,             // Use parent's starting directory.
                  &si,              // Pointer to STARTUPINFO structure.
                  &pi )             // Pointer to PROCESS_INFORMATION structure.

I use this method, because I want to wait until the console application finishes to launch another application using :

      // Wait until child process exits.
      while (WaitForSingleObject( pi.hProcess, 50 )==WAIT_TIMEOUT)

So the execution is handled, now to the interesting part.
using THIS CODE or one that has the SAME BEHAVIOUR...

is there a way to capture the output from this console application?
is there a way to COMPLETELY hide the console application from the user?

ovbiously I'm using windoze!

Question by:javock
  • 3
  • 3
  • 2
LVL 17

Expert Comment

ID: 10964335

to hide
si.wShowWindow = SW_HIDE;

to capture You must create pipes
wait a moment
LVL 17

Assisted Solution

mokule earned 300 total points
ID: 10964366

Author Comment

ID: 10964430
I'll have to checkout about the pipes...

On a rapid test, SW_HIDE didn't work...

Will see tomorrow with more time!

LVL 17

Assisted Solution

mokule earned 300 total points
ID: 10964587

     si.cb = sizeof(si);
     si.wShowWindow = SW_HIDE;

Superior storage. Superior surveillance.

WD Purple drives are built for 24/7, always-on, high-definition security systems. With support for up to 8 hard drives and 32 cameras, WD Purple drives are optimized for surveillance.

LVL 48

Expert Comment

ID: 10966386
To hide console wndow use CREATE_NO_WINDOW flag in the dwCreationFlags CreateProcess parameters.

To redirect console input/output use lpStartupInfo CreateProcess parameter. STARTUPINFO::dwFlags should include STARTF_USESTDHANDLES flags, and STARTUPINFO members hStdInput, hStdOutput and hStdError should be pipe or file handles for input/output. Unfortunately, it's impossible to regirect onlt process output. You must redirect all handles.
You can use pipes or files. Using of files is less flexible but simple. For example, if you know that process doesn't use any input, open emplty file using CreateFile function with read permissions, and pass it's handle as hStdInput. Create two new files using CreateFile function with write permissions, and pass these handles as hStdOutput and hStdError. After process finished, read hStdOutput file - this is process output.

Author Comment

ID: 10967130

The console application does not expect input, so it would be read only. But, the process is quite long, and I would like to read output as it comes (during execution) and not when the application has finished...


Author Comment

ID: 10967175
Now that I think, that is not possible right? read output as it comes out... and react to it... I have this progressbar and the console aplication prints a dot for each percent of process done, so I would like to read this output and increment the bar as each new dot appears...

also it could be useful to show the output as a debug tool, and to hide it for the user when the application has ended.

LVL 48

Accepted Solution

AlexFM earned 200 total points
ID: 10968184
In this case use pipes. Pipe is created using CreatePipe, and ReadFile is used to read process output.

Featured Post

Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Trouble linking program with -lcrypt 3 142
why "." vs "->" 23 119
Line meaning 9 82
Super Scope, DHCP 5 53
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode ( They will have you believe that Unicode requires you to use…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now