tmighty tmighty - 1 month ago 11 Question

PostMessage WM_CLOSE returns 1

I have the following function:

Public Function CloseWindow(ByVal uHandle As Int32) As Boolean

Dim iRet As Integer = PostMessage(uHandle, WM_CLOSE, 0, 0)

Return CBool(iRet = 0)

End Function

The function in run-time is like this:

Dim iRet As Integer = PostMessage(uHandle, WM_CLOSE, 0, 0)

If (iRet <> 0) Then
Dim iLastErr As Integer = Err.LastDllError()
'This always returns 0, so I don't know what to make up of it
End If

In some cases, iRet returns 1.

Can anbody tell me what that might mean?


WM_CLOSE is a sent message, not a posted message. A posted message cannot return a value to the poster.

The return value of PostMessage() is a BOOL that simply indicates whether the message was successfully added to the window's message queue, NOT whether the message was actually processed.

If you need to handle the LRESULT value that WM_CLOSE returns to indicate whether the close was accepted or rejected, you need to use SendMessage() (or related function) instead of PostMessage().

To actually close the window, you can use DestroyWindow() (unless the window in question passes the WM_CLOSE message to DefWindowProc(), in which case it will call DestroyWindow() for you).