change afxGetMainWnd to afxGetApp()->m_pMainWnd, so it will not return NULL.