Solved

Execute program and capture output

Posted on 2001-08-15
8
244 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
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
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 
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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
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 clear a vector as well as how to detect empty vectors in C++.

785 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