What is C++/CLI ?

Warning! Some information on this page is older than 5 years now. I keep it for reference, but it probably doesn't reflect my current knowledge and beliefs.

# What is C++/CLI ?

20:24
Fri
25
Sep 2009

Today I've started learning C++/CLI. It's a Microsoft technology that allows to write managed code for .NET platform in C++. But I can see it's not just a Microsoft idea to introduce custom language extensions. It's a massive piece of technology and even some C++ gurus were involved in its development, like Herb Sutter and Stanley Lippman. It's also approved as ECMA-372 standard.

So what exactly is C++/CLI? It's yet another language for .NET, just like C# or VB.NET, but it has the unique feature of being compatible with native C++ and being able to freely mix managed and unmanaged code.

For many people it may trigger some bad connotations with ugly syntax like __gc, __interface, __property etc., but that's not true. Syntax like this existed in Managed Extensions for C++, but C++/CLI is its new, improved version introduced in Visual Studio 2005.

Here is a screenshot from my first experiment, where I've created a property grid control from .NET next to a native Direct3D9 device initialized inside a panel control.

Just look at how old good native code can be mixed with managed one:

#include <d3dx9.h>
namespace ManagedCpp02 {
  using namespace System::Windows::Forms;

...

public:
  Form1()
  {
    PropertyGrid^ propertyGrid1 = gcnew PropertyGrid();
    ...
    IDirect3D9 *d3d = Direct3DCreate9(D3D_SDK_VERSION);
    System::Diagnostics::Debug::Assert(d3d != nullptr);

Microsoft wasn't afraid to introduce new keywords and operators to the language. These designed to operate on managed code work next to the native ones, so you can define and use native and managed classes next to each other just like this:

class NativeClass { };

// Allocate from unmanaged heap - memory must be freed.
NativeClass *nPtr = new NativeClass();
delete nPtr;

ref class ManagedClass { };

// Allocate from managed heap - garbage collected.
ManagedClass ^mPtr = gcnew ManagedClass();

Comments | #c++ #.net #visual studio Share

Comments

STAT NO AD
[Stat] [STAT NO AD] [Download] [Dropbox] [pub] [Mirror]
Copyright © 2004-2017