Click here to Skip to main content
Click here to Skip to main content

Running console applications silently

, 3 Jul 2002
Rate this:
Please Sign up or sign in to vote.
Shows how to run a console application without showing the window.
<!-- Download Links --> <!-- Add the rest of your HTML here -->


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));
  ::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.

DWORD RunSilent(char* strFunct, char* strstrParams)
	char Args[4096];
	char *pEnvCMD = NULL;
	char *pDefaultCMD = "CMD.EXE";
	ULONG rc;
	memset(&StartupInfo, 0, sizeof(StartupInfo));
	StartupInfo.cb = sizeof(STARTUPINFO);
	StartupInfo.dwFlags = STARTF_USESHOWWINDOW;
	StartupInfo.wShowWindow = SW_HIDE;

	Args[0] = 0;

	pEnvCMD = getenv("COMSPEC");

		strcpy(Args, pEnvCMD);
		strcpy(Args, pDefaultCMD);

	// "/c" option - Do the command then terminate the command window
	strcat(Args, " /c "); 
	//the application you would like to run from the command window
	strcat(Args, strFunct);  
	strcat(Args, " "); 
	//the parameters passed to the application being run from the command window.
	strcat(Args, strstrParams); 

	if (!CreateProcess( NULL, Args, NULL, NULL, FALSE,
		return GetLastError();		

	WaitForSingleObject(ProcessInfo.hProcess, INFINITE);
	if(!GetExitCodeProcess(ProcessInfo.hProcess, &rc))
		rc = 0;


	return rc;


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.

A list of licenses authors might use can be found here


About the Author

Steven Szelei
CEO Communications Systems Distribution Group
United States United States
I have worked in the field of Information Technology since 1994 and with a number of development languages including Assembly, C++, C#, PHP, and Python.

Comments and Discussions

Questionhow to make a TSR app with c#? or change app to do like an TSR app? Pinmembernashalj20-Oct-14 7:22 
QuestionI did not understand the thing about the flag Pinmembermark lajom20-Jul-11 23:32 
GeneralHere's how to do it from C# PinmemberFaxedHead10-Aug-08 19:23 
QuestionRead console outputs and send instructions Pinmember12kaunas30-Mar-07 6:57 
GeneralThanks! PinmemberMindWanderer20-Jul-06 0:17 
Questionhide console window in a console application at start Pinmemberlaleh_sg14-Jul-06 22:01 
GeneralI like it! PinmemberJohn R. Shaw5-Jan-06 13:59 
Questionanother method... Pinmemberleojose31-Dec-05 0:57 
GeneralRunning aplication with desired size. Pinmembermukta_here_11-Jul-05 5:05 
Hello forum,
I want to run an application with the dimension that is specified in the code. The code is much similar to the code for 'Running console applications silently' The some changes made are as follows --

StartupInfo.wShowWindow = SW_SHOW;
StartupInfo.dwY = 100;
StartupInfo.dwX = 200;
StartupInfo.dwXSize = 100;
StartupInfo.dwYSize = 100;
char pRunAppli = "c:\\Project1.exe";
CreateProcess( NULL, pRunAppli, NULL, NULL, FALSE,
The result is the position is getting set at the specified location but i am not able to adjust the size.
Can someone help me in this regard?
Thanks & Regards,
Mukta Sathaye.
GeneralRe: That is not working in Windows XP at all. PinmemberShawn, Han2-Jan-06 20:52 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150327.1 | Last Updated 4 Jul 2002
Article Copyright 2002 by Steven Szelei
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid