• Advertisement
Sign in to follow this  

how do you write a wrapper in C#?

This topic is 3674 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm working on a project with some groupmates, and we are using an already-established open-source C++ project for our backend... how do we get this to interface with our program if we are writing in C#? Do we need to make a C# wrapper for it? If so... how?? Thanks -Brian

Share this post


Link to post
Share on other sites
Advertisement
Generally speaking, the "easiest" way (though still somewhat of a pain) is to write a managed C++ CLR dll and include that as an assembly reference in your C# app. You won't be able to access any of your unmanaged types in C#, but you'll be able to use any managed C++ class automatically (as though it were a C# class). Alternatively, if you only need to export a "few" functions, you can write a normal C++ dll and explicitly import the C/C++ functions. Note that with the latter option, you're pretty much stuck with just C/C++ functions (i.e., no user-defined types). I'd recommend going with the C++ CLR dll for long-term. You can create one through Visual Studio 2005 -> New Project -> Visual C++ -> CLR -> Class Library I believe.
Once you get that going, you'll be writing manged C++ classes in your dll; those managed types can only have pointers to unmanaged code, but you can access all your unmanaged C++ code in the implementation of those managed classes.

Share this post


Link to post
Share on other sites
*scratches head*

Would a google search for C++/CLR explain how to make a wrapper in this fashion? (i.e. is it self explanatory, once I learn what CLR is?)


Edit: never mind - that second explanation got me on track. Thanks to both of you!

Share this post


Link to post
Share on other sites
C++/CLI is basically just the newer version of managed extensions to C++. You go about it the same as how I explained, just make sure you use their newer CLI syntax.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement