What is the best WinAPI function to use when you only want to run a simple shell command like hg > test.txt
?

- 144,682
- 38
- 256
- 465

- 30,618
- 31
- 128
- 208
3 Answers
To simply run a file, then ShellExecute()
and CreateProcess()
are the best options.
As you want to redirect output to a file/run a shell command, it complicates things...
Output redirection is a feature of the command prompt, and as such, the command you want to run needs to be passed to cmd.exe
(on NT/XP+) passing /c
and your command as the parameters (either ShellExecute
or CreateProcess
will do).
cmd /c "ipconfig >c:\debug\blah.txt"
The best way however is to use CreateProcess()
and create your own pipes to talk to the stdin and stdout of the program (This is all cmd
does internally)

- 23,876
- 7
- 71
- 156
-
Oh, this has already been answered on the almost duplicate [followup question](http://stackoverflow.com/questions/7018228/correctly-using-createprocess/7018258#7018258)... – Deanna Aug 11 '11 at 08:15
You could use ShellExecute()
, but why not try system()
first? I am not so sure that ShellExecute()
can actually do piping or redirection. There is also CreateProcess()
, but that requires a bit more work. CreateProcess()
gives you the best control, though.

- 28,387
- 5
- 46
- 94
-
I tried using CreateProcess. I've a question: why does `CreateProcess(0, "notepad.exe test.txt", ...)` work but `CreateProcess(0, "hg > test.txt", ...)` does not? – Paul Manta Aug 10 '11 at 21:35
-
CreateProcess doesn't handle redirection. See [my answer](http://stackoverflow.com/questions/7017680/run-a-simple-shell-command/7022711#7022711) for more details. – Deanna Aug 11 '11 at 08:09
-
CreateProcess doesn't act like a command line, as he tried to use it, indeed. – Rudy Velthuis Aug 11 '11 at 08:19
There are two ways of issuing commands: the Windows Shell way, and the command line way.
Windows Shell issues commands by executing verbs on files. Verbs are associated with file types in the registry. Examples of common verbs are Open and Print. The WinAPI to use for this is ShellExecute. Windows Shell does not help you pipe the output of a process to a file. You can do it using CreateProcess, but it is a little bit involved.
The command line way is to use the system function.

- 13,814
- 3
- 48
- 61
-
-1. Jonathan pointed out nothing relevant. The strange windows-verb-thing works with system(), too. Try `#include
int main () { system ("bitmap.bmp"); }`, assuming a bitmap file named "bitmap.bmp". Maybe *you* can point out the benefit of ShellExecute over system in this case? (if there is, I would be the last one not to vote you up) – Sebastian Mach Aug 11 '11 at 08:43 -
@phresnel, dude, this is not a contest. Paul asked about the WinAPI way of issuing commands and I explained it. As for your example, it does not even execute a verb. Verbs are registered for file types. For instance, the Word.Document.12 file type has associated verbs *Edit*, *New*, *OnenotePrintto*, *Open*, *OpenAsReadOnly*, *Print*, *Printto*, and *ViewProtected*. – Don Reba Aug 11 '11 at 09:00
-
Even though I am not your "dude", I must apologize. I confused verbs with the standard action ("double-click") associated with files; however, in _this_ case, I don't see how `system()` is worse than `ShellExecute` – Sebastian Mach Aug 11 '11 at 09:19
-
It is not about being better or worse. Paul asked about issuing commands using WinAPI. WinAPI issues commands using verbs. – Don Reba Aug 11 '11 at 09:24
-
Though 45 minutes ago, you yourself considered `system` as a worse answer: `However, as Jonathan pointed out, this has some drawbacks`, so I was not the one starting to judge ;). After that edit, of course, my -1-justification vanishes. Sidenote: One coulr also consider `system` to be part of the OS-API. – Sebastian Mach Aug 11 '11 at 10:25