views:

1113

answers:

5

I'm using ShellExecuteEx to execute a command in C. Is there a way to use ShellExecuteEx and capture standard in/out/err?

Note: I don't want to use CreateProcess.

+2  A: 

That's not possible. ShellExecute(Ex) basically executes the application in the context of the shell - so you are basically doing what explorer does.

Capturing STDIN and STDOUT is something the shell generally doesn't do, you you will have to go the CreateProcess route (which, after all, is what ShellExecute eventually calls if the file to execute is a program and the verb is 'open').

pilif
+2  A: 

No. The only way to do this is to use CreatePipe and CreateProcess. See the MSDN article here

Bob Moore
+1  A: 

As mentioned by pilif and Bob, you need to use CreateProcess.

If you want code that wraps it all up for you, I do have a class for this exact issue at:

http://code.google.com/p/kgui/source/browse/trunk/kguithread.cpp.

The class ( kGUICallThread ) handles linux / mac / and windows versions. The code is licensed LGPL.

KPexEA
A: 

I use to found the problem like you.

Suppose, You want to capture the output from STDOUT that it's generated by dir command and save the captured into out.txt.

  1. Use text editor and type dir > out.txt and save it with mybat.bat (*.bat, don't *.txt)

  2. In your c/c++ program, type WinExec("mybat.bat", SW_HIDE); and run your application.

  3. Open the out.txt you will see the name of folders and files in current directory.

Also, you can run any executable files (*.exe) at the same way as follow.

xxx.exe > out.txt

I hope it can be helps you. Sorry, my English really not good.

A: 

CreateProcess is what most people use.

You may also want to consider using _popen

http://msdn.microsoft.com/en-us/library/96ayss4b%28VS.80%29.aspx

Jason