将成员函数指针转换为TIMERPROC

时间:2011-06-24 19:41:18

标签: c++ winapi casting member-function-pointers

如何将成员函数指针转换为TIMERPROC类型以与WINAPI SetTimer一起使用?下面的代码片段显示了我现在是如何做的,但是当我编译时,我得到了这个错误:

  

错误C2664:'SetTimer':无法将参数4从'void(__ stdcall CBuildAndSend :: *)(HWND,UINT,UINT_PTR,DWORD)'转换为'TIMERPROC'

回调需要绑定到其原始类实例。如果有更好的方法,我会全力以赴。感谢。

class CMyClass
{
public:
    void (CALLBACK CBuildAndSend::*TimerCbfn)( HWND hwnd, UINT uMsg, UINT_PTR idEvent, DWORD dwTime );

private:
    void CALLBACK TimeoutTimerProc( HWND hwnd, UINT uMsg, UINT_PTR idEvent, DWORD dwTime );
};

CMyClass::CMyClass()
{
    ...

    this->TimerCbfn = &CBuildAndSend::TimeoutTimerProc;

    ...

    ::CreateThread(
        NULL,                           // no security attributes
        0,                              // use default initial stack size
        reinterpret_cast<LPTHREAD_START_ROUTINE>(BasThreadFn), // function to execute in new thread
        this,                           // thread parameters
        0,                              // use default creation settings
        NULL                            // thread ID is not needed
        )
}

void CALLBACK CMyClass::TimeoutTimerProc( HWND hwnd, UINT uMsg, UINT_PTR idEvent, DWORD dwTime )
{
    ...
}

static DWORD MyThreadFn( LPVOID pParam )
{
    CMyClass * pMyClass = (CMyClass *)pParam;

    ...

    ::SetTimer( NULL, 0, BAS_DEFAULT_TIMEOUT, pMyClass->TimerCbfn ); // <-- Error Here

    ...
}

1 个答案:

答案 0 :(得分:9)

成员函数和TIMEPROC不兼容类型。

您需要创建成员函数static。然后它将起作用,假设参数列表在静态成员函数和TIMEPROC中都相同。

class CMyClass
{
public:
    //modified
    void (CALLBACK *TimerCbfn)(HWND hwnd, UINT uMsg, UINT_PTR idEvent, DWORD dwTime);

private:
    //modified
    static void CALLBACK TimeoutTimerProc(HWND hwnd, UINT uMsg, UINT_PTR idEvent, DWORD dwTime );
};

修改了函数指针和成员函数。现在应该可以了。

现在,由于回调函数变为静态,因此它无法访问类的非静态成员,因为函数中没有this指针。

要访问非静态成员,您可以执行以下操作:

class CMyClass
{
public:

    static void CALLBACK TimerProc( HWND hwnd, UINT uMsg, UINT_PTR idEvent, DWORD dwTime );

    //add this static member
    static std::map<UINT_PTR, CMyClass*> m_CMyClassMap; //declaration
};

//this should go in the  CMyClass.cpp file
std::map<UINT_PTR, CMyClass*> CMyClass::m_CMyClassMap;  //definition

static DWORD MyThreadFn( LPVOID pParam )
{
    CMyClass * pMyClass = (CMyClass *)pParam;

    UINT_PTR id = ::SetTimer( NULL, 0, BAS_DEFAULT_TIMEOUT, CMyClass::TimerProc);

    //store the class instance with the id as key!        
    m_CMyClassMap[id]= pMyClass; 
}

void CALLBACK CMyClass::TimerProc( HWND hwnd, UINT uMsg, UINT_PTR idEvent, DWORD dwTime )
{
    //retrieve the class instance
    CMyClass *pMyClass= m_CMyClassMap[idEvent];

    /*
      now using pMyClass, you can access the non-static 
      members of the class. e.g
      pMyClass->NonStaticMemberFunction();
    */
}

我从我的实现中删除了TimerCbfn,因为它并不真正需要。您可以将TimerProc作为最后一个参数直接传递给SetTimer