VB icon

How to Hide the Console Window

Email
Submitted on: 1/4/2015 9:58:00 AM
By: PlanetCPP-Chris(Hound)/Micheal(xix) (from psc cd)  
Level: Intermediate
User Rating: By 7 Users
Compatibility: C++ (general), Microsoft Visual C++, Borland C++
Views: 1172
 
     Sometimes when your developing an application you come across the need or want to use a console app to get part of the job done, or maybe you have a program where batch files are performing certain actions you need. No matter what the reason, if there's no output then theres no reason for your user to be subjected to a console window being splashed on the screen for a moment, or worse, have it remain visible for a long time. Face it, the console window is nothing pretty to look at, even in XP. Here's a way for you to bypass the console window and shoot straight to main(). There's a function called mainCRTStartup thats sole purpose is to parse the command line arguments and then call main while passing the arguments to main also. In C++ you can set the entry point for you application and if you want to skip straight to main or WinMain (In Windows applications) then you tell it to start at mainCRTStartup or WinMainCRTStartup accordinglly. Most of you will be using different compilers so it won't get into how you set the entry point for every compiler there is, instead we'll use my favorite preprocessor directive #pragma. You can set linking options among other things with this, and all you need to do is add this line above your code: #pragma comment( linker, "/subsystem:\"windows\" /entry:\"mainCRTStartup\"" ) Here is a small example, this program will play the system start sound 3 times then exit.
 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
				
//**************************************
// Name: How to Hide the Console Window
// Description:Sometimes when your developing an application you come across the need or want to
use a console app to get part of the job done, or maybe you have a program where batch
files are performing certain actions you need. No matter what the reason, if there's
no output then theres no reason for your user to be subjected to a console window being
splashed on the screen for a moment, or worse, have it remain visible for a long time.
Face it, the console window is nothing pretty to look at, even in XP.
Here's a way for you to bypass the console window and shoot straight to main().
There's a function called mainCRTStartup thats sole purpose is to parse the command
line arguments and then call main while passing the arguments to main also.
In C++ you can set the entry point for you application and if you want to skip straight
to main or WinMain (In Windows applications) then you tell it to start at mainCRTStartup
or WinMainCRTStartup accordinglly.
Most of you will be using different compilers so it won't get into how you set the entry
point for every compiler there is, instead we'll use my favorite preprocessor directive
#pragma. You can set linking options among other things with this, and all you need to do
is add this line above your code:
#pragma comment( linker, "/subsystem:\"windows\" /entry:\"mainCRTStartup\"" ) 
Here is a small example, this program will play the system start sound 3 times then exit.
// By: PlanetCPP-Chris(Hound)/Micheal(xix) (from psc cd)
//
// Side Effects:do NOT take input with this from the console obviouslly.
//**************************************

#include <windows.h>
#include <mmsystem.h>
#pragma comment(lib,"winmm.lib")
#pragma comment( linker, "/subsystem:\"windows\" /entry:\"mainCRTStartup\"" ) 
int main(void)
{
	PlaySound("SystemStart",NULL, SND_ALIAS|SND_SYNC);
	Sleep(50);
	PlaySound("SystemStart",NULL, SND_ALIAS|SND_SYNC);
	Sleep(50);
	PlaySound("SystemStart",NULL, SND_ALIAS|SND_SYNC);
	Sleep(50);
	return 0;
}


Other 11 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Intermediate category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.