?
Solved

Access a member variable from a callback function

Posted on 2002-04-16
4
Medium Priority
?
364 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Independent Software Vendors: 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: 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…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
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.
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
Suggested Courses

752 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