LoadLibrary error

| 4 Comments

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;
SayWhat.png
and then, when someone presses the button, you get NULL returned... How crap is that.

4 Comments

You get NULL returned from GetLastError()?

No, you get all the right error information, LoadLibrary() returns 0, GetLastError() returns 193 (not a valid win32 application) so you could deal with it however you wanted except for the fact that there's a message box popped up which forces user intervention before you get the errors...

You'll want to use SetErrorMode to suppress that.

http://msdn.com/library/en-us/debug/base/seterrormode.asp


Setting the SEM_FAILCRITICALERRORS flag suppresses that particular box, but there are other, similar ones you might want to get rid of as well.

Hope that helps, and thanks for an excellent blog.

Kim

Thanks for that :)

Leave a comment