VC2008中AfxGetMainWnd函数的变化

来源:互联网 发布:昆山大数据产业园招聘 编辑:程序博客网 时间:2024/06/02 04:26

今天把一个VC6的旧项目转为VC2008项目,运行后程序挂掉。中断后发现程序挂在工作线程中调用AfxGetMainWnd后,因为AfxGetMainWnd返回了NULL指针,随后导致非法访问。

 

查看MSDN,发现如下描述:

 

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函数在主线程下,返回主窗口指针。在其它线程下,返回当前线程的窗口指针。因为我是在工作线程下调用的AfxGetMainWnd函数,该线程没有创建窗口,所以使该函数返回空指针。

 

之前使用VC6的时候,在不管在任何线程下AfxGetMainWnd都是返回主窗口的指针。因为一直使用VC6,不知道微软什么时候更改了这个函数的实现。