1
votes

When trying to execute cmd.exe with the SW_HIDE paramter:

ShellExecute(NULL, "open", "cmd.exe", NULL, NULL, SW_HIDE);

cmd.exe is actually executed hidden! I don't understand why this happened, does cmd.exe's console window considered to be a "normal" window that accepts the nCmdShow parameter, or does cmd.exe when executed sees that I passed SW_HIDE for the nCmdShow parameter and decides not to show the console window?

Note: I tried this code on Windows 7.

1
CREATE_NO_WINDOW is the way to do this. Console mode apps don't get a chance to create windows. The system handles that. ShellExecute is for shell verbs. For creating processes use CreateProcess. - David Heffernan
@David Heffernan So when executing cmd.exe using CreateProcess() with the CREATE_NO_WINDOW parameter, the system will not create any console window for cmd.exe? - James
Correct. That's the right way to do it. - David Heffernan

1 Answers

2
votes

The implication is that cmd.exe simply uses the nCmdShow argument to WinMain (or that it uses SW_SHOWDEFAULT which means the same thing) when showing its window via ShowWindow, without checking or modifying the value first.

It's not documented that it will work and so you shouldn't rely on it, but it's a nice side effect that can be a good way to run batch scripts without a visible window appearing.

Of course if you actually launch an interactive cmd.exe in a hidden window it will be quite difficult to ever make it visible :)