Reputation: 11
I am new to C++/CLI and I would like to know how I can wrap unmanaged C++ class with virtual methods to managed and use that from C#?
Unmanaged C++ class:
class IProgression
{
public:
virtual ~IProgression(void) {}
virtual void sendProgression(int amount, int present) = 0;
};
sendProgression method is used to send progress information to C# layer. So it's callback.
I have other unmanaged C++ class where this progression callback is set:
virtual void setProgression(IProgression * pIProgression) = 0;
So I need to set callback interface from C# to unmanaged C++ via C++/CLI and get progression information back to C# from unmanaged C++ via C++/CLI.
Can someone give advices how to implement this since I'm kind of confused with this?
Upvotes: 1
Views: 1764
Reputation: 51
You could do something like:
public interface IManagedProgression {
void SendProgression(int amount, int present);
};
public class Wrapper : public IProgression {
public:
Wrapper(IManagedProgression^ c)
{
callBack = c;
}
void sendProgression(int amount, int present)
{
callBack->SendProgression(amount, present);
}
private:
gcroot<IManagedProgression^> callBack;
};
You can then implement the IManagedProgression in C# or another language and wrap it in a Wrapper object.
Upvotes: 2