views:

148

answers:

2

The message loop generated by class wizard often looks like

while( GetMessage() )
{
    if( !TranslateAccelerator() )
    {
        TranslateMessage();
        DispatchMessage();
    }
}

Whereas TranslateAccelerator documentation says:
If the function succeeds, the return value is nonzero.
If the function fails, the return value is zero. To get extended error information, call GetLastError.

In my tests, when the only reason why the TranslateAccelerator failed was no accelerator was found for this particular message, GetLastError returned 0==ERROR_SUCCESS.

I'm just curious, are there any realistic conditions when TranslateAccelerator fails because of some other reason, and the message should not be translated & dispatched?

Thanks in advance!

+1  A: 

Yes. The message may pass through some other message filtering API such as IsDialogMessage().

From MSDN:

Because the IsDialogMessage function performs all necessary translating and dispatching of messages, a message processed by IsDialogMessage must not be passed to the TranslateMessage or DispatchMessage function.

gwell
+1  A: 

Whatever the various reasons that TranslateMessage might fail... well its not at all implied (and certainly not explicitly stated) that the message should not still be delivered to DispatchMessage.

The real problem with the code sample provided is, that in addition to 0, GetMessage can return an error code of -1. -1 means you should not process the message (as there is no message - the MSG struct is most likely either left uninitialized or probably has the previous message's data). Unless you can do something to 'fix' the broken condition, if GetMessage returns -1 once it will probably return -1 on subsequent calls - the (probable) correct strategy is to exit the message loop.

Also, an uninitialized MSG struct could certainly be a conceivable cause for TranslateMessage and/or DispatchMessage to fail.

Chris Becke