Delay Loading DLLs Delay Loading DLLs windows windows

Delay Loading DLLs


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.


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. Thisrelieves you of the need to use the Windows SDK functions LoadLibraryand GetProcAddress to implement DLL delayed loading.

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

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

An application can delay load a DLL using the /DELAYLOAD (Delay LoadImport) linker option with a helper function (default implementationprovided by Visual C++). The helper function will load the DLL at runtime 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'sexecution.

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


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);