Solved

Execute program and capture output

Posted on 2001-08-15
8
246 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
Industry Leaders: 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

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…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

733 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