Hello World in Managed Extensions for C++

The following code shows how Hello World looks in Managed Extensions for C++.

Listing 1. Hello World in Managed Extensions for C++ (HelloVC.cpp)

#using <mscorlib.dll>

// Allow easy reference to the System namespace classes.
using namespace System;

// The global function, main, is the application's entry point.
void main() {
   // Write text to the console.
   Console::WriteLine(S"Hello World using Managed Extensions for C++!");
}

Even though the entire program is only a few lines of code, there are several things worth noticing, beginning with the following line:

#using <mscorlib.dll>

In Managed Extensions for C++, the #using directive is similar to the #import directive, which is used to incorporate information from a type library. Note that these are different from the #include directive, which is for incorporating source code rather than prebuilt libraries. Also, to import the namespace into the program — in other words, to make it convenient to reference System objects without having to fully qualify their paths — the following additional statement is required:

using namespace System;

Next, consider the following line:

void main() {

Although the entry point main takes no command-line arguments, this can obviously be enhanced for nontrivial programs. The entry point also does not return anything, although it is possible to modify the function to return a single, 32-bit numeric value to be used as an exit code.

The next line is the following:

Console::WriteLine(S"Hello World using Managed Extensions for C++!");

The core of the program, this line writes a string using the runtime Console type. The Console type can be used by the Read, ReadLine, Write, and WriteLine methods for both input and output of any string or numeric value. As mentioned earlier, the double-colon in Console::WriteLine is required in Managed Extensions for C++ to indicate the scope. The double colon separates a namespace from a class name as well as a class name from a static method. Finally, the S in front of the string tells the compiler to make this a System::String*, which has better performance in managed code than C++ string literals.

The Build.bat file contains the following line, which is all that is necessary to build this program:

cl.exe /Zi /clr HelloVC.cpp

The first item of note is the /clr switch, which tells the compiler to create managed code, as required by the runtime. Running Build.bat generates the following output:

C:\...\HelloWorld\vc>build
C:\...\HelloWorld\vc> cl.exe /Zi /clr HelloVC.cpp
Microsoft (R) C/C++ Optimizing Compiler...
Copyright (C) Microsoft Corporation 1984-2001. All rights reserved.

HelloVC.cpp
Microsoft (R) Incremental Linker ...
Copyright (C) 1992-2001 Microsoft Corporation.  All rights reserved.

/out:HelloVC.exe
/debug
HelloVC.obj 

Finally, running the resulting executable file yields the following output:

C:\...\HelloWorld\vc>hellovc
Hello World using Managed Extensions for C++!

See Also

Hello World in Visual C# | Hello World in Visual Basic | Writing Simple .NET Components | Clients for the Simple Components | Summary of Development Tutorial | Appendix A: Tools for Exploring Namespaces