5 out of 8 rated this helpful - Rate this topic

PostQuitMessage function

Applies to: desktop apps only

Indicates to the system that a thread has made a request to terminate (quit). It is typically used in response to a WM_DESTROY message.

Syntax

VOID WINAPI PostQuitMessage(
  __in  int nExitCode
);

Parameters

nExitCode [in]

Type: int

The application exit code. This value is used as the wParam parameter of the WM_QUIT message.

Return value

This function does not return a value.

Remarks

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.

Examples

For an example, see Posting a Message.

Requirements

Minimum supported client

Windows 2000 Professional

Minimum supported server

Windows 2000 Server

Header

Winuser.h (include Windows.h)

Library

User32.lib

DLL

User32.dll

See also

Reference
GetMessage
PeekMessage
PostMessage
WM_DESTROY
WM_QUIT
Conceptual
Messages and Message Queues

 

 

Send comments about this topic to Microsoft

Build date: 2/3/2012

Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ
?? what happend to the "add community" editor??
?? what happend to the "add community" editor?? All linefeeds are removed and the markup functions are gone to indicate code...
not possible to create windows anymore if a window fails WM_CREATE with -1 and then PostQuitMessage
If the WM_CREATE of your window fails with -1 and then does a PostQuitMessage from its WM_DESTROY then it is impossible anymore to create any other window like for instance a MessageBox. This has been tested on W2K, XP and Win7 Why? dunno. You need to keep an indicator not to call PostQuitMessage if your WM_CREATE fails. Example to demostrate this. Like this code is the messagebox does not get displayed and returns immediately with IDOK. If you remove the PostQuitMessage the messagebox gets displayed --- LRESULT CALLBACK TestWndProc ( HWND in_hWnd, UINT in_uMsg, WPARAM in_wParam, LPARAM in_lParam ) { // which message? switch (in_uMsg) { // the test case: let it fail case WM_CREATE: return -1; // then post quit message case WM_DESTROY: PostQuitMessage(0); break; } // default processing return DefWindowProc(in_hWnd, in_uMsg, in_wParam, in_lParam); } VOID TestFailWnd(VOID) { /* locals */ HWND lv_hWnd; WNDCLASS lv_WndClass; // register the class memset(&lv_WndClass, 0, sizeof(WNDCLASS)); lv_WndClass.style = CS_DBLCLKS; lv_WndClass.lpfnWndProc = TestWndProc; lv_WndClass.hInstance = GetModuleHandle(0); lv_WndClass.hIcon = LoadIcon(0, IDI_APPLICATION); lv_WndClass.hCursor = LoadCursor(NULL, IDC_ARROW); lv_WndClass.lpszClassName = TEXT("CLASSXX"); RegisterClass(&lv_WndClass); // create window lv_hWnd = CreateWindowEx( 0, TEXT("CLASSXX"), "ABC", WS_MINIMIZEBOX | WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN | WS_CAPTION, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, 0, 0, GetModuleHandle(0), NULL); // result is NULL printf("lv_hWnd=%08X\n", lv_hWnd); // this is the problem: the messagebox will not get displayed because of the // WM_CREATE failed with -1 and the PostQuitMessage in WM_DESTROY MessageBox(0,"ABC","ABC",0); // halt the app printf("DONE\n"); getchar(); } void main(void) { TestFailWnd(); }
Wrong comment?
Sorry, EVHClarity, but setting the local variable hwnd in the WndProc to NULL will change nothing to the message loop's variables and structures, unless you make it a global variable (which it is not, in the code you posted). There must be more to it in your code. Or does it effect the DefWndProc call parameter after your WM_CLOSE processing?
Where did that WM_QUIT go?
PostQuitMessage is used after the window has been destroyed (case WM_DESTROY) making the handle invalid for GetMessage. You can see this by catching the last error after running GetMessage:

MSG msgs = { };
int catcherror;
while((catcherror = GetMessageW(&msgs, swhandle, 0, 0)) != 0)
{
  if(catcherror == -1)
    return msgs.message;
  TranslateMessage(&msgs);
  DispatchMessage(&msgs);
} // Take a look at the exit value output at the end of execution.

You can avoid the stall by assigning a null value to the variable storing the destroyed windows handle:


LRESULT CALLBACK WinProc(HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam)
{
switch(message)
{
  case(WM_DESTROY):
  hwnd = NULL;
  PostQuitMessage(0);
  ...

GetMessage will then pick up any messages in the thread. :-)