-1

I am in the process of learning the Win32 API. I have a problem where the window is visibly closed but the application is running in the background (I can see this using the Windows 10 task manager). Here is my code that closes the window:

case WM_CLOSE:
    DestroyWindow(hwnd);
break;

case WM_DESTROY:
    PostQuitMessage(0);
break;

My application is called a.exe and you can see it in this screenshot I took of the Windows 10 task manager.

It is a.exe

nobody
  • 19,814
  • 17
  • 56
  • 77
Hyden
  • 289
  • 1
  • 9
  • 1
    What are you returning from the window procedure ([1](https://msdn.microsoft.com/en-ca/library/windows/desktop/ms632617(v=vs.85).aspx) [2](https://msdn.microsoft.com/en-us/library/windows/desktop/ms632620(v=vs.85).aspx))? What would be really helpful is an [MCVE](http://stackoverflow.com/help/mcve). – chris Oct 31 '15 at 14:32
  • It turn's out it's some issue with the console. It won't close the application if the console isn't closed and I'm running FreeConsole(). I'm now closing the console as well and it's working. Thanks though! – Hyden Oct 31 '15 at 14:49

2 Answers2

2

Read the docs:

"The PostQuitMessage function posts a WM_QUIT message to the thread's message queue and returns immediately; the function simply indicates to the system that the thread is requesting to quit at some time in the future. When the thread retrieves the WM_QUIT message from its message queue, it should exit its message loop and return control to the system. The exit value returned to the system must be the wParam parameter of the WM_QUIT message.

The WM_QUIT message is not associated with a window and therefore will never be received through a window's window procedure. It is retrieved only by the GetMessage or PeekMessage functions."

See think link also.

Community
  • 1
  • 1
OldProgrammer
  • 12,050
  • 4
  • 24
  • 45
  • It turn's out it's some issue with the console. It won't close the application if the console isn't closed and I'm running FreeConsole(). I'm now closing the console as well and it's working. Thanks though! – Hyden Oct 31 '15 at 14:49
  • @Hyden update your question with the information about the console, then post your own answer. – Mark Ransom Oct 31 '15 at 16:17
  • @MarkRansom Okay, thanks. I have updated my question with a clearly marked edit and I have posted an answer below. – Hyden Oct 31 '15 at 16:22
  • @Hyden: Your question doesn't show the code that allocates the console. This is an essential detail of your problem. As posted, the question is not really useful to future visitors that may have the same problem. They may not even know, that they have the same issue, since the question is lacking important details. – IInspectable Oct 31 '15 at 16:40
0

I have discovered the issue. Thanks for all the help but the window was actually being closed but the console was still open in the background. I was running:

FreeConsole();

and that was hiding the console. When I closed the window the console kept running and I am now closing the console as well and it works.

Hyden
  • 289
  • 1
  • 9
  • 2
    The console *shouldn't* have any effect on your program's ability to exit. Something else must be going on. But at any rate, calling `FreeConsole` is usually a sign that you should be building a GUI application (`/SUBSYSTEM:Windows`) rather than a console application (`/SUBSYSTEM:Console`). – Harry Johnston Oct 31 '15 at 20:10
  • Sorry, that wasn't very clear. What I meant was FreeConsole() was hiding the console and when I closed the window the console carried on running in the background. The console was what was showing up in task manager, I've removed FreeConsole() and now I can close the console and there is nothing in task manager. – Hyden Nov 26 '15 at 16:23