Introduction

Arrrgh! So you need to run your console app via a primary application, well ye've come to the right place. This article is intended for those who need to run one of those pesky little console applications without the console window popping up over your main app or worse popping up when calling it from a service.

So now matey lets dive into it and see how to avoid the console windows from sinking our application. Prepare the app for silent running.

Walking the Plank

Ok! That's enough of the pirate stuff. On with the show..

You have probably searched the web and have seen many developers show examples of how to spawn applications using the CreateProcess function. Well I shall do the same thing but with a twist. I will do it in such a way that the application that you would like to spawn does not show up when run.

There are several caveats to this process and many gotcha's. The main one is that if this program you wish to run is interactive, that is requires input from the user outside of what can be passed in through the command line, then blindly using this code will cause your program to hang. To recap! How can you input information into a program if you can't see the window. Ok! Think you get the point.

There are ways of doing this but they will not be covered in this article.

Running Silent

The trick to running a console window silent is burried in the STARTUPINFO structure that we will populate then pass into the CreateProcess function. STARTUPINFO specifies the main window properties. There are many items in the
STARTUPINFO structure that we just don't care about. The ones that are of interest to us are the

DWORD cb

DWORD dwFlags

WORD wShowWindow

First the STARTUPINFO is instantiated

STARTUPINFO StartupInfo;

then the memory is cleared for the length of the structure.

memset(&StartupInfo, 0, sizeof(StartupInfo));

or

::ZeroMemory(&StartupInfo, sizeof(StartupInfo));

If this is not done our data within our STARTUPINFO object could be corrupt rendering it useless and possible crashing the application.

Next we will fill our structure with the relevant code that will tell our console window to start up without showing itself.

// set the size of the structure
StartupInfo.cb = sizeof(STARTUPINFO);
// tell the application that we are setting the window display
// information within this structure
StartupInfo.dwFlags = STARTF_USESHOWWINDOW;
// set the window display to HIDE
StartupInfo.wShowWindow = SW_HIDE;

Well that's not much of a trick but remember unless you specify the dwFlags to the flag STARTF_USESHOWWINDOW the setting in wShowWindow will not be recognized and your window will not be hidden.

Davy Jones' Locker

Well this is where I will leave you to it.

To test this use the function source below and paste it into your application or Download the project file above and test it out. The application will open the IPConfig.exe application in a hidden window, pipe the text to a file and then display the contents of that file in the main application window.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

Comments and Discussions

Hi everybody
I want to write an app to translate character between two language(decoder), so I need a TSR app that run behind of windows and by pressing some special keys(ctrl+shift+N),the app get the text and translate it to desired language and leave the decoded text into clipboard.
I write this APP in windows form but i don't know how to change it to TSR.
anyone can help me to do it/
thanks in advanced

Maybe somebody has done running console app from C++ with capability to read console messages while running and to send data to console such as Y or N character to continue. I found one codeproject ""To Execute a Console Application From VC++ and retrieve the messages shown in the console"". It works for reading but I was not able to send my instructions back.

I have a problem ..I would be so grateful to know your idea about it..i have a console application for monitoring some packets on a port..this app is with support of MFC in order to have a UI and etc.Now I need to hide console window at the beginning of running the app..It will be shown to user later if he/she want to.I can get handle of console window and give it to the API showwindow..but this does'nt work well for me because at the beginning of running user will see something like flashing of console window which is not desirable..How do you think I can do it?
Thanks

I have created a win32 Application using Visual c++. How to execute a command line program from within a windows program. I am using Win32 api in C. I have a DICOM Image file and I want to convert it to a bmp file by running a command line option pOovided by InfraView, by clicking a button ON MY PROGRAM.

I wanna execute a command like "i_view32.exe c:\test.bmp /convert=c:\test.jpg" ....

Hi there,
I wanted to ask you if we use the createprocess command for some command line function and if that function requires a prompt for (y/n) confirmation how do we pass in yes or no to that command prompt window. for example the cacls function if i have something where my strcommand looks like
cacls temp \t \g user:W and i make a call to
CreateProcess(NULL, // command is part of input string
strcommand , // (writeable) command string
NULL, // process security
NULL, // thread security
TRUE, // inherit handles flag
0, // flags
NULL, // inherit environment
NULL, // inherit directory
&startup, // STARTUPINFO
&procinfo);
now if i want to type in Y at the confirmation window so that the user doesnt have a choice how do I do that. The application that I am running will call createprocess and give the user a chance to type Y Please let me know thanks

Well, no it's not that simple. What if you deploy a system with this code on Windows Me/98/95? This value is not supported.
Thanks for the added info though, I didn't know about this one. But then there are so many flags in windows it's hard to know them all.

OK what about your own running console process? I know nothing about it.
How should a console app change it's appearance. It is a console application not a GUI application. The skin will never chage. You can change it's Opacity or the window shape, or the border, menu, or app bar colors.

What are the specifications for when the window will show or hide. This is realy up to you. I would have a function variable that I would send to the function to tell the window wether to show or hide itself when I call it.

OK what about your own running console process? I know nothing about it.
How should a console app change it's appearance. It is a console application not a GUI application. The skin will never chage. You can change it's Opacity or the window shape, or the border, menu, or app bar colors.

Ok when would you want to write a console application that would show and hide itself. This sounds like you would like to write a TSR or service that has a front end GUI app to administer it. I do not think you would want to run a consol app in the backgroud. Look into services build with ATL with interfaces, then build a front end GUI that tie into the service interfaces.

Don't know if you got my email but to make things a little bit clear. I want to hide/unhide an already running process. Your article is about starting new processes with SW_SHOW or SW_HIDE flags. I want to do the same but for an already running process. So I want to change the state of a process. My process is not quite a service it is an executable (named pipe server) which ist started by another application for the time this application is running. Before the application is finishing, it send the QUIT command to the Pipe Server waits for the acknowledge and the exits itself.

So you see. It is not a full service and I don't want all the troubles you have when using a service (I mean you first must install it, you need special access rights, ...). It just should start up, stay there and quit after it receives the QUIT command.

May be you have an idea on how to change the state of a process. Could "OpenProcess" give some help ?

I wrote this because I see the question asked by begenners fairly often. I did not want to go further into obtaining the output than what was done in the sample app because that was not the intent. I wanted to give a quick and easy to understand way of running the console without it popping up. If you would like to see a way of getting the console content read the message below it will point you to an article that shows a way of doing this. In a way I did use pipes. I piped the text to a file using the standard DOS method ">>". I know it's not what you had in mind. If I find time in the next few weeks I will write an article showing how to obtain the output of a console window whether it uses stdin/stdout or not. Thanks.