?
Solved

Execute program and capture output

Posted on 2001-08-15
8
Medium Priority
?
252 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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 400 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
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…
Suggested Courses
Course of the Month13 days, 21 hours left to enroll

801 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