LoadLibrary()

Started by
8 comments, last by Origin 21 years, 9 months ago
I just want to know what happens if I load a DLL using LoadLibrary() which statically links to - say - the DirectX libraries and those aren''t installed. LoadLibrary() will almost certainly fail. But does the user get a message box saying "dx8...dll not found"? My idea is to have a plugin system and load each plugin DLL on startup to call the functions GetName(), GetType() and GetDescription(). Then, the libraries will be unloaded again since the necessary information is gathered. The user may now select the appropriate plugins. Of course, it would be a problem if each loading of an unsupported plugin resulted in an error message on the screen. In this case, static linking would have to be forbidden for the plugin DLLs...
Advertisement
Well, LoadLibrary loads an instance of a DLL and it returns a Handle to that instance. If it fails, it returns a NULL handle. You would simply add this snippet of code after your LoadLibrary call:

HINSTANCE lib;lib = LoadLibrary("Hello.dll";if(lib == NULL){AfxMessageBox("Error Loading Hello.DLL",MB_OK,NULL);return -1;} 



LoadLibrary checks the currnet directory and the the windows and system directories for the libraries.

-James
Did I describe the problem so inaccurate?

Well, I now how to use the LoadLibrary() function.

My question was rather what happened if I call it to load a DLL which in turn statically links to other DLLs which aren''t present on the current system (meaning that the import address table cannot be intialized by the system).

Anybody?
LoadLibrary will return NULL in that case, it is up to you to display any messagebox you want. You use the GetLastError-function to determine what went wrong.
Arguing on the internet is like running in the Special Olympics: Even if you win, you're still retarded.[How To Ask Questions|STL Programmer's Guide|Bjarne FAQ|C++ FAQ Lite|C++ Reference|MSDN]
quote:Original post by Origin
My question was rather what happened if I call it to load a DLL which in turn statically links to other DLLs which aren''t present on the current system (meaning that the import address table cannot be intialized by the system).

You won''t get to the code that executes LoadLibrary. If a statically linked import cannot be resolved at runtime, your program will not run at all. Always. You will get the message "dll not found" and your program will not be executed.

For a plugin system, LoadLibrary+GetProcAddress probably provide the best solution. If you want to reduce the number of GetProcAddress calls, use a class with member functions. Alternative way will be to make your plugins COM dlls and use CoCreateIntance to load registered plugins.

There''s also delay-loading for the cases when you want to statically link to a DLL that may or may not be present on target system. This approach won''t work that nice for plugins, though.
---visit #directxdev on afternet <- not just for directx, despite the name
quote:Original post by IndirectX
You won''t get to the code that executes LoadLibrary. If a statically linked import cannot be resolved at runtime, your program will not run at all. Always. You will get the
message "dll not found" and your program will not be executed.

Why wouldn''t LoadLibrary() be executed? The plugin loader itself will of course not statically link to any DLL, otherwise the call to LoadLibrary() would be quite useless.

quote:Original post by IndirectX
For a plugin system, LoadLibrary+GetProcAddress probably provide the best solution. If you want to reduce the number of GetProcAddress calls, use a class with member functions. Alternative way will be to make your plugins COM dlls and use CoCreateIntance to load registered plugins.

Yeah, sure. Doesn''t answer my question, though.

quote:Original post by IndirectX
There''s also delay-loading for the cases when you want to statically link to a DLL that may or may not be present on target system. This approach won''t work that nice for plugins, though.

Right.

quote:Original post by dalleboy
LoadLibrary will return NULL in that case, it is up to you to display any messagebox you want. You use the GetLastError-function to determine what went wrong.

Well, that''s actually an answer to my question. Thanks. Are you sure that the system won''t display an own message box?
I think you''re looking for this: SetErrorMode(SEM_FAILCRITICALERRORS);
Sorry, I misread your post.
---visit #directxdev on afternet <- not just for directx, despite the name
quote:Original post by Origin
Well, that''s actually an answer to my question. Thanks. Are you sure that the system won''t display an own message box?

Yes, I''m sure if the DLL you''re trying to load are only using static linked libraries.

Though, if it in turn uses LoadLibrary (or some other way) to dynamic load its libraries lateron, it would depend on the implementation of the DLL. It would be perfectly possible for it to display a MessageBox if its call to LoadLibrary failes.

The system will not display a MessageBox directly. SetErrorMode (as developer stated) could be useful.
Arguing on the internet is like running in the Special Olympics: Even if you win, you're still retarded.[How To Ask Questions|STL Programmer's Guide|Bjarne FAQ|C++ FAQ Lite|C++ Reference|MSDN]
Thanks a lot.

That should suffice.

This topic is closed to new replies.

Advertisement