How can you tell if SendMessage() was successful (message was delivered)?

后端 未结 2 1817
别跟我提以往
别跟我提以往 2021-01-25 00:26

is it possible to determine if a SendMessage() call was successful and delivered my message to the target window?

The SendMessage() description in the Windows API seems

2条回答
  •  我在风中等你
    2021-01-25 00:41

    if SendMessage fail, it set last win32 error code, which we can get back via call GetLastError(). and returned error code, in case fail, never will be 0 (NOERROR) despite this is clear not documented. but how detect that SendMessage is fail ? here we can not base on return value. but we can SetLastError(NOERROR) before call SendMessage and check GetLastError() after:

    • if SendMessage fail - value returned by GetLastError() will be not 0. (most common in this case ERROR_INVALID_WINDOW_HANDLE or ERROR_ACCESS_DENIED).
    • if we still got 0 as last error - this mean that message was delivered with no error.
    • however, possible case, when during SendMessage call, some window procedure in current thread will be called and code inside window procedure, set last error to non 0 value. so we get finally not 0 last error, but not because SendMessage fail. distinguish between these two cases very problematic

    -

        SetLastError(NOERROR);
        LRESULT lr = SendMessageW(hwnd, *, *, *);
        ULONG dwErrorCode = GetLastError();
        if (dwErrorCode == NOERROR)
        {
            DbgPrint("message was delivered (%p)\n", lr);
        }
        else
        {
            DbgPrint("fail with %u error\n", dwErrorCode);
        }
    

提交回复
热议问题