locked
how to update UI element from DLL callback

    Question

  • How to write a callback function in windows store app,  which can be callback from a WinRT DLL?

    code snippet in DLL :

    btDLL.h:

    typedef void(CALLBACK  *RECV_DATA_CALLBACK)(Platform::String^ str);

    class BTWinRTDLL_API CBTWinRTDLL
    {

       void CBTWinRTDLL::register_recv_callback(RECV_DATA_CALLBACK method);

    }

    btDLL.cpp:

    RECV_DATA_CALLBACK  pRecvCallBack;

    void CBTWinRTDLL::register_recv_callback(RECV_DATA_CALLBACK method)

    {
         pRecvCallBack = method;
    }

     void CBTWinRTDLL::ReceiveStringLoop(DataReader^ reader, StreamSocket^ socket)  
    {
       ...
        (*pRecvCallBack)(L"5002");     //fire call back
       ...
    }

    But in the windows store app side,  how do i write the callback function to update UI element in the MainPage.xaml ?

    MainPage.xaml.cs:

    void callback_recv(String^ Str)
    {

        // update UI element using Str
        ???

    }

    void BTApp_CPP::MainPage::Btn_ConnectDevice_Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)

    {

        ....

        btDLL.register_recv_callback(&callback_recv);

       ...

    }


    nio

    Tuesday, January 7, 2014 6:02 AM

Answers

  • If you use standard C++ DLLs you can use normal C++ techniques. If you're creating a Windows Runtime Component then you'll need to work with structures that can pass over the ABI. Take a look at Raising Events in Windows Runtime Components (and at Creating Windows Runtime Components in C++ ).

    • Marked as answer by nio Pan Wednesday, January 8, 2014 5:09 AM
    Wednesday, January 8, 2014 12:46 AM
    Owner
  • thanks Rob,

    Already solved .

    code in MainPage.xaml.cpp:

    MainPage^ m_rootPage;

    MainPage::MainPage()

    {
    InitializeComponent();
    m_rootPage = this;
    }

    void NotifyUserFromAsyncThread(String^ message)
    {
    m_rootPage->Dispatcher->RunAsync(CoreDispatcherPriority::Normal, ref new DispatchedHandler([message]()
    {
    m_rootPage->NotifyUser(message);
    }));
    }


    void __stdcall callback_recv(String^ Str)
    {
    NotifyUserFromAsyncThread(Str);
    }



    void MainPage::NotifyUser(String^ strMessage)
    {
    txtBlock_recv->Text = strMessage;
    }

    void BTApp_CPP::MainPage::Btn_ConnectDevice_Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)

    {

        ....

        btDLL.register_recv_callback(callback_recv);

       ...

    }



    nio

    • Marked as answer by nio Pan Wednesday, January 8, 2014 5:09 AM
    Wednesday, January 8, 2014 5:09 AM

All replies

  • If you use standard C++ DLLs you can use normal C++ techniques. If you're creating a Windows Runtime Component then you'll need to work with structures that can pass over the ABI. Take a look at Raising Events in Windows Runtime Components (and at Creating Windows Runtime Components in C++ ).

    • Marked as answer by nio Pan Wednesday, January 8, 2014 5:09 AM
    Wednesday, January 8, 2014 12:46 AM
    Owner
  • thanks Rob,

    Already solved .

    code in MainPage.xaml.cpp:

    MainPage^ m_rootPage;

    MainPage::MainPage()

    {
    InitializeComponent();
    m_rootPage = this;
    }

    void NotifyUserFromAsyncThread(String^ message)
    {
    m_rootPage->Dispatcher->RunAsync(CoreDispatcherPriority::Normal, ref new DispatchedHandler([message]()
    {
    m_rootPage->NotifyUser(message);
    }));
    }


    void __stdcall callback_recv(String^ Str)
    {
    NotifyUserFromAsyncThread(Str);
    }



    void MainPage::NotifyUser(String^ strMessage)
    {
    txtBlock_recv->Text = strMessage;
    }

    void BTApp_CPP::MainPage::Btn_ConnectDevice_Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)

    {

        ....

        btDLL.register_recv_callback(callback_recv);

       ...

    }



    nio

    • Marked as answer by nio Pan Wednesday, January 8, 2014 5:09 AM
    Wednesday, January 8, 2014 5:09 AM