Did you try to post WM_CLOSE to the main window.
AfxGetMainWnd()->PostMessage(WM_CLOSE, 0, 0);

[email protected]
Bangalore