Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Win32 project - Open cmd & print output

Status
Not open for further replies.

AtomicChip

Programmer
May 15, 2001
622
CA
Hey all,

I'm assuming this can be done with the system("cmd") call or something like that.. Only problem is, how to I then turn around and get the handle to that window, or output text to it?

I'm assuming it's something pretty straightforward that I'm missing...

-----------------------------------------------
"The night sky over the planet Krikkit is the least interesting sight in the entire universe."
-Hitch Hiker's Guide To The Galaxy
 
I'm assuming you're asking how you can get the output of a command you run with system("cmd")...?

If that's what you want to do, use popen() instead of system(). Here's the example in the MSDN:
Code:
#include <stdio.h>
#include <stdlib.h>

void main( void )
{

   char   psBuffer[128];
   FILE   *chkdsk;

   /* Run DIR so that it writes its output to a pipe. Open this
    * pipe with read text attribute so that we can read it 
    * like a text file. 
    */
   if( (chkdsk = _popen( "dir *.c /on /p", "rt" )) == NULL )
      exit( 1 );

   /* Read pipe until end of file. End of file indicates that 
    * CHKDSK closed its standard out (probably meaning it 
    * terminated).
    */
   while( !feof( chkdsk ) )
   {
      if( fgets( psBuffer, 128, chkdsk ) != NULL )
         printf( psBuffer );
   }

   /* Close pipe and print return value of CHKDSK. */
   printf( "\nProcess returned %d\n", _pclose( chkdsk ) );
}
 
Thanks - that's a starting point, but it won't work for my app...

Here's a quote from that same page:
Note If used in a Windows program, the _popen function returns an invalid file pointer that will cause the program to hang indefinitely. _popen works properly in a Console application.

This seems to be the proper way of doing it:
-----------------------------------------------
"The night sky over the planet Krikkit is the least interesting sight in the entire universe."
-Hitch Hiker's Guide To The Galaxy
 
As I understood, AtomicChip wants to have a console-like output window for his windowed application. I would like to know such type of built in windows control too. Until now I have done it with simply static or edit control performing all console scrolling by myself.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top