C++/CLI programming

With .NET framework Microsoft created Common Language Infrastructure (CLI) which lays down specifications for a language to become compatible with .NET framework; to be executed under Common Language Runtime (CLR). CLR is a virtual machine which provides an software environment for programs written using CLI specification to execute. It basically provides a managed environment where memory allocation-release, optimization and interoperability between different .NET langauges, all these are taken care by the environment.

So, here we are going to describe C++/CLI programming technique, a .NET extension of native C++ programming with the help of which we create programs executing within CLR. In other words, with the help of this technique a program can access all .NET managed components in C++. And additionally, as C++/CLI programs written execute within CLR, managed code written in C# can also access C++/CLI classes and invoke its APIs.

This technique first appeared with Visual Studio 2005 as a substitute for the Managed C++ programming which became deprecated. It is usually used as a bridge between C# and C++ in the large production development setup where an application is created using both managed and unmanaged components.

Syntax for C++/CLI classes

A class written in C++/CLI is decalared with ‘ref‘ keyword.

public ref class MyClass

ref keyword here indicates that this class is of reference type and thus can accessed from .NET runtime.

To access above class in C++ Caret symbol (^) is added as a suffix to a class name, symbolizing that this class is a .NET environment class not a native C++ class.

For e.g., following snippet shows way to access .NET string class in C++/CLI class.

String^ myString;

For creating instance of managed types, gcnew keyword is used, ‘gc’ of course symbolizes ‘garbage collected’.

String^ myString = gcnew String();

Note that caret is not used on right side of assignment operator.

Next, C++ scope resolution operator (::) is used instead of dot(.) for accessing public properties/methods of a .NET class and in importing managed namespaces.

Console::ReadLine();

using namespace System::Diagnostics;

Now, that you are familiarized with differences in C++/CLI from native C++ syntax, lets brush up some C++ concepts before starting to build a C++/CLI program.

Object creation in C++

In C++, if you have a class as MyNativeClass and we declare its object like

MyNativeClass myNativeClass;

this myNativeClass is not a reference to the object (which is the usual case in C#), it is a value type object. C++ objects are referred to as static variables which means they are created in Stack unless they are created with pointer reference.

Second, when you have myNativeClass1 and myNativeClass2 and you assign one to another using assignment operator (=), you are not referencing one object to another, you are actually doing a MEMBER-WISE ASSIGNMENT.

So, after this you can appreciate caret(^) and gcnew usage above to create reference type objects in C++ context.

Now, that we have explored all aspects of C++/CLI programming, in our next post we will demonstrate its example by creating a library which can be accessed in both native C++ and C#.

Published by Code Pantheon

I am a learner and a believer.

Join the Conversation

  1. Unknown's avatar

1 Comment

Leave a comment

Design a site like this with WordPress.com
Get started