正在将成员函数指针转换为TIMERPROC
Converting Member Function Pointer to TIMERPROC
如何将成员函数指针转换为与WINAPI SetTimer
一起使用的TIMERPROC
类型?下面的代码片段显示了我现在是如何做的,但当我编译时,我得到了这个错误:
错误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
...
}
Member函数和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++的隐式转换
- 模板参数替换失败,并且未完成隐式转换
- 努力将整数转换为链表。不知道我在这里做错了什么
- HEX值到wchar_t字符(UTF-8)的转换
- lambda参数转换为constexpr技巧,然后获取带链接的数组
- 将 Qvector<uint8_t> 转换为 QString
- 如何在cuSparse中使用cusparseXcoo2csr从coo转换为csc
- 有关插入适配器的错误。[错误]请求从 'back_insert_iterator<vector<>>' 类型转换为非标量类型
- 在c++中使用nlohmann从类到json的转换
- 从"int*"强制转换为"unsigned int"会丢失精度错误
- 将Integer转换为4字节的unsined字符矢量(按大端字节顺序)
- 处理小于cpu数据总线的数据类型.(c++转换为机器代码)
- 如何使用OpenCV将RBG图像转换为HSV,并将H、S和V值保存为C++中的3个独立图像
- 复制列表初始化的隐式转换的等级是多少
- 正在将指针转换为范围
- 如何防止 c++ 在从浮点型转换为双精度型(不适用于 IO)时添加额外的小数?
- 将"打开的CV图像"中的"颜色"转换为整数格式
- 是否可以从int转换为enum类类型
- 了解 GLM- openGL 中的相机转换
- 正在将成员函数指针转换为TIMERPROC