Message Processing

Windows graphical applications (GUI) are based on message queues. Windows operating system has an event and graphical management server running as a background system process. This process acts as a message queue server and all windows applications act as message queue clients. Application calls GetMessage to receive a message from the event server. This is a blocking call and it waits for a message to arrive. The operating system receives keyboard inputs and mouse movements or clicks or other system events. All these are events are passed to the event and graphical management server. The server constructs a message object of type MSG and sends this to the message queue client which is the current application. There can be many applications running but the server decides to send the message to the application whose top-level window is focused on inputs. GetMessage blocking call in WinMain returns and this event MSG is further passed to DispatchMessage(). DispatchMessage() is an API that indirectly triggers a call back to window procedure which is registered by application with windows class. The application does not call this function directly, Win32 subsystem calls this function when the application dispatches the particular message. GetMessage and DispatchMessage are generally placed in an infinite loop. It keeps receiving new messages and further dispatches to wndproc till the application exits.

Windows Event processing and Window procedure wndproc


The WindowProc function is an application-defined function that processes messages sent to a window. The WNDPROC type defines a pointer to this callback function. WindowProc is a place holder for the application-defined function name. Application processes only messages those are needed and for the rest, it passes to DefWindowProc() for default handling in Win32 subsystem.

  HWND hwnd,      /* handle to window */
  UINT uMsg,      /* message identifier */
  WPARAM wParam,  /* first message parameter */
  LPARAM lParam   /* second message parameter */


  • hwnd - Handle to the window.
  • uMsg - Specifies the message.
  • wParam - Specifies additional message information. The contents of this parameter depend on the value of the uMsg parameter.
  • lParam -Specifies additional message information. The contents of this parameter depend on the value of the uMsg parameter.

Return Values

The return value is the result of the message processing and depends on the message sent.

Source Code

#include <windows.h>

                         UINT message, 
                         WPARAM wParam,
                         LPARAM lParam);

int APIENTRY WinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPSTR     lpCmdLine,
                     int       nCmdShow)
  MSG msg;
  HWND hWnd;
  ZeroMemory(&amp;wc, sizeof(WNDCLASS));           = CS_HREDRAW | CS_VREDRAW;
  wc.lpfnWndProc     = (WNDPROC)WndProc;
  wc.hInstance       = hInstance;
  wc.hCursor         = LoadCursor(NULL, IDC_ARROW);
  wc.hbrBackground   = (HBRUSH)(COLOR_BACKGROUND);
  wc.lpszClassName   = (LPCTSTR)"MyWin32Class";


   hWnd = CreateWindow("MyWin32Class", "Hello World!", WS_OVERLAPPEDWINDOW,

  ShowWindow(hWnd, nCmdShow);
  while (GetMessage(&msg, NULL, 0, 0)) 
  return 0;

  switch (message) 
    case WM_DESTROY:
      return DefWindowProc(hWnd, message, wParam, lParam);
   return 0;

About our authors: Team EQA

Further readings

What is WinMain function in windows C? Main vs WinMain, WinMain prototype and arguments.
Describes What is WinMain()? Entry point funtion in Windows exe. Function prototype. Implementation, Command line argument passing. Show Window flag.

Steps to write a basic window program with C and Win32 APIs.
Include windows.h, Define WinMain, Define WNDCLASS attributes, RegisterClass, CreateWindow, ShowWindow, UpdateWindow, GetMessage, DispatchMessage, DefWindowProc

What are LPARAM and WPARAM in window proc function ? How Win32 subsystem pass parameters to this function?
Explains the parameters WPARAM and LPARAM and in window procedure function. What is this W and L stands for? How Win32 subsystem pass parameters?

What are the members of MGS structure and utility of each members?
What are the members of MGS structure and utility of each members?