none
Interop: how to pass an unmanaged member function pointer to a c++/cli member function

    Question


  • I have an unmanaged class whose member function I want to use as a callback in a c++/cli class:

    Compiled as an MFC dll (i..e unmanaged c++)

    class Unmanaged
    {
       public:
          int unmanagedCallback(std::string msg) { cout << msg; };

    };


    Compiled with /clr (i.e. c++/cli)

    class Managed 
    {
    public:
    void doSomething( /* somehow pass unmanagedCallback */) {}
    }

    How do I pass (and invoke) the unmanaged member function in the managed code?

    Tuesday, May 29, 2012 8:09 PM

Answers

  • Hi Mmsood,

    Welcome to the MSDN Forum.

    Based on your description, it seems that this is an issue about Delegate in C++, so please take a look at this article: http://msdn.microsoft.com/en-us/library/367eeye0(v=vs.100).aspx 

    This topic demonstrates the marshalling of callbacks and delegates (the managed version of a callback) between managed and unmanaged code using Visual C++.

    The following code examples use the managed, unmanaged #pragma directives to implement managed and unmanaged functions in the same file, but the functions could also be defined in separate files. Files containing only unmanaged functions do not need to be compiled with the /clr (Common Language Runtime Compilation).

    I hope this will be helpful.

    Best regards,


    Mike Feng
    MSDN Community Support | Feedback to us
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Wednesday, May 30, 2012 10:01 AM
  • In just native code, this page is pretty helpful when understanding the different ways to and when to use member function pointers:

    http://www.parashift.com/c++-faq-lite/pointers-to-members.html

    Managed C++/CLI can use function pointers the same way as C++...If you really want to go for passing pointers around. That being said, this seems like it would be out of scope to simply providing a callback from a unmanaged class.

    However,  in your case, I would just write a managed wrapper function that indirectly called the unmanaged function rather than worry about function pointers, and just keep a reference to the unmanaged class:

    class Unmanaged
    {
       public:
          int unmanagedCallback(std::string msg) { cout << msg; };
    
    };
    
    
    //Compiled with /clr (i.e. c++/cli)
    
    public ref class Managed 
    {
    private:
    Unmanaged cl;
    public:
    
    void doSomething(String^ Message) {cl.unmanagedCallback(marshal_as<std::string>(Message));}
    }

    Wednesday, May 30, 2012 5:32 PM

All replies

  • Hi Mmsood,

    Welcome to the MSDN Forum.

    Based on your description, it seems that this is an issue about Delegate in C++, so please take a look at this article: http://msdn.microsoft.com/en-us/library/367eeye0(v=vs.100).aspx 

    This topic demonstrates the marshalling of callbacks and delegates (the managed version of a callback) between managed and unmanaged code using Visual C++.

    The following code examples use the managed, unmanaged #pragma directives to implement managed and unmanaged functions in the same file, but the functions could also be defined in separate files. Files containing only unmanaged functions do not need to be compiled with the /clr (Common Language Runtime Compilation).

    I hope this will be helpful.

    Best regards,


    Mike Feng
    MSDN Community Support | Feedback to us
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Wednesday, May 30, 2012 10:01 AM
  • In just native code, this page is pretty helpful when understanding the different ways to and when to use member function pointers:

    http://www.parashift.com/c++-faq-lite/pointers-to-members.html

    Managed C++/CLI can use function pointers the same way as C++...If you really want to go for passing pointers around. That being said, this seems like it would be out of scope to simply providing a callback from a unmanaged class.

    However,  in your case, I would just write a managed wrapper function that indirectly called the unmanaged function rather than worry about function pointers, and just keep a reference to the unmanaged class:

    class Unmanaged
    {
       public:
          int unmanagedCallback(std::string msg) { cout << msg; };
    
    };
    
    
    //Compiled with /clr (i.e. c++/cli)
    
    public ref class Managed 
    {
    private:
    Unmanaged cl;
    public:
    
    void doSomething(String^ Message) {cl.unmanagedCallback(marshal_as<std::string>(Message));}
    }

    Wednesday, May 30, 2012 5:32 PM