SUMMARY
When you create a Windows Forms based application with
Managed Extensions to C++, you may see a console window appear before the
application window appears. This article describes how to suppress the console
window.
NOTE: This behavior may also occur in unmanaged C++ applications, for
example, if you create a window from a regular, unmanaged console application.
You can use the resolutions that are described in this article for those cases
also.
For example, if you follow these steps, note that as soon as
you compile and run the function, the console window appears in the background
and a Windows Form appears in the foreground.
- In the Visual C++ integrated development environment
(IDE), create a new Managed C++ application.
- Open the main <projectname>.cpp file.
- Paste the following code before the main function:
#using <System.Windows.Forms.dll>
using namespace System::Windows::Forms;
#using <System.dll>
- Inside the main function, comment out the Console::WriteLine statement and paste the following code before the return statement:
Form* testForm = new Form();
testForm->ShowDialog();
- Compile and run the application. Note that a console window
appears in the background and a Windows Form appears in the
foreground.
To suppress the console window, choose one of the following
methods:
- Method 1: Change the _tmain function to a _tWinMain function.
- Method 2: Change the SubSystem and Entry Point options for the linker.
back to the top
Method 1
- Comment out the _tmain function and replace it with a _tWinMain function as shown in the following code. Note that the second
line of code is needed at the beginning of the file.
#include "stdafx.h"
#include "windows.h"
#using <mscorlib.dll>
#include <tchar.h>
using namespace System;
#using <System.Windows.Forms.dll>
using namespace System::Windows::Forms;
#using <System.dll>
// This is the entry point for this application
// int _tmain(void)
int APIENTRY _tWinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPTSTR lpCmdLine,
int nCmdShow)
{
// TO DO: Replace the sample code below with your own code.
// Console::WriteLine(S"Hello World");
Form* testForm = new Form();
testForm->ShowDialog();
return 0;
}
Note You must add the common language runtime support compiler option (/clr:oldSyntax) in Visual C++ 2005 to successfully compile the previous code sample.
To add the common language runtime support compiler option in Visual C++ 2005, follow these steps:
- Click Project, and then click <ProjectName> Properties.
Note <ProjectName> is a placeholder for the name of the project. - Expand Configuration Properties, and then click General.
- Click to select Common Language Runtime Support, Old Syntax (/clr:oldSyntax) in the Common Language Runtime support project setting in the right pane, click Apply, and then click OK.
For more information about the common language runtime support compiler option, visit the following Microsoft Web site:
- Compile and run your application.
back to the top
Method 2
- In Solution Explorer, right-click the project name and
select Properties.
- In the left pane of the Properties window, click Linker, and then click System.
- In the right pane, click the SubSystem drop-down list box, and then select WINDOWS (/SUBSYSTEM:WINDOWS).
- In the left pane, under Linker, click Advanced.
- In the right pane, note an entry called Entry Point, which is for the entry point function name.
The entry
point function name varies, depending on the character set you have chosen for
your project. Because this article uses _tmain as the main function name, the entry point function name is either main or wmain. The _tmain function is defined as either wmain or main, depending on whether _UNICODE is defined in the Tchar.h file.
To identify the
character set that is used:
- Right-click the project, click Properties, and then select General in the left pane.
- In the Entry Point option, if the character set is multibyte, enter main; if the character set is Unicode, enter wmain.
- Build and attempt to run the application. Only the form
window should appear.
back to the top
Building from Command Line
This article assumes that you have the following code sample in
your .cpp file:
// sample.cpp
#using <mscorlib.dll>
#include <tchar.h>
using namespace System;
#using <System.Windows.Forms.dll>
using namespace System::Windows::Forms;
#using <System.dll>
// This is the entry point for this application
int _tmain(void)
{
// TO DO: Replace the sample code below with your own code.
// Console::WriteLine(S"Hello World");
Form* testForm = new Form();
testForm->ShowDialog();
return 0;
}
You normally compile the project as:
cl /clr sample.cpp
A console window may not appear if you are running the application
directly from a command window. If you double-click the program from Windows
Explorer, a separate console window appears.
You can use Method 1
without making any changes to your compilation line. Just replace
_tmain with
_tWinMain as described in Method 1.
Alternatively, you can use
the following compile options, which also produces the expected behavior. Make
sure that you use either
main or
wmain, depending on which character set is used.
cl /clr sample.cpp /link /ENTRY:main /SUBSYSTEM:WINDOWS
back to the top