Microsoft KB Archive/317433

= How to suppress the console window for a managed extensions to Visual C++ Windows Forms application =

Article ID: 317433

Article Last Modified on 12/31/2005

-

APPLIES TO


 * Microsoft Visual C++ 2005 Express Edition
 * Microsoft Visual C++ .NET 2003 Standard Edition
 * Microsoft Visual C++ .NET 2002 Standard Edition
 * Microsoft .NET Framework 1.1
 * Microsoft .NET Framework 1.0

-



This article was previously published under Q317433



This article refers to the following Microsoft .NET Framework Class Library namespace:
 * System.Windows.Forms

SUMMARY
 * Method 1
 * Method 2
 * Building from Command Line

REFERENCES



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 .cpp file.  Paste the following code before the main function: using namespace System::Windows::Forms;
 * 1) using 

  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.</ol>
 * 1) using <System.dll>

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
<ol>  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.
 * 1) include &quot;stdafx.h&quot;
 * 2) include &quot;windows.h&quot;


 * 1) using <mscorlib.dll>
 * 2) include <tchar.h>

using namespace System;

using namespace System::Windows::Forms;
 * 1) using <System.Windows.Forms.dll>


 * 1) 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&quot;Hello World&quot;);

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: <ol style="list-style-type: lower-alpha;"> Click Project, and then click  Properties.

Note  is a placeholder for the name of the project.</li> Expand Configuration Properties, and then click General.</li> 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.</li></ol>

For more information about the common language runtime support compiler option, visit the following Microsoft Web site:

/clr (Common Language Runtime Compilation)

http://msdn2.microsoft.com/en-us/library/k8d11d4s.aspx

</li> Compile and run your application.</li></ol>

back to the top

Method 2
<ol> In Solution Explorer, right-click the project name and select Properties.</li> In the left pane of the Properties window, click Linker, and then click System.</li> In the right pane, click the SubSystem drop-down list box, and then select WINDOWS (/SUBSYSTEM:WINDOWS).</li> In the left pane, under Linker, click Advanced.</li> 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: <ol style="list-style-type: lower-alpha;"> Right-click the project, click Properties, and then select General in the left pane.</li> In the Entry Point option, if the character set is multibyte, enter main; if the character set is Unicode, enter wmain.</li></ol> </li> Build and attempt to run the application. Only the form window should appear.</li></ol>

back to the top

Building from Command Line
This article assumes that you have the following code sample in your .cpp file: // sample.cpp


 * 1) using <mscorlib.dll>
 * 2) include <tchar.h>

using namespace System;

using namespace System::Windows::Forms;
 * 1) using <System.Windows.Forms.dll>


 * 1) 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&quot;Hello World&quot;);

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

<div class="references_section">