Solved

Execute program and capture output

Posted on 2001-08-15
8
250 Views
Last Modified: 2009-07-29
Hi

I can't quite figure out how to execute an external application and get the output provided by this app for further processing;

Ex:

my_buffer = exec("NET USER");
processusers( (char *)my_buffer );

Hope somebody can help..

0
Comment
Question by:ToHo
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
LVL 6

Expert Comment

by:kotan
ID: 6387426
you can redirect the output to a file.
Then read the file to get output.

exec("NET USER > out.txt");

read("out.txt");
0
 
LVL 2

Author Comment

by:ToHo
ID: 6387625
Yeah, I know, but I was thinking more of redirecting the standard output (con) to my own stream or buffer. This way I don't have to go via another file.
0
 
LVL 5

Expert Comment

by:proskig
ID: 6387662
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 2

Expert Comment

by:bkdc
ID: 6387774
Check out the MSDN

This deals with consoles
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/fileio/hh/winbase/conchar_3vg3.asp

and

this deals with processes

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dllproc/hh/winbase/prothred_3mgj.asp

The most interesting function is CreateProcess()

If you "really" need help I'll give you a sample; currently I'm using QNX and is quite impossible to write a program for you on my machine
0
 
LVL 86

Accepted Solution

by:
jkr earned 100 total points
ID: 6387830
/* POPEN.C: This program uses _popen and _pclose to receive a
 * stream of text from a system process.
 */

#include <stdio.h>
#include <stdlib.h>

void main( void )
{

   char   psBuffer[128];
   FILE   *netuser;

   /* Run 'net user' 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( (netuser = _popen( "net user" )) == NULL )
      exit( 1 );

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

   /* Close pipe and print return value of 'net user'. */
   printf( "\nProcess returned %d\n", _pclose( netuser ) );
}
0
 
LVL 4

Expert Comment

by:AssafLavie
ID: 6388960
// Open the target file. Specify the ability to inherit the handle.
SECURITY_ATTRIBUTES sa = { sizeof(SECURITY_ATTRIBUTES), NULL, TRUE };
HANDLE hOutputFile = CreateFile("OutputFile", GENERIC_ALL, 0, &sa, CREATE_ALWAYS, 0, 0); // Tell the new process to use the file as the standard output.
STARTUPINFO si = {0};
si.dwFlags = STARTF_USESTDHANDLES;
si.hStdOutput = hOutputFile; // Start the process.
PROCESSINFORMATION pi = {0};
HANDLE hProcess = CreateProcess("file.exe", NULL, NULL, NULL, TRUE, 0, NULL, NULL, &si, &pi); // Don't forget.
CloseHandle(hOutputFile);
CloseHandle(hProcess);
0
 
LVL 4

Expert Comment

by:AssafLavie
ID: 6388963
oops, it got all messed up. You can read it here:
http://www.geocities.com/assaflavie/faqs/redirecting_std_output.txt
0
 
LVL 2

Author Comment

by:ToHo
ID: 6389609
Thanks everybody.

The solution proposed by jkr provided exactly what I was trying to do, so I'm awarding the points to him.

Note: _popen( "net user" ) should read _popen( "net user", "r");
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

690 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