Click here to Skip to main content
15,867,704 members
Articles / Programming Languages / C++
Article

Running console applications silently

Rate me:
Please Sign up or sign in to vote.
4.98/5 (27 votes)
3 Jul 2002CPOL2 min read 347K   6.6K   101   30
Shows how to run a console application without showing the window.

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.

DWORD RunSilent(char* strFunct, char* strstrParams)
{
	STARTUPINFO StartupInfo;
	PROCESS_INFORMATION ProcessInfo;
	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");

	if(pEnvCMD){
		
		strcpy(Args, pEnvCMD);
	}
	else{
		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,
		CREATE_NEW_CONSOLE, 
		NULL, 
		NULL,
		&StartupInfo,
		&ProcessInfo))
	{
		return GetLastError();		
	}

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

	CloseHandle(ProcessInfo.hThread);
	CloseHandle(ProcessInfo.hProcess);

	return rc;
	
}

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
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

 
GeneralMy vote of 5 Pin
Member 1346504620-Oct-19 10:30
Member 1346504620-Oct-19 10:30 
PraiseThanks just what I needed Pin
DavidBaird14-Oct-18 13:38
DavidBaird14-Oct-18 13:38 
Questionhow to make a TSR app with c#? or change app to do like an TSR app? Pin
nashalj20-Oct-14 6:22
nashalj20-Oct-14 6:22 
AnswerRe: how to make a TSR app with c#? or change app to do like an TSR app? Pin
Steven Szelei4-Nov-15 10:04
Steven Szelei4-Nov-15 10:04 
QuestionI did not understand the thing about the flag Pin
mark lajom20-Jul-11 22:32
mark lajom20-Jul-11 22:32 
GeneralHere's how to do it from C# Pin
FaxedHead10-Aug-08 18:23
FaxedHead10-Aug-08 18:23 
QuestionRead console outputs and send instructions Pin
723Alex30-Mar-07 5:57
723Alex30-Mar-07 5:57 
GeneralThanks! Pin
MindWanderer19-Jul-06 23:17
MindWanderer19-Jul-06 23:17 
Questionhide console window in a console application at start Pin
laleh_sg14-Jul-06 21:01
laleh_sg14-Jul-06 21:01 
GeneralI like it! Pin
John R. Shaw5-Jan-06 12:59
John R. Shaw5-Jan-06 12:59 
Questionanother method... Pin
leojose30-Dec-05 23:57
leojose30-Dec-05 23:57 
GeneralRunning aplication with desired size. Pin
mukta_here_11-Jul-05 4:05
mukta_here_11-Jul-05 4:05 
GeneralRe: That is not working in Windows XP at all. Pin
Shawn, Han2-Jan-06 19:52
Shawn, Han2-Jan-06 19:52 
GeneralRunning aplication with desired size. Pin
mukta_here_11-Jul-05 3:58
mukta_here_11-Jul-05 3:58 
GeneralInvoking a command line .exe program Pin
Hemanth Balaji18-May-05 16:26
Hemanth Balaji18-May-05 16:26 
GeneralPassing instruction to the createprocess command prompt Pin
vgandhi13-Aug-03 13:49
vgandhi13-Aug-03 13:49 
GeneralI know of a much simpler way... Pin
Nizz6-Jul-03 12:12
Nizz6-Jul-03 12:12 
GeneralRe: I know of a much simpler way... Pin
Steven Szelei6-Jul-03 12:48
Steven Szelei6-Jul-03 12:48 
QuestionWhat about hiding/showing myself ? Pin
posbis20-May-03 14:25
posbis20-May-03 14:25 
AnswerRe: What about hiding/showing myself ? Pin
Steven Szelei20-May-03 14:47
Steven Szelei20-May-03 14:47 
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.

StevenEek! | :eek:
AnswerRe: What about hiding/showing myself ? Pin
Steven Szelei20-May-03 14:51
Steven Szelei20-May-03 14:51 
GeneralRe: What about hiding/showing myself ? Pin
posbis23-May-03 6:55
posbis23-May-03 6:55 
Generalseems complicated Pin
evilman29-Jan-03 5:35
evilman29-Jan-03 5:35 
GeneralJust a quick note Pin
Marc Bednar9-Jul-02 2:27
Marc Bednar9-Jul-02 2:27 
GeneralPipes Pin
comrade8-Jul-02 17:51
comrade8-Jul-02 17:51 

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

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