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

Article translations Article translations
Article ID: 317433 - View products that this article applies to.
This article was previously published under Q317433
Expand all | Collapse all

On This Page

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.
  1. In the Visual C++ integrated development environment (IDE), create a new Managed C++ application.
  2. Open the main <projectname>.cpp file.
  3. Paste the following code before the main function:
    #using <System.Windows.Forms.dll>
    using namespace System::Windows::Forms;
    
    #using <System.dll>
    					
  4. 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();
    					
  5. 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.

Method 1

  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:
    1. Click Project, and then click <ProjectName> Properties.

      Note <ProjectName> is a placeholder for the name of the project.
    2. Expand Configuration Properties, and then click General.
    3. 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:
    /clr (Common Language Runtime Compilation)
    http://msdn2.microsoft.com/en-us/library/k8d11d4s.aspx
  2. Compile and run your application.

Method 2

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

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
				

REFERENCES

For more information, see the Visual Studio .NET Help documentation at the following MSDN Web sites:
/ENTRY (Entry-Point Symbol)
http://msdn.microsoft.com/en-us/library/f9t8842e.aspx
/SUBSYSTEM (Specify Subsystem)
http://msdn.microsoft.com/en-us/library/fcc1zstk.aspx

Properties

Article ID: 317433 - Last Review: December 31, 2005 - Revision: 3.3
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
Keywords: 
kbarchitecture kbfaq kbhowtomaster kbmanaged kbwindowsforms kbwndw KB317433

Give Feedback

 

Contact us for more help

Contact us for more help
Connect with Answer Desk for expert help.
Get more support from smallbusiness.support.microsoft.com