Closed4

GetMessage戻り値のお話

yohhoyyohhoy

GetMessage function (winuser.h)

BOOL GetMessage(
  [out]          LPMSG lpMsg,
  [in, optional] HWND  hWnd,
  [in]           UINT  wMsgFilterMin,
  [in]           UINT  wMsgFilterMax
);

Return value
Type: BOOL

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 there is an error, the return value is -1. For example, the function fails if hWnd is an invalid window handle or lpMsg is an invalid pointer. [...]

yohhoyyohhoy

The Old New Things, Raymond Chen, When will GetMessage return -1?

But don’t worry, the standard message pump is safe. If your parameters are exactly

  • a valid pointer to a valid MSG structure,
  • a null window handle,
  • no starting message range filter,
  • no ending message range filter,

then Get­Message will not fail with -1.

yohhoyyohhoy

When Does GetMessage Return -1? (2010-10-04)

Knowning what we know now, we can see that the return value section of the GetMessage doc page is correct, if only partially. Out of the 5 failure cases over which a user has control, only an invalid, non-null HWND actually returns -1. An invalid message pointer never does, returning 0 on older NT and 1 on 9x Windows, but worse, causing an access violation on new when given a completely wild pointer.

このスクラップは2021/12/24にクローズされました