C++ Interop: How do I call a C# class from native C++, with the twist the class is non-static? -
i have large application written in native c++. have class in c# need call.
if c# class static, trivial (there's lots of examples on web) - write mixed c++/cli wrapper, export interfaces, , you're done.
however, c# class non-static, , can't changed static has interface (the compiler generate error if attempt make c# class static).
has run problem before - how export non-static c# class native c++?
update 2010-11-09
final solution: tried com, worked nicely didn't support structures. so, went c++/cli wrapper, because absolutely needed able pass structures between c++ , c#. wrote mixed mode .dll wrapper based on code here:
as target class non-static, had use singleton pattern make sure instantiating 1 copy of target class. ensured fast enough meet specs.
contact me if want me post demo project (although, fair, i'm calling c# c++, , these days people want call c++ c#).
c++/cli or com interop work non-static classes static. using c++/cli reference assembly holds non-static class , can use gcnew
obtain reference new instance.
what makes think not possible non-static class?
edit: there example code here.
using namespace system; public ref class csquare { private: double sd; public: csquare() : sd(0.00) {} csquare(double side) : sd(side) { } ~csquare() { } property double side { double get() { return sd; } void set(double s) { if( s <= 0 ) sd = 0.00; else sd = s; } } property double perimeter { double get() { return sd * 4; } } property double area { double get() { return sd * sd; } } }; array<csquare ^> ^ createsquares() { array<csquare ^> ^ sqrs = gcnew array<csquare ^>(5); sqrs[0] = gcnew csquare; sqrs[0]->side = 5.62; sqrs[1] = gcnew csquare; sqrs[1]->side = 770.448; sqrs[2] = gcnew csquare; sqrs[2]->side = 2442.08; sqrs[3] = gcnew csquare; sqrs[3]->side = 82.304; sqrs[4] = gcnew csquare; sqrs[4]->side = 640.1115; return sqrs; }
Comments
Post a Comment