?
Solved

Access a member variable from a callback function

Posted on 2002-04-16
4
Medium Priority
?
383 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 400 total points
ID: 6945270
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
ID: 6947170
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
ID: 6947555
>>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
ID: 6977890
Worked great jkr.
Thanks!
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction: Displaying information on the statusbar.   Continuing from the third article about sudoku.   Open the project in visual studio. Status bar – let’s display the timestamp there.  We need to get the timestamp from the document s…
Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
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.
Loops Section Overview
Suggested Courses

839 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