I have two windows that are created independently, each one with its class, messages handler and everything. The only thing they share is the instance.
The first window starts with WinMain.
The second one is created and shown from the message handler of the first one (by pressing a button).
What I want is to close the first window after opening the second one. I can just hide it but I suppose it still remains in memory (it's resources and everything), so I would to destroy it and free the memory allocated to it. Is there any way to do it. If I just DestroyWindow(firstwindow), the hole application exits. If I close the second one the application ends also (as it should be). Should I create a second instance for the second window to maintain the application running after destroying the first window? What is the usual way to do this? Thanks!
DefWindowProc() calls PostQuitMessage() on WM_DESTROY. Also, it is customary to see Windows samples doing the same in the window's window procedure (even though it is the standard behavior and therefore not needed). So I guess you either don't process WM_DESTROY, or you process it and call PostQuitMessage().
As you should know, posting the quit message (WM_QUIT) forces GetMessage() to return FALSE, meaning the message loop stops running and the main thread finally ends.
What you need to do is NOT post the quit message if the first window is closed while the second one is shown.