Loading a DLL implicitly

When an application uses functions or variables defined in a DLL, the compiled code loads the DLL. This implicit load is transparent to the application. The load establishes the required references to functions and variables in the DLL by updating the control information contained in function and variable descriptors.

If the DLL contains static classes, constructors are run when the DLL is loaded. This loading may occur before main(); in this case, the corresponding destructors are run once when main() returns.

To implicitly load a DLL, do one of the following:

  1. Statically initialize a variable pointer to the address of an exported DLL variable.
  2. Reference a function pointer that points to an exported function.
  3. Call an exported function.
  4. Reference (use, modify, or take the address of) an exported variable.
  5. Call through a function pointer that points to an exported function.

In the first situation, the DLL is loaded before main() is invoked, and if the DLL contains C++ code, constructors are run before main() is invoked. In the other situations, the DLL loading may be delayed until the time of the implicit call, although optimization may move this load earlier.

If the DLL application references (imports) an exported DLL variable, that DLL may be implicitly loaded before that DLL application is invoked (not necessarily before main() is invoked). With XPLINK, the DLL will always be implicitly loaded before invoking the DLL application that references (imports) a DLL variable or takes the address of a DLL function.

Note: When a DLL is loaded, its writable static is initialized. If the DLL load module contains C++ code, static constructors are run once at initial load time, and static destructors are run once at program termination. Static destructors are run in the reverse order of the static constructors.