Last Updated:

Managed code development in Visual C++

Managed code development in Visual C++

Managed code development in Visual C++ uses several new keywords, and the C++ compiler extension that enables you to create applications for .NET is called by using the /CLR (Compile to Run in Common Language) option. This option instructs the compiler to use the intermediate IL instruction set in the destination file, rather than the regular processor instruction set. The new keywords are used in managed code generation and are not supported when generating regular unmanaged code. Although the presence or absence of the /CLR (Compile for Execution in Common Language) option completely determines whether the compiler generates managed (in intermediate IL) or unmanaged code, you can specify a compilation mode for individual parts of your program. This is done by means of the #pragma Directive:

#pragma managed
// The following code is compiled as managed
ttpragma unmanaged
// Following code compiles as unmanaged

If you specify the /CLR (Compile for Execution in Common Language) compiler option, if there are no directives #pragma, the source code is compiled as managed by default. If there is no /CLR (Compile for Execution in a Common Language Environment) option, the directive #pragma compiler are ignored and the code is compiled as unmanaged.

To use extensibility features, you insert a fusing directive into the source file that specifies the assembly mscorlib.dll that contains the type information that is required for managed code to work. Such assemblies are an extension for the .NET platform and usually consist of DLL (or EXE) files. In addition, it is almost always determined that the System namespace will be used; this, however, is not necessary for the use of managed code.

The concept of namespaces in C++ directly copies the concept of the namespaces of the multilingual .NET framework, which is a hierarchy of names. These two aspects of .NET code development require that the following two lines be included at the beginning of the source file:

fusing <mscorlib.dll>
// Required for managed C++ code
using namespace System;
// use namespace System
// Not required, but usually used

The fusing preprocessor directive is similar to the #import directive in earlier versions of Visual C++ in that it makes type information available to the compiler. In the case of the #import directive, type information was contained in type libraries, which were usually TLB, DLL, OCX, or EXE files. In the case of the #using directive, the type information is in the form of metadata contained in the .NET assembly.

The mscorlib.dll assembly contains the type information required by all .NET applications, including information about the base class that is the ancestor of all managed classes, the System::Object class. Note that in this entry, System denotes the namespace, and Object is the name of the root class of the managed type hierarchy.

Your First Managed C++ .NET Program


While you're almost certainly very familiar with C++, we'll start by looking at a very simple but traditional example: helloWorld. In this section, we'll show you how to write, compile, and run this and other programs.

HelloWorld Program

Just below is an example of code from a very simple managed program that prints a single line to the console. You can open the accompanying solution or create your own project and enter the text of the program yourself. To do this, you need to create an empty HelloWorld project, add the source code, and then compile and run the project.

How to: Create a Console Application in Managed C++

Create an empty Managed C++ console application project called HelloWorld:

  1. Open Visual Studio .NET. Select File › New › Project to open the New Project dialog.
  2. Select Visual C++ Projects from the Project Types list.
  3. Select Managed C++ Empty Project from the Templates list.
  4. Enter HelloWorld as the project name.
  5. Specify the folder where the project will be stored.
  6. Click OK to close the New Project dialog box and finish creating a new project. Add the source code:
  7. Right-click the Source Files folder in solution explorer.Select Add › Add New Item to open the Add New Item dialog.
  8. In the Templates list, click C++ File.
  9. Specify HelloWorld as the project name.
  10. Do not change the default Location value.
  11. Click the Open button to close the Add New Item dialog and open the Source Editor.
  12. Enter the HelloWorld sample code. Compile and run the project:
  13. Select the menu item Build › Build.
  14. Use CTRL+F5 to run the program without a debugger.

The fusing directive is required for all managed C++ programs. It makes available to the compiler the standard types (such as Console and Object) defined in the NET class library. The Console class is in the System namespace and its fully qualified name is System::Console This class contains a WnteLine method that outputs text to the console and adds a newline character to it.

fusing <mscorlib.dll> // required for managed St+ code
void main(void) {
System::Console::WriteLine("Hello World");
// ("Hello World"); }

A program can be compiled either in Visual Studio .NET or by using the command line with the /CLR (Common Language Runtime compilation) option. If you are using the command line. The easiest way to do this is to open a command window by selecting Start › Programs › Microsoft Visual Studio .NET 7.0 › Visual Studio .NET Tools › Visual Studio .NET Command Prompt. At the command prompt:

cl /CLR HelioWorld.cpp

The source file is compiled and then automatically composed so that the result is a HelloWorld.exe EXE file. Later, we'll show you how to create a managed dynamic link library (DLL).

You can run the resulting managed program in Visual Studio .NET or from the command line as a regular executable. The result of the program will be the following message:

hello world
(Hello World)