Suppress console when calling "system" in C++

Dana picture Dana · Nov 26, 2009 · Viewed 9.3k times · Source

I'm using the system command in C++ to call some external program, and whenever I use it, a console window opens and closes after the command finishes.

How can I avoid the opening of a console window? I would be happy if the solution could be platform-independent. I would also like for my program to wait until the command is finished.

Answer

Tim Visée picture Tim Visée · Mar 13, 2012

This is probably the easiest and maybe the best way, this will also make it so that your program doesn't freeze while running this command. At first don't forget to include the Windows header using;

#include <Windows.h>

Then you need to use the following function to run your command;

WinExec("your command", SW_HIDE); 

Note; The WinExec method has been deprecated for over a decade. It still works fine today though. You shouldn't use this method if not required.

... instead of the way you don't want to use;

system("your command");