locked
How to callback C++ from C++/CX?

    General discussion

  • Short version: In a public ref sealed class, how do I callback to a regular C++ class when an event occurs?

    Long version: I have a large body of C++ code that I am trying to integrate with Windows 8 Store. In the existing code, I use a pure virtual interface for when a network message arrives

    class RNS2EventHandler {
    virtual void OnRNS2Recv(RNS2RecvStruct *recvStruct)=0;
    };
    class RakPeer : public RakPeerInterface, public RNS2EventHandler {
    virtual void OnRNS2Recv(RNS2RecvStruct *recvStruct) {DoStuff();}
    };

    Now I want to call OnRNS2Recv when a network message arrives in Windows Store 8. This code is basically out of the DatagramSocket sample

    listenerContext = ref new ListenerContext(listener);
    listener->MessageReceived += ref new TypedEventHandler<DatagramSocket^, DatagramSocketMessageReceivedEventArgs^>(listenerContext, &ListenerContext::OnMessage);

    public ref class ListenerContext sealed
    {
    public:
    void OnMessage(Windows::Networking::Sockets::DatagramSocket^ socket, Windows::Networking::Sockets::DatagramSocketMessageReceivedEventArgs^ eventArguments) {
    // Need to call the C++ callback. How???
    }
    }

    If I try putting a RNS2EventHandler* inside ListenerContext the compiler complains that I can't put C++ types inside a C++/CX class.

    So how do I implement a callback to notify C++ when ListenerContext::OnMessage occurs? As a last resort I can poll the ListenerContext class in a thread from the C++ code, but that ruins performance and the whole point of a callback.

    Thanks in advance.

    • Changed type Jesse Jiang Wednesday, January 16, 2013 2:05 AM
    Wednesday, January 9, 2013 5:05 AM

All replies

  • Hi,

    Did you try to write a C++ component with the native codes?

    As far as I know, we can set the native codes as private member, the complier error should disappear.
     
    Best regards,
    Jesse


    Jesse Jiang
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Thursday, January 10, 2013 6:47 AM
  • Hi,

    Would you mind letting me know the result of the suggestions? If you need further assistance, feel free to let me know. I will be more than happy to be of assistance.

    Best regards,
    Jesse


    Jesse Jiang
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Monday, January 14, 2013 2:44 AM
  • Hello,

    I would like to change this thread type as discussion, because I think there may be no exact answer for this question.

    Thanks for your understanding,

    Best regards,
    Jesse


    Jesse Jiang
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Wednesday, January 16, 2013 2:05 AM
  • you can declare "event" in class ListenerContext,

    and in OnMessage implementation, you should fire that event.

    following is some code snippet

    public delegate void SocketEvent(....some parameter if you need);

    public ref class ListenerContext sealed
    {

    public:

     event SocketEvent^ socketmsg;

    ....other code..

    }

    .....

    in your some class that managed ListenerContext ...

    auto myListenerContext = ref new ListenerContext();

    myListenerContext->socketmsg += ref new SocketEvent(this,&....your callback function);

    and now when  OnMessage be called,your callback function will be called,too

    Sunday, January 20, 2013 6:58 PM