Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8420459
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T02:53:26+00:00 2026-06-10T02:53:26+00:00

I’ve got problems passing a member function of a C++ CLI class to a

  • 0

I’ve got problems passing a member function of a C++ CLI class to a native C callback from a library.

To be precise its the Teamspeak 3 SDK.

You can pass a non member function using the following code without problem:

struct ClientUIFunctions funcs;

/* Initialize all callbacks with NULL */
memset(&funcs, 0, sizeof(struct ClientUIFunctions));
funcs.onConnectStatusChangeEvent        = onConnectStatusChangeEvent;

But I need to pass a pointer to a member function, for example:

    funcs.onConnectStatusChangeEvent        = &MyClass::onConnectStatusChangeEvent;

Any other idea how to use the event within a non static member function is welcome to.

Thanks in advance!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T02:53:27+00:00Added an answer on June 10, 2026 at 2:53 am

    This can only be done via a static class function because C doesn’t know anything about the vtable or what object the function is part of. See below for a C++ and Managed C++ example

    This could however be a work around, build a wrapper class which handles all the callbacks you need.

    #include <string.h>
    
    struct ClientUIFunctions
    {
         void (*onConnectStatusChangeEvent)(void);
    };
    
    class CCallback
    {
    public:
         CCallback()
          {
               struct ClientUIFunctions funcs;
    
               // register callbacks
               my_instance = this;
    
               /* Initialize all callbacks with NULL */
               memset(&funcs, 0, sizeof(struct ClientUIFunctions));
               funcs.onConnectStatusChangeEvent        = sOnConnectStatusChangeEvent;
    
          }
    
         ~CCallback()
          {
               // unregister callbacks
               my_instance = NULL;
          }
    
         static void sOnConnectStatusChangeEvent(void)
          {
               if (my_instance)
                my_instance->OnConnectStatusChangeEvent();
          }
    
    private:
         static CCallback *my_instance;
    
         void OnConnectStatusChangeEvent(void)
          {
               // real callback handler in the object
          }
    };
    
    CCallback *CCallback::my_instance = NULL;
    
    int main(int argc, char **argv)
    {
        CCallback *obj = new CCallback();
    
        while (1)
        {
             // do other stuff
        }
    
        return 0;
    }
    

    Another possibility would be if the callback supports and void *args like void (*onConnectStatusChangeEvent)(void *args); which you can set from the plugin. You could set the object in this args space so in de sOnConnectStatusChangeEvent you would have something like this:

    static void sOnConnectStatusChangeEvent(void *args)
        {
               if (args)
                  args->OnConnectStatusChangeEvent();
        }
    

    For managed C++ it should be something like this, however I can’t get it to compile because it doesn’t like the template brackets..

    wrapper.h:

    using namespace std;
    using namespace System;
    using namespace System::Runtime::InteropServices;
    using namespace System::Text;
    
    namespace Test
    {
        struct ClientUIFunctions
        {
            void (*onConnectStatusChangeEvent)(void);
        };
    
        public delegate void ConnectStatusChangeEvent(void);
    
        public ref class ManagedObject
        {
        public:
            // constructors
            ManagedObject();
    
            // destructor
            ~ManagedObject();
    
            //finalizer
            !ManagedObject();
    
            event ConnectStatusChangeEvent^ OnConnectStatusChangeEvent {
                void add(ConnectStatusChangeEvent^ callback) {
                    m_connectStatusChanged = static_cast<ConnectStatusChangeEvent^> (Delegate::Combine(m_connectStatusChanged, callback));
                }
    
                void remove(ConnectStatusChangeEvent^ callback) {
                    m_connectStatusChanged = static_cast<ConnectStatusChangeEvent^> (Delegate::Remove(m_connectStatusChanged, callback));
                }
    
                void raise(void) {
                    if (m_connectStatusChanged != nullptr) {
                        m_connectStatusChanged->Invoke();
                    }
                }
            }
    
        private:
            ConnectStatusChangeEvent^ m_connectStatusChanged;   
        };
    
        class CCallback
        {
        public:
            static void Initialize(ManagedObject^ obj);
            static void DeInitialize(void);
    
        private:
            static void sOnConnectStatusChangeEvent(void);
    
            static gcroot<ManagedObject^> m_objManagedObject;
        };
    }
    

    wrapper.cpp:

    #include <string.h>
    #include "wrapper.h"
    
    using namespace System;
    using namespace Test;
    
    void CCallback::Initialize(ManagedObject^ obj)
    {
        struct ClientUIFunctions funcs;
    
        // register callbacks
        m_objManagedObject = obj;
    
        /* Initialize all callbacks with NULL */
        memset(&funcs, 0, sizeof(struct ClientUIFunctions));
        funcs.onConnectStatusChangeEvent        = sOnConnectStatusChangeEvent;
    
    }
    
    void CCallback::DeInitialize(void)
    {
        // unregister callbacks
        m_objManagedObject = nullptr;
    }
    
    void CCallback::sOnConnectStatusChangeEvent(void)
    {
        if (m_objManagedObject != nullptr)
            m_objManagedObject->OnConnectStatusChangeEvent();
    }
    
    
    // constructors
    ManagedObject::ManagedObject()
    {
        // you can't place the constructor in the header but just for the idea..
        // create wrapper
        CCallback::Initialize(this);          
    }
    
    // destructor
    ManagedObject::~ManagedObject()
    {
        this->!ManagedObject();
    }
    
    //finalizer
    ManagedObject::!ManagedObject()
    {
        CCallback::DeInitialize();        
    }
    
    gcroot<ManagedObject^> CCallback::m_objManagedObject = nullptr;
    
    int main(array<System::String ^> ^args)
    {
         ManagedObject^ bla = gcnew ManagedObject();
    
         while (1)
         {
          // do stuff
         }
    
         return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a view passing on information from a database: def serve_article(request, id): served_article
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I've got a string that has curly quotes in it. I'd like to replace
I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a text area in my form which accepts all possible characters from
I'm trying to select an H1 element which is the second-child in its group
i got an object with contents of html markup in it, for example: string

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.