Importing a C# class library into Visual C++
Question
I have a C# class library that contains methods that need to be used with an external application. Unfortunately this external application only supports external APIs in C/C++.
Suppose I have a takeIntReturnDoubleArray method in this C# library that takes an integer and returns an array of doubles. All I need to do is have a C++ method that takes an integer, calls the C# library and returns an array of doubles to the calling application.
So in essence the C++ library is just acting as an intermediary between the C# wrapper and the external application.
Is there an easy way to do this? Do I have to do anything special on the C# side to allow it to be imported into C++ easily? I have seen some talk of using the #import statement but I really have no idea what I am doing when it comes to C++.
What is the approach I should be taking here?
Solution
You have two main options here:
- C++\CLI - this allows you to have both managed and unmanaged code in the same source file. The managed portion can then call the C# code.
- COM Interop - expose your .NET type as a COM interface and matching coclass which you can easily use from unmanaged C++.
OTHER TIPS
COM Interop is one way to approach this problem. You'll have to have a COM layer for the C# library function(s) you want to expose to the C++ application. This (http://msdn.microsoft.com/en-us/library/aa302324.aspx#usingframeworktools_topic10) might be of interest.