Skip to main content
gamedev.net
🔒 Locked

Accessing C++ native code from C#

Started by BigBadBob 2 replies 1.4k views
Original Post
BigBadBob
BigBadBob
Hello. I have a large library in native C++ that I wish to use from a new C# windows application I am writing. I am trying to ascertain my options. It would seem that if the native code is a DLL then I can use System.Runtime.InteropServices and the DllImport attribute class to create a function declaration that I can use directly in C# for my C++ functions. But what if I don't want to convert my library to a DLL and leave it as is. Is this possible? Secondly, if I want to access types in this native static library, am I right in thinking I can only do this via a managed C++ wrapper object? I have no desire to actually access the internals of the types exposed in the native lib only to hold pointers / references to an instance and then pass it to various functions in the lib from the C# app. Thirdly, would it be better to use managed C++ as a proxy in this example? And just redefine managed versions of my types ( wrap them ) in managed C++ so that C# could just get them from the managed C++ assembly, and then also redefine managed functions that simply call the native ones that I want? In this case...am I right in thinking that the native lib could stay as a static lib, the managed assembly would be a "dll" ( in .NET terms an assembly ) that then statically links to the native lib. Thanks for help and advice.
d_emmanuel
d_emmanuel
To expose a native C++ library to C#, there are two options:

(1) Explicit PInvoke (System.Runtime.InteropServices). However, you'll need to compile your lib as a DLL to use this and it only lets you call functions that are declared with C linkage. For example,

extern "C"{  MY_API void myFunc();}


(2) Wrap your native C++ classes and functions in managed C++ classes.
In this case, as you correctly said, the native library can remain as a static lib and the managed wrapper classes will be contained in their own DLL.

Unless, your library consists of a reasonably small number of C style functions, I would recommend going with option 2.

Try searching MSDN to help find out which approach best suites your needs.
BigBadBob
BigBadBob
Hi and thanks for the reply. I would also like to know the debugging implications of mixed mode debugging in cases presented if possible as I don't seem to find much on this subject. Now lets suppose that I had wrapped the parts of my native lib in managed C++ code and then used this from C#, how would this affect debugging under visual studio .NET ? Are there any problems / pitfalls to look out for? Is the "step-in" transition from managed to unmanaged problematic, are there any visual studio settings that can make this better? Thanks very much, going to be playing around all day with this but any info from those who have had experience is well appreciated.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.