C++/CLI Wrapper – how to set up a CLI file

What is CLI and what is it used for?

The Common Language Infrastructure (CLI) is a part of .NET programming. It is commonly referred to as the C++/CLI wrapper (this may be important if ever you’re Googling for help) and is used for managing code between different .NET based code. As mentioned previously (here and here) CLI is useful in creating a C# WPF GUI and linking it to a strong C++ core, and this is what I use it for primarily in my work. I have twice before worked with CLI interfaces between C# and C++ and today I’m working on my third for RTM V3, so I thought now would be a good time to give a brief (hopefully) post on how to set up a CLI file for my future reference, and maybe for the benefit of those reading this. The C++/CLI wrapper isn’t complicated to understand if you’re familiar with the C++ language. If anything isn’t clear enough please comment or contact me and I’ll try to explain better.

Setting up the .h file

There are only a few things you need to know about making your header file for your managed CLI interface. After that, everything is pretty much the same as C++.

The first is the namespaces. Depending on the work you intend to do with your interface you may need multiple namespaces, but for the basic C++/CLI wrapper, you need to use the System namespace. You also need to provide a namespace name for your code for the C# code to use. This helps make everything tidy. Similar to C#, you declare the namespace with your given name and using curly brackets {} to wrap your method declarations within.

The second is the class, rather than the class className{}; approach used in C++, you need to declare it in a similar fashion to the C# method declaration. That is, you declare your class as ‘public ref class className{};’. All of this is wrapped inside your namespace brackets.

The third is the data types you’ll be using to transfer data between the C# code and the C++ code. The primitive data types are fine and you can use them without problem (int, double, bool, etc.) but others need to be managed (such as strings, for example). Therefore you need to make sure that any method declarations consider this – the compiler will not give you any warnings if you have used C++ data types instead of managed data types until you come to use them and the error will relate to the calling function, not the CLI interface (meaning you’ll have to go hunting for the problem). The key thing is to remember what your file is being used for and, if it’s the same as what I build them for, then you have C# data coming into methods (through the parameters) and C# data being returned (or void for no return). Typically then, any inputs or outputs would be managed and should avoid using C++ data types, as it is likely to produce an error when you try to use them. The example below shows how to set up a quick C++/CLI header file.

Example .h C++/CLI file

#pragma once
using namespace System;

namespace MyNameSpace {
    public ref class MyClass {
    public:
        MyClass(); //Constructor
        ~MyClass(); //Destructor

        void LoadFile(String^ fileName); //Notice the '^' to denote a managed data type
        int CountTwo(int a, int b); //Notice a lack of '^' as int is a primitive type not needing to be managed
        String^ GetName(); //Notice the '^' for the return string - denoting it will be a managed string going out
    private:
    };
}

Setting up the .cpp file

The .cpp file is simpler than the .h file as it is now just giving definitions to the methods declared in the header file. However, the key thing to remember is that you need to declare your namespace again to wrap the .cpp code in. This is seen in the example below.

Example .cpp C++/CLI file

#include 
namespace MyNameSpace {
    MyClass::MyClass() {
        //Constructor
    }
    MyClass::~MyClass() {
        //Destructor
    }

    void MyClass::LoadFile(String^ fileName) {
        //Some operations
    }
    int MyClass::CountTwo(int a, int b) {
        return (a+b);
    }
    String^ MyClass::GetName() {
        //Some operations
    }
}

Conclusion

Hopefully the explanations and examples here are clear and you will be able to make use of them. You should now be able to create anything in C# and link it to C++ (or vice versa). Knowledge of CLI will of course make you a powerful programmer being able to create C# WPF GUIs and link them to superior C++ calculation engines without worrying about Java Layout Managers, or not making use of C++’s memory management by only using C#. If further explanation is wanted then feel free to comment, hopefully I will be posting other posts later on other CLI work to help you build on your knowledge if you so wish. If you have a particular topic you want covered, again, feel free to ask.

Leave a Reply

Your email address will not be published. Required fields are marked *