12.3 Client Access to a Library

An NLM that uses the resources (symbols) of a library is a client of the library. Clients can access a library either statically or dynamically.

Statically Linked Libraries. Traditionally, clients have linked statically to the library by linking with an import file which lists all the symbols the client consumes from the library. This creates an auto-load dependency and causes the library to be auto-loaded before the client NLM. Libraries facilitate this linking process by providing the import file for the clients. The import file usually has the same name as the library file, except uses an .imp extension. For example:

  libc.nlm
  libc.imp
  

Dynamically Linked Libraries. Clients can also link dynamically to a library. This method is based on the POSIX interfaces from the dlfcn.h file, which includes the dlopen, dlsym, and dlclose functions. Clients dynamically link to the library by calling dlopen with the name of the library and use dlsym to specify a specific function. This is useful for applications and other consumers that do not want to produce a detailed list of their functional dependency at link time.

This method is very inefficient because accessing entry points via function pointers in C results in loss of performance. However, it is widely used, is often absolutely necessary, and can be used with DllMain start-up code. The window.h functions, LoadLibrary and FreeLibrary, can also be used with DllMain, and they essentially perform the same tasks as dlopen and dlclose.

If you code a DllMain function, you can also use the following DllMain messages to dynamically load and unload a library:

These messages perform the same tasks as dlopen/dlclose and LoadLibrary/FreeLibrary functions. LibC supports two other DllMain messages (DLL_THREAD_ATTACH and DLL_THREAD_DETACH) for applications that use standard functions (such as pthread interfaces) to create and terminate threads.