How to Close/Kill Parent from MFC DLL

jensjoseph's Avatar, Join Date: Nov 2007
Newbie Member
Hi

I have an MFC DLL thats called by another application. When I hit Alt-F4 on the parent window, it closes itself and my MFC DLL application as well.

I have been trying to implement the same from my MFC DLL application as well. i.e. When I hit Alt+F4 from my MFC DLL app, it should close itself and the calling application as well.

I've tried the following in the OnClose message handler of the MFC DLL application:

1. DestroyWindow(GetParent()->m_hWnd);
2. DestroyWindow(AfxGetMainWnd()->GetParent()->m_hWnd);
3. DestroyWindow(AfxGetMainWnd()->m_hWnd);
4. PostMessage(AfxGetMainWnd()->m_hWnd, WM_SYSCOMMAND, SC_CLOSE, NULL);

However, none of them seems to be working: When I hit Alt+F4, the MFC DLL application closes followed by a crash message from Windows.

A quick reply would help immensely.

Thanks.
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
Try sending the WM_CLOSE message
jensjoseph's Avatar, Join Date: Nov 2007
Newbie Member
Are you saying
PostMessage(AfxGetMainWnd()->m_hWnd, WM_CLOSE, SC_CLOSE, NULL);
jensjoseph's Avatar, Join Date: Nov 2007
Newbie Member
Are you saying
PostMessage(AfxGetMainWnd()->m_hWnd, WM_CLOSE, NULL, NULL);
jensjoseph's Avatar, Join Date: Nov 2007
Newbie Member
I tried WM_CLOSE. But it still crashes.
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
Even Alt + F4 crashes so I think there is some problem in the cleanup of your code.