Solved

Capturing and hiding console output!

Posted on 2004-04-30
8
2,082 Views
Last Modified: 2007-12-19
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
Comment
Question by:javock
  • 3
  • 3
  • 2
8 Comments
 
LVL 17

Expert Comment

by:mokule
ID: 10964335

to hide
si.wShowWindow = SW_HIDE;

to capture You must create pipes
wait a moment
0
 
LVL 17

Assisted Solution

by:mokule
mokule earned 300 total points
ID: 10964366
0
 

Author Comment

by:javock
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!

Thanks!
0
 
LVL 17

Assisted Solution

by:mokule
mokule earned 300 total points
ID: 10964587

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

0
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
LVL 48

Expert Comment

by:AlexFM
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.
0
 

Author Comment

by:javock
ID: 10967130
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
 

Author Comment

by:javock
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.

Thanks
0
 
LVL 48

Accepted Solution

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

http://support.microsoft.com/default.aspx?scid=http://support.microsoft.com:80/support/kb/articles/q190/3/51.asp&NoWebContent=1
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
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 how to use strings and some functions related to them in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.

758 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

18 Experts available now in Live!

Get 1:1 Help Now