• C

Capturing and hiding console output!

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!

Who is Participating?
AlexFMConnect With a Mentor Commented:
In this case use pipes. Pipe is created using CreatePipe, and ReadFile is used to read process output.


to hide
si.wShowWindow = SW_HIDE;

to capture You must create pipes
wait a moment
Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

javockAuthor Commented:
I'll have to checkout about the pipes...

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

Will see tomorrow with more time!

mokuleConnect With a Mentor Commented:

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

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.
javockAuthor Commented:

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...

javockAuthor Commented:
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.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.