Aravind's Tutorials

Home
Bio
Publications
Astrophotography
Photography
Tutorials
Reviews

AK Imagery
Photo Blog


Writing and using managed C++ .NET assemblies


In this tutorial I will quickly go over how to created a .NET assembly as a library from a C++ project. I will then call my C++ assembly from a C# application. I am going to assume you have Visual Studio .NET installed.

An assembly is basically a package of code along with a manifest that describes how to use the code. It is meant to be language neutral. There are two types of assemblies, shared and private. By default a private assembly is created. When using a private assembly you will need to explicitly import the assembly (described later). Shared assemblies are global to the system and are stored in a protected folder called the Global Assembly Cache (GAC). In order to create a shared assembly, the assembly must be given a strong name in the form of a 128-bit key.

This tutorial will deal only with private assemblies.

For more information on assemblies see:

First let us create a managed C++ DLL. I will describe creating a very simple project.
  1. Open Visual Studio .NET, Create a new project. Under "Visual C++ Projects", select "Managed C++ Class Library"
  2. Create a managed class, create methods (shown below)
#include "stdafx.h"
#include "stdlib.h"

using namespace System;

namespace ManagedCPPTest {
	__gc public class VCTest
	{
	public:
		int RandomValue()
		{
			return rand();
		}
	};
};
Notes:
  • The "__gc" keyword tells the compiler that the class is a managed class, and thus can be exported.
  • "const" and "volatile" function types are not support for managed classes
  • Managed classes can have static member functions just like nonmanaged classes
  • Managed classes can call unmanaged classes (see below), however all classes in a hierarchy must be strictly managed or unmanaged, there can be no mixing
  • All memory allocations and pointers are handled by the caller. So in the case of C# the garbage collection will automatically clean up when appropriate

Now create the C# project to call your managed C++ assembly. When you compiled your managed C++ library, the compiler automatically generates an private assembly.
  1. Add a new project to your solution. Under "Visual C# Projects", select "Windows Application"
  2. Go to Project -> Add Reference
  3. Click on Browse...
  4. Navigate to the folder you built your managed component to (usually under Debug/ or Release/)
  5. Select your DLL and click on Open
  6. Click on OK
  7. Go to the form designer
  8. Select ListBox, drag out a ListBox on your form
  9. Double-click on a blank area in your form to edit the "Load" code for the form
  10. The code in the "Form1_Load" function is shown below
private void Form1_Load(object sender, System.EventArgs e)
{
	ManagedCPPTest.VCTest myVCTest = new ManagedCPPTest.VCTest();
	listBox1.Items.Add("Numbers from VC component");
	for (int index = 0; index < 5; index++) {
		listBox1.Items.Add(myVCTest.RandomValue());
	}
}
When you added a reference to your project, the assembly is automatically loaded, and all managed classes that can be used become available. From that point on, all managed classes transparently become available.

This is a very introductory and elementary tutorial. For more details on using managed C++ libraries, see the other tutorials.
Other web resources and references

Return to tutorial index


Hosted by theorem.ca

All text and images (c) 2000-2012 Aravind Krishnaswamy. All rights reserved.