Dave's Brain

Browse - Programming Tips - AfxGetMainWnd() is returning NULL. What can I do about this?

Date: 2010jan24
Product: Visual Studio
Language: C/C++

Q.  AfxGetMainWnd() is returning NULL.  What can I do about this?

A.  Apparently it returns NULL when called from a thread other than the main one.
Which means its not very useful.  Before Visual Studio 2005 it worked fine.
Use this function instead:

inline CWnd *MyGetMainWnd() { CWinApp *app; if ((app = AfxGetApp()) == NULL) return NULL; return app->m_pMainWnd; }

Add a comment

Sign in to add a comment
Copyright © 2008-2017, dave - Code samples on Dave's Brain is licensed under the Creative Commons Attribution 2.5 License. However other material, including English text has all rights reserved.