views:

323

answers:

6

My scenario is as follows: my application depends on a certain DLL (I use it's lib during linkage). However, when my application executes, I want to explicitly load that DLL using LoadLibrary. However, by default, when the code reaches a scope where that DLL is needed, the environment automatically look it up, and then loads it. I want to disable this behavior, and for all I care, if the application reached a point where it wants to execute code that belongs to that DLL, I prefer that it will crash instead of loading it automatically (So the DLL will be loaded only because I explicitly called LoadLibrary).
In the meanwhile, I'm using the delay-load ability (so the load trigger will occur only when the DLL actually needs to be loaded). However, I would prefer that the application will just crash if the DLL wasn't loaded yet.

Perhaps anyonehere is familiar with a way to achieve this?

+4  A: 

(I use it's lib during linkage)

If you want to load it manually using LoadLibrary and GetProcAddress then you shouldn't pass its *.lib file to your linker.

ChrisW
+5  A: 

If you want to use LoadLibrary, then don't link application with the import library. PE format doesn't support unresolved externals, so you either use headers and dllimport, or LoadLibrary, GetProcAddress and pointers to functions.

PiotrLegnica
+2  A: 

You can prevent automatic loading by not linking against the DLL's import library (the .lib file). You can then use LoadLibrary to manually load the DLL whenever you need it.

I posted a blog entry about doing this sort of thing here.

Ferruccio
A: 

The delayload functionality won't load a dll until its first function call, not scope. If you have global initializers that call into that dll, then that maybe be why you think its scope based. My company uses the technique of calling LoadLibrary before use without problems. I suggest digging further into your problem.

Joel Lucsy
I'm aware of that, that's why I've used delayLoad on the first place..
A: 

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

I mean, you can provide you own function to load DLL, and crash your aplication from there. It is detailed in the link provided.

queen3
A: 

You can hook the delayload mechanism. Set __pfnDliNotifyHook2 to a function you provide, and in that hook simply terminate your application.

MSalters