WindowProc Function

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 placeholder for the application-defined function name.

Syntax

LRESULT CALLBACK WindowProc(      
    HWND hwnd,     UINT uMsg,     WPARAM wParam,     LPARAM lParam );

Parameters

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

Return Value

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

Remarks

To make your application more robust, you should use structured exception handling to trap and handle any errors that might occur in the callback. The following code shows the guarded body (various message handlers) in the __try, and exception filters and exception handler block in the __except block.

	
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
  __try
  {
    // guarded body
    switch (message)
    {
      HANDLE_MSG(hwnd, WM_CREATE, Cls_OnCreate);
      HANDLE_MSG(hwnd, WM_COMMAND, Cls_OnCommand);
      HANDLE_MSG(hwnd, WM_PAINT, Cls_OnPaint);
      HANDLE_MSG(hwnd, WM_DESTROY, Cls_OnDestroy);
      HANDLE_MSG(hwnd, WM_NOTIFY, Cls_OnNotify);
    } 
  } 
  __except (filter-expression /* evaluate filter */) 
  {
    // exception handler block 
  }		
  return DefWindowProc(hwnd, message, wParam, lParam);
}

		

If your application runs on a 32-bit version of Windows operating system, uncaught exceptions from the callback will be passed onto higher-level exception handlers of your application when available.

However, if your application runs on a 64-bit version of Windows operating system or WOW64, you should be aware that a 64-bit operating system handles uncaught exceptions differently, based on its 64-bit processor and exception architecture, and calling convention. The following table summarizes all possible ways of a 64-bit Windows operating system or WOW64 handling uncaught exceptions.

Behavior type How the system handles uncaught exceptions
1The system suppresses any uncaught exceptions.
2 The system first terminates the process, and then the Program Compatibility Assistant (PCA) offers to fix it the next time you run the application. You can disable the PCA mitigation by adding a Compatibility section to the application manifest.
3 The system calls the exception filters but suppresses any uncaught exceptions when it leaves the callback scope, without invoking the associated handlers.

The following table shows how a 64-bit version of Windows operating system or WOW64 handles uncaught exceptions. Notice that behavior type 2 only applies to the 64-bit version of the Windows 7 operating system.

Operating SystemWOW6464-bit Windows
Windows XP31
Windows Server 200331
Windows Vista31
Windows Vista SP111
Windows 712

Function Information

HeaderDeclared in Winuser.h, include Windows.h
Import libraryNone
Minimum operating systems Windows 95, Windows NT 3.1

See Also

Tags : winproc


Page view tracker