Disable automatic DLL loading in C ++

My script looks like this: my application depends on a specific DLL (I am using lib while linking). However, when my application is running, I want to explicitly load this DLL using LoadLibrary. However, by default, when the code reaches the area that requires this DLL, the environment automatically searches for it and then loads it. I want to disable this behavior, and for me it doesn't matter if the application has reached the point where it wants to execute the code belonging to that DLL, I prefer it to crash automatically rather than load it automatically (so the DLL will only load because I explicitly called LoadLibrary).
In the meantime, I am using the latency ability (so the load trigger will only happen when the DLL needs to be loaded). However, I would prefer that the application just crash if the DLL hasn't loaded yet.

Is anyone perhaps familiar with a way to achieve this?

+2


source to share


6 answers


If you want to use LoadLibrary

, don't link the app with the import library. The PE format does not support unresolved external elements, so you use either headers, and dllimport

, or LoadLibrary

, GetProcAddress

and function pointers.



+6


source


(I use it during communication)



If you want to load it manually using LoadLibrary

and GetProcAddress

, you do not have to pass its *.lib

file to your linker.

+4


source


You can prevent automatic loading by not linking to the DLL import library (.lib file). Then you can use LoadLibrary to manually load the DLL when you need it.

I posted a blog post on how to do this here .

+2


source


You can connect a delay mechanism. Install __pfnDliNotifyHook2

in the function you provide, in which case just terminate your application.

+1


source


The load delay functionality will not load the dll until its first call to the function and not the scope. If you have global initializers that call this DLL, this might be why you think its scope is based. My company uses the technique of calling LoadLibrary before using it without issue. I suggest going deeper into your problem.

0


source


Is this what you need: http://msdn.microsoft.com/en-us/library/151kt790(VS.80).aspx ?

I mean, you can provide you a custom function to load the DLL and drop your application from there. It is detailed in the link provided.

0


source







All Articles