Dynamic Link Libraries Inside-Out - Points of Interest
(Page 8 of 9 )
Here are a few points that any developer would need to keep in mind:
• When the system starts a program that uses load-time dynamic linking, it uses the information in the file to locate the names of the required DLL(s).
Following search sequence will be used to locate DLLs:
- The directory from which the application loaded.
- The current directory.
- The Windows system directory. Use the GetSystemDirectory function to get the path of this directory.
- The 16-bit Windows system directory. There is no function that obtains the path of this directory, but it is searched. The name of this directory is System.
- The Windows directory. Use the GetWindowsDirectory function to get the path of this directory.
- The directories that are listed in the PATH environment variable.
• The DLL is mapped into the virtual address space of the process during its initialization and is loaded into physical memory only when needed.
• DLLs are loaded using LoadLibrary or LoadLibraryEx APIs.
• Standard search sequence (mentioned above) will be used to locate DLLs unless you decide to redirect the DLL(Win2K only).
• Two DLLs that have the same base file name and extension but are found in different directories are not considered to be the same DLL.
• The system calls the entry-point function in the context of the thread that called LoadLibrary or LoadLibraryEx.
• If the system cannot find the DLL or if the entry-point function returns FALSE, LoadLibrary or LoadLibraryEx returns NULL.
• The process can use GetProcAddress to get the address of an exported function in the DLL using a DLL module handle returned by either LoadLibrary, LoadLibraryEx, or GetModuleHandle
• When the DLL module is no longer needed, the process can call FreeLibrary or FreeLibraryAndExitThread.
• Run-time dynamic linking enables the process to continue running even if a DLL is not available.
• Run-time dynamic linking can cause problems if the DLL uses the DllMain function to perform initialization for each thread of a process, because the entry-point is not called for threads that existed before LoadLibrary or LoadLibraryEx is called.
• DLLs can contain global data or local data.
• The default scope of DLL variables is the same as that of variables declared in the application.
• When a DLL allocates memory using any of the memory allocation functions (GlobalAlloc, LocalAlloc, HeapAlloc, and VirtualAlloc), the memory is allocated in the virtual address space of the calling process and is accessible only to the threads of that process.
• The thread local storage (TLS) functions enable a DLL to allocate an index for storing and retrieving a different value for each thread of a multithreaded process.
• Warning: VC++ compiler supports a syntax that enables you to declare thread-local variables: _declspec(thread). If you use this syntax in a DLL, you will not be able to load the DLL explicitly using LoadLibrary or LoadLibraryEx. If your DLL will be loaded explicitly, you must use the thread local storage functions instead of _declspec(thread).
• GetModuleFileName Retrieves the full path and file name for the file containing the specified module.
• GetModuleHandle Retrieves a module handle for the specified module.
• GetModuleHandleEx Retrieves a module handle for the specified module.
• GetProcAddress Retrieves the address of an exported function or variable from the specified DLL.
• LoadLibrary Maps the specified executable module into the address space of the calling process.
• LoadLibraryEx Maps the specified executable module into the address space of the calling process.
Next: Troubleshooting and Common Errors >>
More Code Examples Articles
More By Digvijay Chauhan