Delay Loading DLLs
Asked Answered
K

3

10

I am in desperate need of help, I need to manage an application dependency in Visual Studio. The application links to a DLL only on a specific version of windows, lets say Windows 7. and on other environments, the DLL should not be loaded. How will I be able to achieve that using DLL Delay Loading as this topic is completely new to me and there isn't any good references online for this particular matter.

Regards

Kathiekathleen answered 7/9, 2009 at 9:17 Comment(1)
Are you asking how delay-loading works, or how you can apply it?Montreal
M
11

Your project can specify that a dll it depends upon should but be loaded when needed, by specifying it in the Linker/Input/Delay Loaded DLLs field. This setting can be different for different build configurations.

Montreal answered 7/9, 2009 at 9:26 Comment(4)
Thx for the reply man but can you giva an example or code snippet if its applicable :)Kathiekathleen
That's the point of delay loading: it's a configuration thing, not a code thing.Montreal
The wording appears wrong: When you say "The DLL you're depending upon should have been linked with the delay-load capacity", you're seem to be referring to the creation of that DLL from its constituent .obj's. The /DELAY flag is actually applied to the module that does the importing, not the imported module. That's also the reason that you can delay-load Windows 2000 DLLs, which would have been built without this support.Fiendish
@MSalters: You're right: the first option is for the dll to support unloading. Corrected for that, thanks.Montreal
S
11

MSDN has a pretty good description here.

Basically what you are doing is setting the DLL in question to be in the delay load section. It will then not load that DLL until you make a call to a function that is in that DLL.

From the above link:

The Visual C++ linker now supports the delayed loading of DLLs. This relieves you of the need to use the Windows SDK functions LoadLibrary and GetProcAddress to implement DLL delayed loading.

Before Visual C++ 6.0, the only way to load a DLL at run time was by using LoadLibrary and GetProcAddress; the operating system would load the DLL when the executable or DLL using it was loaded.

Beginning with Visual C++ 6.0, when statically linking with a DLL, the linker provides options to delay load the DLL until the program calls a function in that DLL.

An application can delay load a DLL using the /DELAYLOAD (Delay Load Import) linker option with a helper function (default implementation provided by Visual C++). The helper function will load the DLL at run time by calling LoadLibrary and GetProcAddress for you.

You should consider delay loading a DLL if:

Your program may not call a function in the DLL.

A function in the DLL may not get called until late in your program's execution.

The delayed loading of a DLL can be specified during the build of either a .EXE or .DLL project. A .DLL project that delays the loading of one or more DLLs should not itself call a delay-loaded entry point in Dllmain.

Stevenson answered 7/9, 2009 at 9:31 Comment(0)
B
2

Instead of using delay loading, have you considered using dynamic loading with LoadLibrary and GetProcAddress? This is likely to be simpler to use.

typedef void (WINAPI *PGNSI)(LPSYSTEM_INFO);

// Call GetNativeSystemInfo if supported or GetSystemInfo otherwise.

PGNSI pGNSI;
SYSTEM_INFO si;

ZeroMemory(&si, sizeof(SYSTEM_INFO));

pGNSI = (PGNSI) GetProcAddress(
   GetModuleHandle(TEXT("kernel32.dll")), 
   "GetNativeSystemInfo");
if(NULL != pGNSI)
   pGNSI(&si);
else GetSystemInfo(&si);
Brochette answered 7/9, 2009 at 9:27 Comment(5)
How is it simpler to write code that can be automatically generated by the linker?Montreal
its fine if you only have 1 function you wish to call ... otherwise its a plain bad choice, imo.Stevenson
-1 - the support for delay loading in the linker is specifically aimed at preventing you having to deal LoadLibrary() and GetProcAddress()!Craggie
In the case of this question, (we assume) he knows that the DLL will be available on Windows 7 so he can detect the OS version before invoking a function call to the delayloaded DLL. However, it is sometimes the case that you can't know if a DLL is available on a system or what version (and thus what exported functions) are available. In those cases, using run-time dynamic linking is the appropriate albeit tedious solution. If you don't have some manner of detecting the presence of the DLL and required exported functions with 100% certainty, then delay loading may just be delay crashing.Esposito
I second xtofl and Bids. So much more work to use runtime loading.Ettaettari

© 2022 - 2024 — McMap. All rights reserved.