locked
[W8.1] How to pass std::function to C++/CX RRS feed

  • Question

  • Is there any way to pass std::function to c++/cx as delegate?

    void doSomething(std::function<void(std::string)> callback)

    What I need is to call winRT C++/CX from C++ (to buy an IAP), then I need to tell C++ code about the outcome somehow.

    Any ideas? Thank you

    Monday, August 3, 2015 3:58 PM

Answers

  • Thank you for your answer!

    EDIT:

    I managed to get a solution - I just create C++/CX delegate inside (!) my C++ code and then pass it to winRT C++/CX. after processing winRT calls delegate and I can update In App Purchases in the game - seems to work.

    ==============

    I am still a bit lost - I am trying to call WinRT Component written in C++/CX from my main code in C++, passing as parameter C++ function to be called from C++/CX later (once it finishes user interaction while doing In App Purchases).

    I would prefer not to use WRL directly from C++ as it seems more complicated - right now I am accessing store related stuff from WinRT Runtime Component, which is relatively easy.

    Another solution would be to call some C++ function from C++/CX after it finishes processing - I am not sure how to do it either as I am rather new to Microsoft world ;)

    Best regards


    Tuesday, August 4, 2015 11:47 AM

All replies

  • C++/CX delegates have to be themselves methods of WinRT classes.

    You could also make use of WRL directly in C++ taking advantage of the fact that you can reinterpret_cast from a ^ to a pointer to the ABI COM type.

    Tuesday, August 4, 2015 4:06 AM
  • Supplement Chuck's reply

    If you are using C++/CX compiler, you can just use a WinRT class to wrap the std::function<T>.

    For example:

    MainPage::MainPage()
    {
    	InitializeComponent();
    
    	std::function<void()> dispatchHandle = []() { 
    		OutputDebugString(L"Run the function");
    	};
    
    	Dispatcher->RunAsync(Windows::UI::Core::CoreDispatcherPriority::Normal, ref new Windows::UI::Core::DispatchedHandler(dispatchHandle));
    }

    If you are using pure C++ compiler, you may need to wrap it by using WRL as Chunk mentioned.

    How to: Handle Events Using WRL


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.



    Tuesday, August 4, 2015 7:23 AM
  • Thank you for your answer!

    EDIT:

    I managed to get a solution - I just create C++/CX delegate inside (!) my C++ code and then pass it to winRT C++/CX. after processing winRT calls delegate and I can update In App Purchases in the game - seems to work.

    ==============

    I am still a bit lost - I am trying to call WinRT Component written in C++/CX from my main code in C++, passing as parameter C++ function to be called from C++/CX later (once it finishes user interaction while doing In App Purchases).

    I would prefer not to use WRL directly from C++ as it seems more complicated - right now I am accessing store related stuff from WinRT Runtime Component, which is relatively easy.

    Another solution would be to call some C++ function from C++/CX after it finishes processing - I am not sure how to do it either as I am rather new to Microsoft world ;)

    Best regards


    Tuesday, August 4, 2015 11:47 AM