Solved

Access a member variable from a callback function

Posted on 2002-04-16
4
328 Views
Last Modified: 2013-11-20
How do I access a member variable from a callback function? Here's my problem; I have a class which initiates RAS dial functions. In it, I have a callback function (MyRasDialFunction) as required by the RAS API function RasDial(). When the callback function is called by RAS, I would like to accumulate messages in a member variable string. Unlike creating a thread, I can't pass a "this" pointer into RasDial(). How do I access a member variable from inside the callback function?


    void MyClass::TimeToDial()
    {
        // Set up RAS params
        ...

        // Initiate RAS dial
        dwResult = RasDial(NULL,
                            NULL,
                            &RASDialParams,
                            1,
                            MyRasDialFunction,
                            &m_hRasConn);

        // Check results
        ...
    }

    void WINAPI MyClass::MyRasDialFunction(HRASCONN hRasConn,
                                         UINT nMsg,
                                         RASCONNSTATE rascs,
                                         DWORD dwError,
                                         DWORD dwErrorEx)
    {
        if ( nMsg == WM_RASDIALEVENT )
        {
            // Handle dwError & dwErrorEx
            ...

            // Add msg to string
            switch ( rascs )
            {
                case RASCS_OpenPort:
                        m_sRASInfo += "RASCS_OpenPort\r\n";
                        break;

                case RASCS_PortOpened:
                        m_sRASInfo += "RASCS_PortOpened\r\n";
                        break;

                case RASCS_ConnectDevice:
                        m_sRASInfo += "RASCS_ConnectDevice\r\n";
                        break;
                    :
                    :
            }
        }
    }
0
Comment
Question by:Grailman
  • 2
4 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 100 total points
Comment Utility
As I assume it is a static member function, use a static member variable to hold 'this', e.g.

MyClass {

// ...

static void MyRasDialFunction(HRASCONN hRasConn,
                                        UINT nMsg,
                                        RASCONNSTATE rascs,
                                        DWORD dwError,
                                        DWORD dwErrorEx)

static MyClass* ms_pCurrent;

};


MyClass* MyClass::ms_pCurrent = NULL; // allocate storage for the static member

   void MyClass::TimeToDial()
   {
       ms_pCurrent = this;
       // Set up RAS params
       ...

       // Initiate RAS dial
       dwResult = RasDial(NULL,
                           NULL,
                           &RASDialParams,
                           1,
                           MyRasDialFunction,
                           &m_hRasConn);

       // Check results
       ...
   }

   void WINAPI MyClass::MyRasDialFunction(HRASCONN hRasConn,
                                        UINT nMsg,
                                        RASCONNSTATE rascs,
                                        DWORD dwError,
                                        DWORD dwErrorEx)
   {
    MyClass* pThis = ms_pCurrent;

       if ( nMsg == WM_RASDIALEVENT )
       {
           // Handle dwError & dwErrorEx
           ...

           // Add msg to string
           switch ( rascs )
           {
               case RASCS_OpenPort:
                       m_sRASInfo += "RASCS_OpenPort\r\n";
                       break;

               case RASCS_PortOpened:
                       m_sRASInfo += "RASCS_PortOpened\r\n";
                       break;

               case RASCS_ConnectDevice:
                       m_sRASInfo += "RASCS_ConnectDevice\r\n";
                       break;
                   :
                   :
           }
       }
   }
0
 
LVL 49

Expert Comment

by:DanRollins
Comment Utility
Alas, a static function can access only static member variables.  So with jkr's suggestion, you will need to use syntax like:

    pThis->m_sRASInfo += "RASCS_OpenPort\r\n";

whenever you access a member variable.  There is a technique I use that avoids that.  I make a static function 'stub' (MyStubRasDialFunction), but then it calls the 'real' member function:

void WINAPI MyClass::MyStubRasDialFunction(
    HRASCONN hRasConn, UINT nMsg,
    RASCONNSTATE rascs,
   DWORD dwError,DWORD dwErrorEx)
{
m_pThis-> MyRasDialFunction(hRasConn,nMsg,rascs,dwError,dwErrorEx);
}

Now, in the 'real' function, you are fully 'in the class' so there is no static/non-static dichotomy.

-- Dan
0
 
LVL 86

Expert Comment

by:jkr
Comment Utility
>>So with jkr's suggestion, you will
>>need to use syntax like:
>>  pThis->m_sRASInfo += "RASCS_OpenPort\r\n";

Thanks for clarifying, that's exactly what I meant.
0
 
LVL 1

Author Comment

by:Grailman
Comment Utility
Worked great jkr.
Thanks!
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now