locked
Calling a C++ static lib method that use a function pointer parameter within a C++ Metro app?

    Question

  • Hello, I have a C++ Metro application that uses a C++ static lib for most of its processing (so it can be re-used elsewhere). So far so good, everything is working properly except one part I wasn't able to figure out.

    Here's the scenario:

    The static lib provides a function for setting a class function pointer member, that is called on various occasions. It is defined as such:

    typedef void (*Callback)(void);
    void Foo::SetCallback(Callback pCallback)
    {
        mCallback = pCallback;
    }

    Now in the Metro application, I try to call this method from a ref class:

    ref class MetroApp sealed : public SomeClass { public:

    MetroApp(); void SomeMethod(); private: Foo mFoo; } MetroApp::MetroApp() { mFoo.SetCallback(&MetroApp::SomeMethod); }

    And I am getting this compilation error:

    error C2664: 'Foo::SetCallback' : cannot convert parameter 1 from 'void (__cdecl MetroApp::* )(void)' to 'Foo::Callback'
              There is no context in which this conversion is possible

    I don't see why this couldn't work. The function pointer signature does not return anything, nor does it take any parameter. I tried a lot of different way to try to force it with type casting, and I can't find how I can make this work properly. Anyone any idea if that is possible at all? Thank you.

    Thursday, December 13, 2012 6:03 PM

Answers

  • Hi Robert,

    The issue here is that you create a callback for a non-static member function of a C++ class. Even though the signature of MetroApp::SomeMethod looks like void MetroApp::SomeMethod(void), the C++ compiler will actually make it void MetroApp::SomeMethod(MetroApp* this) because it must pass object state to it.

    You have three options:

    1. Declare SomeMethod as static.
    2. Declare SomeMethod outside of the class, such that it is no longer a member.
    3. Use wrappers to implement the callback. One example can be found here. Additionally, the STL may have some useful constructs for this.


    Thursday, December 13, 2012 8:30 PM

All replies

  • Try making sure your Callback typedef has the calling convention mentioned.

    Thursday, December 13, 2012 7:57 PM
  • Hi Robert,

    The issue here is that you create a callback for a non-static member function of a C++ class. Even though the signature of MetroApp::SomeMethod looks like void MetroApp::SomeMethod(void), the C++ compiler will actually make it void MetroApp::SomeMethod(MetroApp* this) because it must pass object state to it.

    You have three options:

    1. Declare SomeMethod as static.
    2. Declare SomeMethod outside of the class, such that it is no longer a member.
    3. Use wrappers to implement the callback. One example can be found here. Additionally, the STL may have some useful constructs for this.


    Thursday, December 13, 2012 8:30 PM
  • Sorry about disturbing the forums for such an easy problem, I didn't realize my function was in a class. I need some coffee... Thanks for the help!
    Thursday, December 13, 2012 9:10 PM