Programming Tips - MFC: How to get the HWND handle for the main Window of my MFC program?

Date: 2008jun6 Update: 2025oct22 Framework: MFC Language: C/C++ Q. MFC: How to get the HWND handle for the main Window of my MFC program? A. First use AfxGetMainWnd() to get the CWnd class then use GetSafeHwnd() on that as this function demonstrates:
// AfxGetMainWnd() is broken in Visual Studio 2005 so here's my own: inline CWnd *MyGetMainWnd() { CWinApp *pApp; if ((pApp = AfxGetApp()) == NULL) return NULL; return pApp->m_pMainWnd; } inline HWND GetMainHwnd() { CWnd *pWnd; if ((pWnd = MyGetMainWnd()) == NULL) return NULL; return pWnd->GetSafeHwnd(); }