how do you write a wrapper in C#?

Started by
4 comments, last by emeyex 16 years, 3 months ago
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
Deep Blue Wave - Brian's Dev Blog.
Advertisement
C++/CLI tends to be optimal for this.

In time the project grows, the ignorance of its devs it shows, with many a convoluted function, it plunges into deep compunction, the price of failure is high, Washu's mirth is nigh.

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.
*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!
Deep Blue Wave - Brian's Dev Blog.
I wrote a bit about this.

In time the project grows, the ignorance of its devs it shows, with many a convoluted function, it plunges into deep compunction, the price of failure is high, Washu's mirth is nigh.

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.

This topic is closed to new replies.

Advertisement