• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2122
  • Last Modified:

Capturing and hiding console output!

Hello experts,

Im using this code to execute a console aplication from a Borland Builder Form :
      STARTUPINFO si;
      PROCESS_INFORMATION pi;

      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)
            Application->ProcessMessages();

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!

Thanks
0
javock
Asked:
javock
  • 3
  • 3
  • 2
3 Solutions
 
mokuleCommented:

to hide
si.wShowWindow = SW_HIDE;

to capture You must create pipes
wait a moment
0
 
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!

Thanks!
0
KuppingerCole Reviews AlgoSec in Executive Report

Leading analyst firm, KuppingerCole reviews AlgoSec's Security Policy Management Solution, and the security challenges faced by companies today in their Executive View report.

 
mokuleCommented:

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

0
 
AlexFMCommented:
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.
0
 
javockAuthor Commented:
AlexFM,

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

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

Thanks
0
 
AlexFMCommented:
In this case use pipes. Pipe is created using CreatePipe, and ReadFile is used to read process output.

http://support.microsoft.com/default.aspx?scid=http://support.microsoft.com:80/support/kb/articles/q190/3/51.asp&NoWebContent=1
0
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.

Join & Write a Comment

Featured Post

Has Powershell sent you back into the Stone Age?

If managing Active Directory using Windows Powershell┬« is making you feel like you stepped back in time, you are not alone.  For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why.

  • 3
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now