- 1 year ago
Delayed loading of exported functions behaviour
When dynamically loading exported functions from a DLL using the LoadLibrary
and GetProcAddress
functions in Windows, the behavior of delayed loading depends on how the DLL is designed and the loading mechanism used.
Implicit Delayed Loading: Some DLLs are designed with implicit delayed loading, which means the DLL itself handles the delayed loading of its exported functions. In this case, when you call
LoadLibrary
to load the DLL, the DLL's initialization routine may defer the actual loading of its functions until they are explicitly called withGetProcAddress
. This allows for improved performance by loading only the necessary functions when needed.Explicit Delayed Loading: In some cases, you may want to explicitly specify that you want to load functions from a DLL using delayed loading. This can be achieved by using the
/DELAYLOAD
linker option during compilation or by using theDelayLoad.dll
mechanism provided by the Visual C++ runtime. Explicit delayed loading allows you to specify specific functions that should be loaded on demand rather than at the time of DLL initialization.
Regardless of the type of delayed loading, the key idea is that the DLL's exported functions are not loaded into memory immediately upon loading the DLL. They are loaded and resolved only when you explicitly call GetProcAddress
to retrieve the function's address.
Delayed loading can be beneficial in scenarios where you want to avoid the overhead of loading all functions from a DLL upfront, especially when the DLL contains a large number of functions or when you want to load functions selectively based on application logic.
It's important to note that delayed loading of DLL functions requires proper error handling to handle cases where the functions fail to load or are not found. Make sure to check the return values of LoadLibrary
and GetProcAddress
to handle any potential errors gracefully.
Additionally, keep in mind that delayed loading of DLL functions is specific to the Windows platform and may not be available or behave the same way on other operating systems.