Hiding batch file's console

This is a follow up on http://www.experts-exchange.com/jsp/qShow.jsp?ta=winprog&qid=10224880 

I have a Win32 console program.  It is complicated but of old-design full of printf's etc.  I need the program to be invisible while running. I do this by calling FreeConsole.  This much works

The question is: the console program also kicks off batch files using system("file.bat");  Each type a batch file runs a console flashes up while it is running.  Is there anyway to hide these consoles.

Any quick fix simple solution gets an A
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Switch from 'system()' to 'CreateProcess()' and use the 'SW_HIDE' flag in the 'STARTUPINFO' struct you're using, e.g.:


ZeroMemory ( &si, sizeof ( STARTUPINFO));

si.cb = sizeof ( STARTUPINFO);
si.wShowWindow = SW_HIDE;

bRes = CreateProcess ( NULL,
pszCmd, // command line, sth. like 'cmd.exe /c file.bat'
GetEnvironmentStrings (),

CloseHandle( pi.hProcess);
CloseHandle( pi.hThread);
Answers2000Author Commented:
setting pszCmd is probably the trick I missed!

i can't test it till Friday, but I'll let you know then or soon after

What does /c option do ?
<locking it knowing that it works> ;-)

The '/c' option makes cmd.exe execute the command and terminate when it's finished. As batch files are no executables but shell command files and a Win32 executable is needed for 'CreateProcess()', 'cmd.exe /c file.bat' creates a console that executes the batch file. '/k' would cause the console not to close after finishing the execution... This might also come in handy if you want to redirect the output of other apps, e.g. by using 'cmd.exe /c myapp.exe >> myapp.txt'

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Development

From novice to tech pro — start learning today.