c++ - 将成员函数指针转换为 TIMERPROC

标签 c++ winapi casting pointer-to-member

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

error C2664: 'SetTimer' : cannot convert parameter 4 from 'void (__stdcall CBuildAndSend::* )(HWND,UINT,UINT_PTR,DWORD)' to 'TIMERPROC'

回调需要绑定(bind)到它的原始类实例。如果有更好的方法来做到这一点,我会洗耳恭听。谢谢。

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

    ...
}

最佳答案

成员函数和 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

关于c++ - 将成员函数指针转换为 TIMERPROC,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6472948/

相关文章:

winapi - 将指针转换为 LRESULT 或从 LRESULT 转换指针

c++ - OSX "clang++ -lboost_mpi"` ->"ld: library not found for -lboost_mpi"在 brew install boost --with-mpi 之后

c++ - 将 Powershell.exe 的输入和输出重定向到 C++ 中的管道

c++ - 如何在 C++ 中打印粗体字符串?

c++ - 使用哪个类型转换; static_cast 还是 reinterpret_cast?

C# LINQ 筛选 IEnumerable<IX> 并表示为 IEnumerable<X :IX>

c++ - 在没有 `new` 关键字的情况下实例化一个类会导致在堆栈或堆上创建其内部变量吗?

C++排序(升序,降序)整数数组

c++ - 对象旋转/平移后重新计算正确的 AABB

c++ - 将可用空间转换为百分比 Windows C++