Solved

Access a member variable from a callback function

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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
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.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

831 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