How to call a C# library from Native C++ (using C++\CLI and IJW)

You can do this fairly easily.

  1. Create an .h/.cpp combo
  2. Enable /clr on the newly create .cpp file. (CPP -> Right click -> Properties)
  3. Set the search path for “additional #using directories” to point towards your C# dll.

Native.h

void NativeWrapMethod();

Native.cpp

#using <mscorlib.dll>
#using <MyNet.dll>

using namespace MyNetNameSpace;

void NativeWrapMethod()
{
    MyNetNameSpace::MyManagedClass::Method(); // static method
}

That’s the basics of using a C# lib from C++\CLI with native code. (Just reference Native.h where needed, and call the function.)

Using C# code with managed C++\CLI code is roughly the same.

There is a lot of misinformation on this subject, so, hopefully this saves someone a lot of hassle. 🙂


I’ve done this in: VS2010 – VS2012 (It probably works in VS2008 too.)

Leave a Comment