I have a large application written in native C++. I also have a class in C# that I need to call.
If the C# class was static, then it would be trivial (there’s lots of examples on the web) – just write the mixed C++/CLI wrapper, export the interfaces, and you’re done.
However, the C# class is non-static, and can’t be changed to static as it has an interface (the compiler will generate an error if you attempt to make the C# class static).
Has anyone run into this problem before – how do I export a non-static C# class to native C++?
Update 2010-11-09
Final solution: tried COM, this worked nicely but didn’t support structures. So, went with a C++/CLI wrapper, because I absolutely needed to be able to pass structures between C++ and C#. I wrote a mixed mode .dll wrapper based on the code here:
As the target class was non-static, I had to use the singleton pattern to make sure I was only instantiating one copy of the target class. This ensured everything was fast enough to meet the specs.
Contact me if you want me to post a demo project (although, to be fair, I’m calling C# from C++, and these days most people want to call C++ from C#).
C++/CLI or COM interop work just as well with non-static classes as with static. Using C++/CLI you just reference your assembly that holds the non-static class and then you can use
gcnewto obtain a reference to a new instance.What makes you think that this is not possible with your non-static class?
EDIT: there is example code here.