Microsoft KB Archive/35774

From BetaArchive Wiki
< Microsoft KB Archive
Revision as of 14:15, 21 July 2020 by X010 (talk | contribs) (Text replacement - "&" to "&")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

INFO: Differences Between PostAppMessage and PostMessage Funcs


The information in this article applies to:

  • Microsoft Windows Software Development Kit (SDK) versions 3.1, 3.0


The following information describes the differences between the PostAppMessage and PostMessage functions, and the circumstances under which each should be used.


In most cases, the PostMessage function should be used. Essentially, both functions accomplish the same result, but PostMessage uses a window handle, and PostAppMessage uses a task handle to identify the destination window for the message.

In the Windows environment, it is possible to create a task that does not have a window associated with it, but not vice versa. Under some circumstances an application must send a message to a windowless application; there is no window handle to use as a parameter to PostMessage. In this case, use the PostAppMessage function to send the message using the task handle.

However, if an application will receive messages sent by PostAppMessage, its message loop must be modified. When a message is posted by PostAppMessage and retrieved by GetMessage, the hwnd field of the MSG structure is NULL because no window was specified as the target for the message. Therefore, it is important to process this special case and to perform whatever processing is appropriate. An application must not pass an MSG structure with a NULL hwnd field to the DispatchMessage function. Doing so will cause an error. Some possibilities for handling this special case are listed below:

  • Process the message within the message loop.
  • Pass the message to another procedure that is set up to process special cases.
  • Set the hwnd field of the MSG structure to the window handle of the window that should receive the message.
  • Change any of the other values in the MSG structure before passing the message along.

The following code demonstrates processing a message posted by the PostAppMessage function. After the window is drawn, pressing the left mouse button in the window generates a PostMessage call, and pressing the right mouse button generates an identical PostAppMessage call. When the application processes the message posted by PostAppMessage, it beeps the speaker and then passes the message to the main window.

   // MinWin - PostMessage versus PostAppMessage example.

   #include <windows.h>

   char szAppName[] = "MinWin";
   HWND hMainWnd;

   long FAR PASCAL WndProc(HWND, unsigned, WORD, LONG);

   int PASCAL WinMain(HANDLE hInstance, HANDLE hPrevInstance,
                      LPSTR lpszCmdLine, int nCmdShow)
      MSG msg;
      WNDCLASS wndclass;

      if (!hPrevInstance)
         { = CS_HREDRAW | CS_VREDRAW;
         wndclass.lpfnWndProc = WndProc;
         wndclass.cbClsExtra = 0;
         wndclass.cbWndExtra = 0;
         wndclass.hInstance = hInstance;
         wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
         wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
         wndclass.hbrBackground = COLOR_WINDOW + 1;
         wndclass.lpszMenuName = NULL;
         wndclass.lpszClassName = szAppName;

         if (!RegisterClass(&wndclass))
            return FALSE;

      hMainWnd = CreateWindow(szAppName, szAppName, WS_OVERLAPPEDWINDOW,
           NULL, NULL, hInstance, NULL);

      ShowWindow(hMainWnd, nCmdShow);

      while (GetMessage(&msg, NULL, 0, 0))
         if (msg.hwnd == NULL) // Beep, then pass the message to
            {                  //  the window
            msg.hwnd = hMainWnd;


      return msg.wParam;

   long FAR PASCAL WndProc(HWND hWnd, unsigned iMessage,
                           WORD wParam, LONG lParam)
      HANDLE hInst;
      HANDLE hTask;

      hInst = GetWindowWord(hWnd, GWW_HINSTANCE);

      switch (iMessage)
      case WM_LBUTTONDOWN:
         PostMessage(hWnd, WM_USER+0x1000, 0, 0L);

      case WM_RBUTTONDOWN:
         PostAppMessage(GetWindowTask(hWnd), WM_USER+0x1000, 0, 0L);

      case WM_USER+0x1000:
         MessageBox(NULL, "Message Received", "WM_USER", MB_OK);

      case WM_DESTROY:
         if (hWnd == hMainWnd)

         return DefWindowProc(hWnd, iMessage, wParam, lParam);

      return 0L;

Additional query words:

Keywords : kb16bitonly kbSDKPlatform kbGrpDSUser kbWndw kbWndwMsg
Issue type : kbinfo
Technology : kbAudDeveloper kbWin3xSearch kbSDKSearch kbWinSDKSearch kbWinSDK300 kbWinSDK310

Last Reviewed: November 6, 1999
© 2001 Microsoft Corporation. All rights reserved. Terms of Use.