LoadLibrary

This function maps the specified DLL file into the address space of the calling process.

HINSTANCE LoadLibrary( 
  LPCTSTR lpLibFileName
); 

Parameters

  • lpLibFileName
    [in] Pointer to a null-terminated string that names the DLL file. The name specified is the file name of the module and is not related to the name stored in the library module itself, as specified by the LIBRARY keyword in the module-definition (.def) file.

    If the string specifies a path but the file does not exist in the specified directory, the function fails. When specifying a path, be sure to use backslashes (\), not forward slashes (/).

    If the string does not specify a path, the function uses a standard search strategy to find the file. See the Remarks for more information.

Return Values

A handle to the module indicates success. NULL indicates failure. To get extended error information, call GetLastError.

Remarks

LoadLibrary can be used to map a DLL module and return a handle that can be used in GetProcAddress to get the address of a DLL function. You need to use FreeLibrary on the handle later. LoadLibrary can also be used to map other executable modules. For example, the function can specify an .exe file to get a handle that can be used in FindResource or LoadResource. Do not use LoadLibrary to run a .exe file, use the CreateProcess function.

If the module is a DLL not already mapped for the calling process, the system calls the DLL's DllMain function with the DLL_PROCESS_ATTACH value. In Windows CE, a DLL is loaded once, but then it is mapped into each processes address space when a process implicitly or explicitly loads the library with the LoadLibrary function. When Windows CE loads a DLL, all path information is ignored when determining if the DLL is already loaded. This means that a DLL with the same name but a different path can only be loaded once. In addition, a module ending with the extension .cpl is treated as if the extension if .dll.

It is not safe to call LoadLibrary from DllMain.

Module handles are not global or inheritable. A call to LoadLibrary by one process does not produce a handle that another process can use — for example, in calling GetProcAddress. The other process must make its own call to LoadLibrary for the module before calling GetProcAddress.

Two different modules cannot have the same file name, given that the extensions are different. These effectively have the same module name. For example, if LoadLibrary is made on Sample.cpl, the operating system will not load Sample.cpl, but instead will again load Sample.dll. A similar limitation exists for modules with the same name but residing in different directories. For example, if LoadLibrary is called on \\Windows\Sample.dll, and then LoadLibrary is called on \\MyDir\Sample.dll, \\Windows\Sample.dll will simply be reloaded.

If no file name extension is specified in the lpLibFileName parameter, the default library extension .dll is appended. However, the file name string can include a trailing point character (.) to indicate that the module name has no extension.

A search path to the executable module cannot be specified. Unless the full path to the module is specified, Windows CE .NET searches the following path for the module:

  • The absolute path specified by the lpLibFileName parameter
  • The .exe launch directory
  • The Windows directory
  • ROM DLL files
  • OEM-specified search path, which also now contains the Windows CE Debug Shell path when the Shell.exe is included in the image.

The following registry subkey specifies a search path to use with LoadLibrary and CreateProcess:

HKEY_LOCAL_MACHINE\Loader
    SystemPath=multi_sz:\\path1\\
                        \\path2\\

For efficiency, the path is searched before CESH but after the ROM and built in file systems. Note also that the path is only searched if path of the file being looked for is not explicitly specified.

The total length of the SystemPath value cannot exceed 260 characters, or the path will be completely ignored. A change to the SystemPath key does not take effect until a Windows CE–based device is reset.

Requirements

OS Versions: Windows CE 1.0 and later.
Header: Winbase.h.
Link Library: Coredll.lib.

See Also

FindResource | LoadResource | CreateProcess | DllMain | FreeLibrary | GetProcAddress

Last updated on Wednesday, April 13, 2005

© 2005 Microsoft Corporation. All rights reserved.