createThread 创建线程,第三个参数线程函数在工程里怎么声明。
请教下:
mfc工程,创建了一个辅助线程,在线程中读取串口,点击“启动”按钮后读取串口数据。
在那个启动按钮的处理函数中创建这个辅助线程。
代码如下:
- C/C++ code
m_readfilethread = CreateThread( (LPSECURITY_ATTRIBUTES)NULL, 0, (LPTHREAD_START_ROUTINE)GetSerialContent, GetSafeHwnd(), 0, &ol ); ASSERT(m_readfilethread != NULL);
在程序中CUpperendDlg.h 是这样声明的:
- C/C++ code
afx_msg CString WINAPI GetSerialContent(HWND hSendWnd);
编译后,报下面这样的错误:
error C2440: 'type cast' : cannot convert from '' to 'unsigned long (__stdcall *)(void *)'
None of the functions with this name in scope match the target type
[解决办法]
线程函数声明:
DWORD WINAPI ThreadProc(LPVOID lpParameter);
你的线程函数貌似是一个消息处理函数,线程函数应该是一个全局函数。
如果在线程函数需要使用某个对象,那么可以用lpParameter传递这个对象的地址。
[解决办法]
static unsigned long WINAPI GetSerialContent(void *param);
[解决办法]
使用类静态函数也是可以的,但是类静态函数只能访问类静态成员,所以如果想访问类的其它成员,还是用全局函数的好,然后把对象指针通过参数传递进去。
[解决办法]