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



The information in this article applies to:

  • Microsoft Visual C++ 2005 Express Edition
  • Microsoft Visual C++ .NET (2003)
  • Microsoft Visual C++ .NET (2002)
  • 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

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.
back to the top

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.
back to the top

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

REFERENCES

For more information, see the Visual Studio .NET Help documentation at the following MSDN Web sites:
back to the top

Modification Type:MajorLast Reviewed:12/31/2005
Keywords:kbArchitecture kbHOWTOmaster kbManaged kbWindowsForms kbWndw KB317433 kbAudDeveloper