Run a simple shell command

What is the best WinAPI feature used when you only want to run a simple shell command, for example hg > test.txt ?

+6
source share
3 answers

To just run the file, ShellExecute() and CreateProcess() are the best options.

How do you want to redirect the output to a file / run a shell command, this complicates the situation ...

Output redirection is a command line function, and so the command you want to run must be passed to cmd.exe (on NT / XP +) passing through /c , and your command as parameters (either ShellExecute or CreateProcess ).

 cmd /c "ipconfig >c:\debug\blah.txt" 

It is best to use CreateProcess() and create your own channels to talk to stdin and stdout of the program (all this cmd does internally)

+6
source

You can use ShellExecute() , but why not try system() first? I'm not sure if ShellExecute() can actually do piping or redirection. There is also CreateProcess() , but this requires a bit more work. CreateProcess() gives you better control.

+2
source

There are two ways to issue commands: the Windows shell path and the command line method.

Windows Shell issues commands by executing verbs in files. Verbs are associated with file types in the registry. Examples of common verbs are Open and Print. WinAPI uses ShellExecute for this. Windows Shell will not help you transfer the output of a process to a file. You can do this with CreateProcess , but it is a bit involved .

The command line uses the system function.

+1
source

Source: https://habr.com/ru/post/894764/


All Articles