C++ classes in C# (Help)

Started by
3 comments, last by Arild Fines 18 years, 5 months ago
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++).
If you don`t do something, something will do, something to you!
Advertisement
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.
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.
- k2"Choose a job you love, and you'll never have to work a day in your life." — Confucius"Logic will get you from A to B. Imagination will get you everywhere." — Albert Einstein"Money is the most egalitarian force in society. It confers power on whoever holds it." — Roger Starr{General Programming Forum FAQ} | {Blog/Journal} | {[email=kkaitan at gmail dot com]e-mail me[/email]} | {excellent webhosting}
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#? ;)
You could look into using SWIG. It generates a proxy .NET class for your C++ class, from which you can inherit.
--AnkhSVN - A Visual Studio .NET Addin for the Subversion version control system.[Project site] [IRC channel] [Blog]

This topic is closed to new replies.

Advertisement