【问题标题】:Converting Member Function Pointer to TIMERPROC将成员函数指针转换为 TIMERPROC
【发布时间】:2011-09-22 07:27:23
【问题描述】:

如何将成员函数指针转换为TIMERPROC 类型以用于WINAPI SetTimer?下面的代码 sn-p 显示了我现在是如何做的,但是当我编译时出现此错误:

错误 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

    ...
}

【问题讨论】:

  • Passing User Data with SetTimer 的可能重复项
  • 顺便说一句,每当您在 win32 API 中使用函数(或函数指针)进行强制转换时,通常都表明存在某种错误。如果它没有按原样编译,那么通常需要更改函数;添加演员只是在掩盖问题。幸运的是,C++(指向)成员函数与(指向)普通函数完全不同,因此在这种情况下甚至 reinterpret_cast 都不可能。

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


【解决方案1】:

Member-function 和 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

【讨论】:

  • 如果你真的想调用成员函数,仅仅使用静态成员函数是不够的!您可能希望将实例指针作为计时器 ID 传递,然后在收到 WM_TIMER 时,反转该过程并调用对象实例上的方法。
  • 谢谢!为了让它编译,我还必须声明静态成员回调函数引用的任何成员变量。对于创建的类的每个实例,我仍然可以期望每个成员变量/函数都有一个单独的实例吗?或者,因为它们被声明为静态的,类的每个实例是否都会引用单个变量或函数?如果是后者,那将是一个问题。
  • @David Heffernan:这是个好主意!提交作为答案,我会接受。
  • 其实比这更复杂。 Windows 会为您分配 ID。因此,您需要 ID 和实例之间的静态映射。见:stackoverflow.com/questions/4625184/…
  • 这里还有一篇好文章:greengingerwine.com/index.php/2011/05/…
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-03-10
相关资源
最近更新 更多