VC8下多线程环境中AfxGetMainWnd()返回空指针问题及解决方法

来源:互联网 发布:淘宝网二手汽车 编辑:程序博客网 时间:2024/06/07 17:48

一个vc6的项目放到vc8下重新编译 这里死活过不去 查了些资料无果 后来翻到一句老外的回答

If AfxGetMainWnd is called from the application’s primary thread, it returns the application’s main window according to the above rules. If the function is called from a secondary thread in the application, the function returns the main window associated with the thread that made the call.

大概意思就是说在子线程里面调用AfxGetMainWnd()返回的是和当前线程相关联的窗体句柄而不是当前程序的主窗体句柄。不知道这是不是vc8的一个改变,也没时间去细查。

解决方法1:
CWnd* m_pCWnd = NULL;
在OnInitDialog里 m_pCWnd = AfxGetMainWnd();

解决方法2:
调用AfxGetMainWnd()的地方替换成AfxGetApp()->m_pMainWnd

编译后运行问题解决

 

 

转自: http://www.freshbug.com/archives/84