Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Attempting to load a DLL on Windows using LoadLibrary when a dependent DLL is missing

Tags:

c

windows

I have an application that uses LoadLibrary on Windows to dynamically load plugins. However some of the plugins have other dependent DLLs, such as database client DLLs. When you attempt to load such a DLL and one of the dependent DLLs doesn't exist you get a nasty Windows dialog:

"The program can't start because xxx.ddl is missing from your computer. Try reinstalling the program to fix this problem."

Is there any easy way to avoid this dialog? I was hoping one could use LoadLibraryEx and pass a flag that says "don't give me that annoying dialog", but it doesn't seem like it.

What I'd like is for the application to handle that error, rather than Windows handling it for me, especially as the text of the message is incorrect (the problem isn't that the program can't start, the program is running quite happily, it just can't load this plugin).

What I'd like to avoid is having to rewrite the plugins that have these external dependencies to make them themselves do a dynamic load of any dependent modules and then query for any entry points.

Anyway, any suggestions would be gratefully received.

like image 295
Tom Quarendon Avatar asked Nov 24 '10 10:11

Tom Quarendon


People also ask

What DLL is LoadLibrary?

Kernel32. dll is loaded into every Windows process, and within it is a useful function called LoadLibrary .

What is LoadLibrary?

LoadLibrary can be used to load a library module into the address space of the process and return a handle that can be used in GetProcAddress to get the address of a DLL function. LoadLibrary can also be used to load other executable modules.

Why would a DLL fail to load?

If a DLL fails to load, there can be numerous reasons for the error, listed below are the most common. The DLL or referenced DLL is not in a directory specified in the path. The DLL references another DLL that is not present. Hard disk error has corrupted or damaged a DLL file.

What is DLL preloading?

If an attacker gains control of one of the directories, they can force the application to load a malicious copy of the DLL instead of the DLL that it was expecting. These attacks are known as “DLL preloading attacks” and are common to all operating systems that support dynamically loading shared DLL libraries.


2 Answers

Use SetErrorMode(). Use it with SEM_NOOPENFILEERRORBOX | SEM_FAILCRITICALERRORS before you load the DLL and with 0 right after.

like image 92
kichik Avatar answered Oct 19 '22 08:10

kichik


From MSDN:

To enable or disable error messages displayed by the loader during DLL loads, use the SetErrorMode function.

Link here

like image 28
Aamir Avatar answered Oct 19 '22 09:10

Aamir