5,692,513 members and growing! (20,795 online)
Email Password   helpLost your password?
Platforms, Frameworks & Libraries » Win32/64 SDK & OS » General     Intermediate

Running console applications silently

By Steven Szelei

Shows how to run a console application without showing the window.
VC6, VC7, C++Windows, Win2K, WinXPVS6, Visual Studio, Dev

Posted: 3 Jul 2002
Updated: 3 Jul 2002
Views: 135,289
Bookmarked: 55 times
Announcements
Loading...



Search    
Advanced Search
Sitemap
22 votes for this Article.
Popularity: 6.08 Rating: 4.53 out of 5
0 votes, 0.0%
1
0 votes, 0.0%
2
1 vote, 5.9%
3
1 vote, 5.9%
4
15 votes, 88.2%
5

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


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.
Occupation: Chief Technology Officer
Company: BabySoft
Location: United States United States

Other popular Win32/64 SDK & OS articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 25 of 25 (Total in Forum: 25) (Refresh)FirstPrevNext
GeneralHere's how to do it from C#memberFaxedHead19:23 10 Aug '08  
QuestionRead console outputs and send instructionsmember12kaunas6:57 30 Mar '07  
GeneralThanks!memberMindWanderer0:17 20 Jul '06  
Questionhide console window in a console application at startmemberlaleh_sg22:01 14 Jul '06  
GeneralI like it!memberJohn R. Shaw13:59 5 Jan '06  
Questionanother method...memberleojose0:57 31 Dec '05  
GeneralRunning aplication with desired size.membermukta_here_15:05 1 Jul '05  
GeneralRe: That is not working in Windows XP at all.memberShawn, Han20:52 2 Jan '06  
GeneralRunning aplication with desired size.membermukta_here_14:58 1 Jul '05  
GeneralInvoking a command line .exe programmemberHemanth Balaji17:26 18 May '05  
GeneralPassing instruction to the createprocess command promptmembervgandhi14:49 13 Aug '03  
GeneralI know of a much simpler way...memberNizz13:12 6 Jul '03  
GeneralRe: I know of a much simpler way...memberSteven Szelei13:48 6 Jul '03  
GeneralWhat about hiding/showing myself ?memberposbis15:25 20 May '03  
GeneralRe: What about hiding/showing myself ?memberSteven Szelei15:47 20 May '03  
GeneralRe: What about hiding/showing myself ?memberSteven Szelei15:51 20 May '03  
GeneralRe: What about hiding/showing myself ?memberposbis7:55 23 May '03  
Generalseems complicatedmemberevilman6:35 29 Jan '03  
GeneralJust a quick notememberMarc Bednar3:27 9 Jul '02  
GeneralPipesmembercomrade18:51 8 Jul '02  
GeneralRe: PipesmemberSteven Szelei3:22 9 Jul '02  
GeneralRe: Pipessussmack.li2:31 20 May '03  
GeneralRe: PipesmemberSteven Szelei16:00 20 May '03  
GeneralAnother article on the subject.memberJonathan de Halleux23:17 3 Jul '02  
GeneralRe: Another article on the subject.memberAnonymous17:55 5 Jul '02  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 3 Jul 2002
Editor: Chris Maunder
Copyright 2002 by Steven Szelei
Everything else Copyright © CodeProject, 1999-2008
Web13 | Advertise on the Code Project