AfxGetMainWnd返回NULL
来源:互联网 发布:光大证券mac无法登陆 编辑:程序博客网 时间:2024/05/19 00:14
关于在线程中使用AfxGetMainWnd()出错的问题.
AfxGetMainWnd()得到的是当前线程的主窗口(如果有的话).
因为主窗口是属于主线程的,所以想得到主窗口HWND值,
只能在主线程中用AfxGetMainWnd(),但要不是处在主线程中,
AfxGetMainWnd()可能是从当前线程查询主窗口的。但好像
AfxGetMainWnd()不能跨线程,故要出错.要想在线程中使用
主窗口的HWND值,可以把主窗口的HWND值传给线程.也
可以用AfxGetApp()先取得主线程,再通过CWinThread的类成
员m_pMainWnd获得主窗口(AfxGetApp()->m_pMainWnd->m_hWnd)
If AfxGetMainWnd is called from the application's primary thread,
it returns the application'smain 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.
今天好像找到原因了~ 我那个线程是用CreateThread创建的,而 CreateThread是由
操作系统提供的接口,在 CreateThread创建的线程中用MFC的函数AfxGetMainWnd()会有问题.
摘自:BBS 水木清华站∶精华区
如果用MFC编程,不要用CreateThread,如果只是使用Runtime Library,用
_BegingThread,总之,不要轻易使用CreateThread
这是因为在MFC和RTL中的函数有可能会用到些它们所封装的公用变量,也就是
说AfxBeginThread和_BeginThread都有自己的启动代码是CreateThread所没有的
在用CreateThread所创建的线程中使用MFC的类和RTL函数就有可能出现问题
如果你是用汇编编写win32程序并且在线程函数中也不调用MFC和RTL的函数,那用
CreateThread就没问题,或者你虽然是用C写线程函数,但你很小心没调用RTL函数
也不会有问题
CreateThread是由操作系统提供的接口,而AfxBeginThread和_BeginThread则是编译
器对它的封装
因为主窗口是属于主线程的,所以想得到主窗口HWND值,
只能在主线程中用AfxGetMainWnd(),但要不是处在主线程中,
AfxGetMainWnd()可能是从当前线程查询主窗口的。但好像
AfxGetMainWnd()不能跨线程,故要出错.要想在线程中使用
主窗口的HWND值,可以把主窗口的HWND值传给线程.也
可以用AfxGetApp()先取得主线程,再通过CWinThread的类成
员m_pMainWnd获得主窗口(AfxGetApp()->m_pMainWnd->m_hWnd)
If AfxGetMainWnd is called from the application's primary thread,
it returns the application'smain 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.
今天好像找到原因了~ 我那个线程是用CreateThread创建的,而 CreateThread是由
操作系统提供的接口,在 CreateThread创建的线程中用MFC的函数AfxGetMainWnd()会有问题.
摘自:BBS 水木清华站∶精华区
如果用MFC编程,不要用CreateThread,如果只是使用Runtime Library,用
_BegingThread,总之,不要轻易使用CreateThread
这是因为在MFC和RTL中的函数有可能会用到些它们所封装的公用变量,也就是
说AfxBeginThread和_BeginThread都有自己的启动代码是CreateThread所没有的
在用CreateThread所创建的线程中使用MFC的类和RTL函数就有可能出现问题
如果你是用汇编编写win32程序并且在线程函数中也不调用MFC和RTL的函数,那用
CreateThread就没问题,或者你虽然是用C写线程函数,但你很小心没调用RTL函数
也不会有问题
CreateThread是由操作系统提供的接口,而AfxBeginThread和_BeginThread则是编译
器对它的封装
- AfxGetMainWnd返回NULL
- AfxGetMainWnd返回NULL
- AfxGetMainWnd()返回NULL
- AfxGetMainWnd()为什么返回NULL
- AfxGetMainWnd返回NULL也是可能的
- AfxGetMainWnd() return NULL
- 多线程AfxGetMainWnd() 返回空
- AfxGetMainWnd 返回值
- 【框架-MFC】MFC 应用程序 AfxGetMainWnd();在子线程中返回null
- AfxGetMainWnd()
- FacesContext.getCurrentInstance() 返回NULL
- getLastKnownLocation 返回null
- findViewById()返回 null
- findViewById返回null
- Andorid findViewById()返回Null
- findViewById()返回null
- getLayoutParams 返回null
- GetProcAddress返回NULL
- IOS开发网络篇之──ASIHTTPRequest下载示例(支持断点续传)
- c++面试题
- 2012-12-21怎么说
- ECCMS解决IE8无法上传图片的问题
- 一致性hash算法 - consistent hashing
- AfxGetMainWnd返回NULL
- More Effective c++阅读
- C# 对xml操作
- 教你如何迅速秒杀掉:99%的海量数据处理面试题
- 用tar归档 linux
- C# / .net 时间格式转换
- 《学习之道》学习笔记
- 数据类型转换(char * ,char [] ,string ,int char)
- 避免JS冲突