LoadLibrary error

I need to dynamically load a dll, grab some function pointers and do stuff; simple stuff, done it 100s of times before. I decide that this time I’ll wrap the HMODULE that I’ll need in a class to make sure I can never forget to call FreeLibrary() on it - RAII is your friend. It’s Sunday, I’m feeling righteous so I’m doing it in a TDD style. I write some tests, load a dll, load a bad dll to check error handling, load a dll that doesn’t exist…

So, what do you expect happens when you call LoadLibrary() on a file that isnt a valid dll or exe image?

Well, the docs say this:

If the function fails, the return value is NULL. To get extended error information, call GetLastError.

What they don’t tell you is that you actually get this;


and then, when someone presses the button, you get NULL returned… How crap is that.