Invoke .NET Assembly from Native C++ Code

Writer: Jialiang Ge
 

Solution 1. Use a C++/CLI wrapper
Solution 2. Host CLR
Solution 3. Convert .NET assembly to a COM server, and call it from C++ through .NET-COM interop

Solution 1. Use a C++/CLI wrapper

Samples:
  • CSClassLibrary (a C# class library)
  • CppCLINETAssemblyWrapper (a C++/CLI wrapper of the C# class library CSClassLibrary)
  • CppCallNETAssemblyWrapper (a native C++ application that invokes CSClassLibrary through CppCLINETAssemblyWrapper)
The native C++ sample application CppCallNETAssemblyWrapper calls the .NET class defined in the C# class library CSClassLibrary through the C++/CLI wrapper CppCLINETAssemblyWrapper.

Download Download the All-In-One Code Framework (Library) package.

CLIWrapper.png

Solution 2. Host CLR

Samples:
  • CSClassLibrary (a C# class library)
  • CppHostCLR (a native C++ application that host CLR and load the .NET assembly CSClassLibrary)
The native C++ sample application CppHostCLR hosts CLR, instantiates a type in the .NET assembly CSClassLibrary.dll and calls its methods. Both .NET Framework 1.x Hosting Interfaces and .NET Framework 2.0 Hosting Interfaces are demonstrated in the code samples.

Download Download the All-In-One Code Framework (Library) package.

HostCLR.png

Solution 3. Convert .NET assembly to a COM server, and call it from C++ through .NET-COM interop

Samples:
  • CSDllCOMServer (a C# class library converted to an in-process COM server)
  • CppCOMClient (a native C++ application that invokes the C# in-process COM server CSDllCOMServer)
The native C++ sample application CppCOMClient invokes the in-process COM server CSDllCOMServer that was converted from a C# class library.

Download Download the All-In-One Code Framework (COM) package.

COMInterop.png

Last edited May 2, 2010 at 1:30 PM by Jialiang, version 33

Comments

MilesAhead2 Mar 3, 2011 at 3:01 AM 
This was a great help - just exactly the information I needed, presented very clearly. Thanks,