• Advertisement

Archived

This topic is now archived and is closed to further replies.

LoadLibrary()

This topic is 5772 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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...

Share this post


Link to post
Share on other sites
Advertisement
Guest Anonymous Poster
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

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

  • Advertisement