Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does LoadLibrary return NULL or an error code < 32 on failure?

Tags:

winapi

The MSDN documentation states

If the function succeeds, the return value is a handle to the module.

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

while Microsoft support has a list of return values less than 32 that indicate an error

The API function LoadLibrary loads a DLL and returns either a handle or an error code. If the return value is less than 32, it indicates one of the errors listed below. A return value greater than or equal to 32 indicates success and you should call the FreeLibrary function to unload the library.

The second article was last reviewed in 2003 and explicitly applies to Visual Basic 4.0.

What is correct? LoadLibrary returning != 0 or >= 32 for success? Or are both correct and I am missing some hint for a Version difference or a VB specific Windows API wrapper that differs from the C style interface?

like image 939
die_hoernse Avatar asked Aug 31 '26 17:08

die_hoernse


1 Answers

  • In 32 and 64 bit Windows, LoadLibrary returns NULL on failure.
  • In 16 bit Windows LoadLibrary returns a value less than 32 to indicate failure.

KB142814 clearly dates from the 16 bit Windows days, and if you look closely you will see a kb16bitonly keyword. I think it is safe to assume that you are not developing for 16 bit Windows anymore!

Some relevant articles from Raymond Chen on this matter:

  • What is the difference between HINSTANCE and HMODULE?
  • What can I do with the HINSTANCE returned by the ShellExecute function?

The modern day documentation for LoadLibrary is here: https://msdn.microsoft.com/en-us/library/windows/desktop/ms684175.aspx, that is the first link in your question. It says:

Return value

If the function succeeds, the return value is a handle to the module.

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

like image 79
David Heffernan Avatar answered Sep 03 '26 14:09

David Heffernan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!