LoadKeyboardLayout is a 00bada55 puppy dog tail wagger of a function

by Michael S. Kaplan, published on 2008/12/10 10:01 -05:00, original URI: http://blogs.msdn.com/b/michkap/archive/2008/12/10/9189637.aspx

There are many functions in the Win32 API that really amount to little more than requests.

The caller is asking for something, perhaps passing parameters to give specifics on what the caller wants.

And the caller will usually get something back.

It will often match what was requested, to some degree... but not always.

Sometimes, it can't -- and rather than fail completely (since almost no one checks return values) it will return something to let the code keep working.

You may recall situations with fonts that I have mentioned in the past that are impacted by this phenomenon.

And there was a question that came up the other day:

The MSDN description regarding return value of LoadKeyboardLayout() says:

Return Value
If the function succeeds, the return value is the input locale identifier to the locale matched with the requested name. If no matching locale is available, the return value is NULL. To get extended error information, call GetLastError.

However, when I supply a random string text (example: “sdfdfsfsfsdf”) for the input locale identifier, pwszKLID, the return value is a non-NULL (0x04090409, the en-US keyboard). The GetLastError() call returns 0, that everything is ok. This behavior is completely different than what the MSDN article says about the return value.

Can anyone explain this behavior? What should I expect when a non-existing (no matching) locale is supplied?

And now we have a ball game.

I verified the same behavior passing 00bada55 and I thought that was a more fun test, myself. :-)

As it turns out, there is some "failsafe" code that will, when everything goes wrong, automatically return the 0409 layout.

Failure of even that code would have to be pretty extreme, but it is in theory possible.

So the documentation isn't wrong. Well, not really wrong. Though for 99.9% of cases it might be effectively wrong.

Besides, what if you passed something illegal as the parameter, rather than something invalid? I'm willing to bet that if one really tried one could make even the most eager to please function fail. Even the puppy dog like nature of LoadKeyboardLayout is not foolproof.

Clearly the other NULL case -- invalid parameter -- is also not being mentioned.

So make sure you always check the return value. And don't rely on the undocumented piece pf this equation, that failsafe code. If it's not documented, then...


This blog brought to you by a (U+0061, aka LATIN SMALL LETTER A)

no comments

referenced by

2011/06/10 What do fonts and keyboards have in common? Not "crap bag" compatibility!

go to newer or older post, or back to index or month or day