Differences Between PostAppMessage and PostMessage FuncsLast reviewed: July 22, 1997Article ID: Q35774 |
3.00 3.10
WINDOWS
kbprg
The information in this article applies to:
SUMMARYThe following information describes the differences between the PostAppMessage and PostMessage functions, and the circumstances under which each should be used.
MORE INFORMATIONIn 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:
//******************************************************************* // 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) { wndclass.style = 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, CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL); ShowWindow(hMainWnd, nCmdShow); UpdateWindow(hMainWnd); while (GetMessage(&msg, NULL, 0, 0)) { if (msg.hwnd == NULL) // Beep, then pass the message to { // the window MessageBeep(0); msg.hwnd = hMainWnd; } TranslateMessage(&msg); DispatchMessage(&msg); } 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); break; case WM_RBUTTONDOWN: PostAppMessage(GetWindowTask(hWnd), WM_USER+0x1000, 0, 0L); break; case WM_USER+0x1000: MessageBox(NULL, "Message Received", "WM_USER", MB_OK); break; case WM_DESTROY: if (hWnd == hMainWnd) PostQuitMessage(0); break; default: return DefWindowProc(hWnd, iMessage, wParam, lParam); } return 0L;}
|
Additional reference words: 3.00 3.10
© 1998 Microsoft Corporation. All rights reserved. Terms of Use. |