Sign in to follow this  
visla

C++ classes in C# (Help)

Recommended Posts

Ok I want someone to help me by writing small peace of code. I have this problem. I made a class in C++. And this class is stored in .dll compiled. now I wish to inherit this class in C#, but have no idea how to do so. And also how am i supposed to call callbacks in this situation from c++. Main engine is written in C++ fully, and I want to use C# to make editors (it is much faster than in C++).

Share this post


Link to post
Share on other sites
You can't just derive a class in C# from a C++ class. That is, unless you wrote it in managed C++. If that's what you've done, simply reference the assembly and derive from the class as usual.

Share this post


Link to post
Share on other sites
Quote:
Original post by visla
Ok I want someone to help me by writing small peace of code.
I have this problem. I made a class in C++. And this class is stored in .dll compiled. now I wish to inherit this class in C#, but have no idea how to do so.

And also how am i supposed to call callbacks in this situation from c++.

Main engine is written in C++ fully, and I want to use C# to make editors (it is much faster than in C++).

Importing all that unmanaged code into a managed application space is going to seriously impact the benefits you get from running managed code (namely, garbage collection and the penalty for extra levels of method indirection). If you're still determined to do it this way, see this article and this one for how to get started.

Share this post


Link to post
Share on other sites
Sounds like a job for COM ;)

To my knowledge, the only way you're going to be able to write a class in C# and have it inherit from a C++ class is to write the C++ class as a COM component, and unless you have read about 500 MSDN articles on COM, its not going to be easy. But ask yourself, does it make sense to hava a C# inherit from a C++ class? What I would do, short of going the COM route, is look at the articles kSquared posted, start learning how to call functions in DLL's, and write a C# wrapper class for the exported functions you need.

That still might get you only have of the way though, and you're most likely going to have to rely on unsafe code in C# to help you through, so again, is it really faster to write it in C#? ;)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this