Win32 API Reference

GetMessage

BOOL GetMessage(
    LPMSG lpMsg,
    HWND hWnd,
    UINT wMsgFilterMin,
    UINT wMsgFilterMax
);

The GetMessage function retrieves messages from the calling thread's message queue and places the message in the specified MSG structure. This is a core function for Windows message loop processing.

Parameters

Parameter Type Description
lpMsg LPMSG A pointer to a MSG structure that receives message information. The structure contains message parameters, such as the type of message, its posting time and location, and values depending on its type.
hWnd HWND A handle to the window whose messages are to be retrieved. If this parameter is NULL, the function retrieves messages for any window that is associated with the current thread.
wMsgFilterMin UINT The minimum value in the range of messages to be retrieved. If this parameter is zero, the first message retrieved is the one at the front of the queue, regardless of its type.
wMsgFilterMax UINT The maximum value in the range of messages to be retrieved. If this parameter is zero, the first message retrieved is the one at the front of the queue, regardless of its type.

Return Value

If the function retrieves a message other than WM_QUIT, the return value is nonzero.

If the function retrieves the WM_QUIT message, the return value is zero.

If an error occurs, the return value is -1. It occurs if lpMsg is an invalid pointer or if the thread is in a bad state.

Remarks

The GetMessage function terminates the application if it retrieves a WM_QUIT message. This message is posted when the PostQuitMessage function is called.

If the message queue is empty, GetMessage waits until a message is available.

The parameter hWnd is used to filter messages. If you want to retrieve messages for a specific window, provide its handle. If you set hWnd to NULL, GetMessage retrieves messages posted to any window associated with the calling thread.

The wMsgFilterMin and wMsgFilterMax parameters allow you to specify a range of message identifiers to retrieve. This is useful for optimizing message processing by ignoring messages that the application does not need to handle.

Typically, GetMessage is used within a loop (the message loop) to process messages sent to a thread's message queue.

See Also

MSG Structure

DispatchMessage

PostQuitMessage

PeekMessage

Example Usage


MSG msg;
BOOL bRet;

// Message loop
while( (bRet = GetMessage( &msg, NULL, 0, 0 )) != 0 )
{
    if (bRet == -1)
    {
        // Handle the error
        break;
    }

    TranslateMessage(&msg);
    DispatchMessage(&msg);
}

// If GetMessage returned 0, WM_QUIT was received.
// The application should exit here.
return msg.wParam;