Solved

Access a member variable from a callback function

Posted on 2002-04-16
4
347 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 100 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

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
iSeries FTP Exit Program 8 163
ORA-01403: no data found 43 112
difference between String.subString() and String.subSequence() 6 273
Authenticate using sesu from script 7 118
Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli…
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…
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.

730 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