Visual C++ .NET is a powerful tool to build Windows applications for C or C++ programmers. It is useful to create dynamic web applications as well as XML based web services. The Visual C++ .NET includes MFC (Microsoft Foundation Classes), ATL (Active Template Library) and STL (Static Template Library). It will support both 32-bit programming and 64-bit programming. The C++ programmer can also invoke the .NET CLR (Common Language Runtime) support with /CLR switch at compile time, with MSIL (Microsoft Intermediate Language) features. Microsoft has provided two types of C++ Libraries. They are Managed C++ and Unmanaged C++. The C++ programmer can mix managed and unmanaged codes. The managed C++ is the simplest form of C++.
The user gets unique features, managed code garbage collection, threading, and access to all .NET framework classes, and unmanaged C++ features like ActiveX® Controls. All the features are available through the Visual studio .NET IDE (Integrated Development Environment). The Visual Studio .NET has improved debugging capability like managed debugging, unmanaged debugging, multilanguage debugging and remote debugging.
The managed code is the code targeted at the Common Language Runtime (CLR). C#, Visual Basic .NET and Jscript are managed by default. But, C++ is not managed even with /CLR switch compilation. We mention the C++ class as a <CODE lang=mc++>__gc at the beginning. The <CODE lang=mc++>__gc memory instance of the class is managed by the garbage collector.
Garbage collection is a mechanism that allows the system to detect when an object can no longer be accessed. It releases unused memory automatically. This mechanism is a very useful new feature in managed C++. It is possible to call native Win32 functions from the .NET framework. The <CODE lang=mc++>__gc class is the most general class in managed C++. The <CODE lang=mc++>__gc interface supports the COM style interface programming in C++. A <CODE lang=mc++>__gc class shall not inherit from more than one managed class.
Any file containing metadata like *.dll, *.obj and *.exe can be referenced by C++ source files. This features gives maximum speed in the compilation time. Because, the compiled code is faster then text files. The managed C++ also supports Windows® resource types like bitmap, brush, and region.
The <CODE lang=mc++>__gc class supports <CODE lang=mc++>struct or <CODE lang=mc++>class data type. The difference between <CODE lang=mc++>struct and <CODE lang=mc++>class is, the default visibility in <CODE lang=mc++>struct is <CODE lang=mc++>public and default visibility in <CODE lang=mc++>class is <CODE lang=mc++>private. The <CODE lang=mc++>__gc class can use any number of interfaces. The <CODE lang=mc++>__gc shall not use <CODE lang=mc++>new or <CODE lang=mc++>delete operators. This also supports visibility like <CODE lang=mc++>private, <CODE lang=mc++>public or <CODE lang=mc++>protected. We do not create any copy constructor. All the <CODE lang=mc++>__gc classes derive from
System.Object root class. When we call the destructor, the memory will be cleared automatically.
MFC and Managed C++:
The MFC gives the easiest way to create C++ based Windows® GUI applications compared to native Win32 API® programming. Now, Microsoft Foundation Classes has many features and many classes have been added (more then 200 classes) with version 7.0. If we create a SDI (Simple Document Interface) or MDI (Multiple Document Interface), we just click some class wizard and we will get new applications with document architecture feature. The .NET framework class library gives a new set of classes that can be accessed by all levels of programmers in a unique way. The programming language may be different. But, the native code is same in Microsoft Common Language. MFC is a class library to build document based applications. It has some features like serialization. Windows Forms is a windowing library for N-Tier architecture.
In Windows based application, we will create at least one form. In that form, we will have some number of controls included. The main form is the parent control. Windows Forms require at least one form. It may be a message box or the Windows Form.
The simplest form in Windows is the Windows message box .The following code example gives a first view of the programming world.
// Class derived from Forms
using namespace System::Windows::Forms;
In the above example, <CODE lang=mc++>#using is used to import the metadata from filename.obj, filename.dll or filename.exe. The message box is the parent form of this example and has many other forms. This program has the following output:.
If we create a simple form in managed C++, first we will create the instance of the
Form class. Then the application will run on the main program. The
main function is the entry point in a C++ program. Any C/C++ program should have at least one
OK. We will see the next example of using forms in .NET framework using managed C++. We already saw the fundamental concepts in garbage collection. Now, we implement a class derived from
using namespace System;
using namespace System::Windows::Forms;
__gc class Hello : public Form
m_p->Click += new EventHandler(this, button_click);
void button_click(Object* sender, EventArgs* e)
Hello is derived from
Form class. All the classes are derived from
System.Object root class. So,
Hello class contains the basic functionality of
Form class. In
Hello class constructor, we mention the form title, change the background color, and etc. We add child controls like
ListBox and other Windows controls.
Comparing MFC, the managed C++ using Windows Forms easily creates the controls. In our example,
Button class is declared as
m_p, then we add the
Click event. In the
Click event, the message box is displayed.
The output in the above example is:
If we click the button, the event occurs. So, the message box is displayed.
In the above two examples, we saw the basic functionality in managed C++. In our final example, we will implement some of the features. The form contains the
RadioButtons. In that text box, we will type any character, the right hand side
GroupBox contains the color changed in the foreground and background.
Snotepad is derived from
Form class and it supports
__gc also. If we click the background color as blue, then the
TextBox color will be changed. The foreground color is changed also when we click the foreground
<PRE lang=mc++>// __gc class garbage collected SNotepad derived from Class Form
__gc class SNotepad : public Form
// Visibility as public
// Inialize the controls
// Display the title in the parent form
Text="Hello Windows Controls ";
// Main From properties
Bounds = Rectangle(200,200,200, 200);
Visible = false;
ClientSize = System::Drawing::Size(600,400);
// Textbox properties
txtBox = new TextBox();
txtBox->Font = new System::Drawing::Font( new
FontFamily( S"Times New Roman"), 14.0f );
txtBox->Size = System::Drawing::Size(400, 600);
txtBox->TabIndex = 1;
txtBox->Multiline = true;
txtBox->ScrollBars = ScrollBars::Both;
// OK button properties
// OK Button event handler
color->Click += new EventHandler(this, okbutton_click);
// Cancel button properties
cancel->Click += new EventHandler(this, cancelbutton_click);
// Change to the default color button
def->Click += new EventHandler(this, defbutton_click);
// Clear the text box when user click this button
clear->Click += new EventHandler(this, clearbutton_click);
// Groupbox contains foreground color properties
grpColor->Location = Point(410, 30);
grpColor->TabStop = false;
grpColor->Text = S"Foreground Color : ";
grpColor->Size = System::Drawing::Size(150,120);
// Add to the main form
// Group box contains background color properties
grp->Location = Point(410,170);
grp->TabStop = false;
grp->Text = S"Background Color : ";
grp->Size = System::Drawing::Size(150,120);
// Foreground color ratio button
// Caption of the ratiobutton
// Handler to the button
radColor1->Click +=new EventHandler(this,radColor1_Click);
radColor2->Click +=new EventHandler(this,radColor2_Click);
radColor3->Click +=new EventHandler(this,radColor3_Click);
// Add to the groupbox
// Create ratio button to change background color
// Specify the location
// Initiaize text in radio button
// Add event handler
radColor4->Click +=new EventHandler(this,radColor4_Click);
radColor5->Click +=new EventHandler(this,radColor5_Click);
radColor6->Click +=new EventHandler(this,radColor6_Click);
// Add to the group box
//Change forcolor to red
void radColor1_Click(Object *sender,EventArgs* e)
//Change forcolor to Blue
void radColor2_Click(Object *sender,EventArgs* e)
//Change forcolor to green
void radColor3_Click(Object *sender,EventArgs* e)
// Change backcolor to Red
void radColor4_Click(Object *sender,EventArgs* e)
// Change backcolor to Blue
void radColor5_Click(Object *sender,EventArgs* e)
// Change backcolor to green
void radColor6_Click(Object *sender,EventArgs* e)
// Initialize the color properties,
// background as white and forground as black
void defbutton_click(Object *sender,EventArgs* e)
// Clear the TextBox
void clearbutton_click(Object *sender,EventArgs* e)
// When the user click ok button, the messagebox
// displayed and Close the form
void okbutton_click(Object* sender, EventArgs* e)
MessageBox::Show("Now You Close this application!");
// When user click cancel button the application terminited
void cancelbutton_click(Object* sender, EventArgs* e)
// Free the memory
// Declare the Control Variables
TextBox * txtBox;
The output of this example is:
If we click the Clear button, the text box clears the messages. If we click OK button, it will display the message box with OK button. If click OK, the application is terminated. If we click Cancel button, it terminates directly.
From the above examples, a beginner programmer will be benefited. Because the examples are easy to understand the basic concepts of managed C++. These examples are use .NET framework from command line. I did not use the .NET IDE (Integrated Development Environment). It will work fine in command line with /CLR compiler option. Working in C++ can let you write faster code than working in C# or VB.NET. If you are comfortable working in C++, there are real benefits for you. The .NET Framework class library provides a vast, rich and uniform class library. The MFC and Managed C++ are designed for different purposes. The .NET framework classes have automatic security and uniform manner.