Link to home
Start Free TrialLog in
Avatar of ToHo
ToHo

asked on

Execute program and capture output

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

Avatar of kotan
kotan
Flag of Malaysia image

you can redirect the output to a file.
Then read the file to get output.

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

read("out.txt");
Avatar of ToHo
ToHo

ASKER

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.
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
ASKER CERTIFIED SOLUTION
Avatar of jkr
jkr
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
// 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);
oops, it got all messed up. You can read it here:
http://www.geocities.com/assaflavie/faqs/redirecting_std_output.txt
Avatar of ToHo

ASKER

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");