2024年1月16日发(作者:)

LoadLibrary Function

Maps the specified executable module into the address space of the calling process.(映射一个指定的可执行模块到调用进程的地址空间中)

For additional load options, use the LoadLibraryEx function.(使用LoadLibraryEx获得附加的加载选项)

Syntax

HMODULE WINAPI LoadLibrary(

__in LPCTSTR lpFileName

);

Parameters

lpFileName

The name of the executable module (either a .dll or .exe file)(可执行模块名字,.dll文件或者.exe文件). 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. (指定的名字是模块的文件名,和在模块定义文件中LIBRARY关键词所指定的库文件并不相关)

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 Value

If the function succeeds, the return value is a handle to the module.(如果函数成功,返回值是对应模块的句柄)

If the function fails, the return value is NULL. To get extended error information, call

GetLastError.(如果函数失败,返回值是NULL,可以调用GetLastError来获得扩展的错误信息)

Remarks

To enable or disable error messages displayed by the loader during DLL loads, use the

SetErrorMode function.(在加载DLL时,可以调用SetErrorMode函数,来设置使用或者不使用错误信息显示)

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.(LoadLibrary可以用来映射一个DLL模块,并返回该模块的句柄,GetProcAddress可以使用该句柄获取DLL函数的地址) LoadLibrary

can also be used to map other executable modules.(LoadLibrary可以用来映射其他可执行模块)

For example, the function can specify an .exe file to get a handle that can be used in

FindResource or LoadResource.(例如,函数可以指定一个.exe文件来获得一个在FindResource或者LoadResource中使用的句柄) However, do not use LoadLibrary to run

an .exe file, use the CreateProcess function.(但是,不要使用LoadLibrary来运行一个.exe文件,使用CreateProcess)

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.(如果模块是一个没有被映射到调用进程的地址空间中的DLL,系统就会调用DllMain函数,并传入DLL_PROCESS_ATTACH值) If

DllMain returns TRUE, LoadLibrary returns successfully. (如果DllMain返回TRUE,LoadLibrary成功返回)If DllMain returns FALSE, the system unloads the DLL from the

process address space and LoadLibrary returns NULL. (如果返回FALSE,系统会从进程的地址空间中卸载DLL,LoadLibrary返回NULL)

It is not safe to call LoadLibrary from DllMain. For more information, see the Remarks

section in DllMain.(在DllMain中调用LoadLibrary并不安全,可以查看在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(一个进程对LoadLibrary的调用并不会产生其它进程可以使用的句柄) — for example, in calling

GetProcAddress. The other process must make its own call to LoadLibrary for the

module before calling GetProcAddress.(例如,在调用GetProcAddress时,其它进程在调用GetProcAddress之前必须自行调用LoadLibrary)

If lpFileName does not include a path and there is more than one loaded module with the

same base name and extension, the function returns a handle to the module that was

loaded first.(如果lpFileName并不包含一个路径,而且有这相同的基名字,和扩展名的加载模块多余一个,函数返回最先加载的模块的句柄)

If no file name extension is specified in the lpFileName parameter, the default library

extension .dll is appended.(如果在lpFileName参数中没有指定文件扩展名,缺省的.dll扩展名就会被附加上) However, the file name string can include a trailing point character (.) to

indicate that the module name has no extension.(然而,文件名字符串可以饱和一个结尾句号字符来俺是模块名没有扩展名) When no path is specified, the function searches for loaded

modules whose base name matches the base name of the module to be loaded. (当没有指定路径时,函数会寻找和基名字相符的已加载模块)If the name matches, the load succeeds.(如果名字相符,加载成功) Otherwise, the function searches for the file. (不然的话,函数会寻找文件) For more information on the DLL search order, see Dynamic-Link Library Search Order.(可以查看动态链接库查找循序来获得更多的DLL查找顺序信息)

The first directory searched is the one directory containing the image file used to create

the calling process(首选路径是包含用于创建调用进程映射文件的路径) (for more information,

see the CreateProcess function)(可以参看CreateProcess函数来获取更多的信息). Doing

this allows private dynamic-link library (DLL) files associated with a process to be found

without adding the process's installed directory to the PATH environment variable.(这样做

可以确保在没有把进程安装路径加入到PATH环境变量的情况下,允许和进程关联的私有动态链接库文件被找到)

The search path can be altered using the SetDllDirectory function.(寻找路径可以使用SetDllDirectory来做修改) This solution is recommended instead of using

SetCurrentDirectory or hard-coding the full path to the DLL.(建议使用这种解决方法来代替SetCurrentDirectory或者硬编码DLL的完整路径)

If a path is specified and there is a redirection file for the application, the function searches

for the module in the application's directory.(如果指定一个路径并且应用程序有一个重定位文件,函数会在应用程序所在路径中寻找模块) If the module exists in the application's directory,

LoadLibrary ignores the specified path and loads the module from the application's

directory. (如果指定模块存在于应用程序所在路径,LoadLibrary会忽略指定路径并且从应用程序所在路径加载指定模块)If the module does not exist in the application's directory, LoadLibrary

loads the module from the specified directory. (如果模块不存在于应用程序所在路径,LoadLibrary会从指定路径中中加载该模块)For more information, see Dynamic Link Library

Redirection.(可以查看动态链接库重定位信息来获得更多信息)

If you call LoadLibrary with the name of an assembly without a path specification and the

assembly is listed in the system compatible manifest, the call is automatically redirected to

the side-by-side assembly.(如果你通过没有路径名字的集合来调用LoadLibrary,并且该集合在系统兼容清单中列举出来,这个调用会自动重定位到并排的集合)

Windows Server 2003 and Windows XP: The Visual C++ compiler supports a syntax

that enables you to declare thread-local variables: _declspec(thread). (Visual C++编译器支持一种可以使你声明线程本地变量的语法:_declspec(thread))If you use this syntax in a

DLL, you will not be able to load the DLL explicitly using LoadLibrary on versions of

Windows prior to Windows Vista.(如果你在一个DLL链接库中使用这种语法,你就不能在Windows Vista之前的版本中通过LoadLibrary来明确加载此DLL) If your DLL will be loaded

explicitly, you must use the thread local storage functions instead of _declspec(thread).

(如果你希望你的DLL呗明确加载,你必须使用线程本地存储函数而不是:_declspec(thread))For

an example, see Using Thread Local Storage in a Dynamic Link Library.可以查看在动态链接库中使用线程本地存储来获取一个实例)

Example Code

For an example, see Using Run-Time Dynamic Linking.(查看使用运行时动态链接来获得一个实例)

Requirements

Client

Server

Header

Requires Windows Vista, Windows XP, or Windows 2000 Professional.

Requires Windows Server 2008, Windows Server 2003, or Windows 2000 Server.

Declared in Winbase.h; include Windows.h.

Library

Use .

DLL

Requires .

Unicode

Implemented as LoadLibraryW (Unicode) and LoadLibraryA (ANSI).

See Also

DllMain

Dynamic-Link Library Functions

FindResource

FreeLibrary

GetProcAddress

GetSystemDirectory

GetWindowsDirectory

LoadLibraryEx

LoadResource

Run-Time Dynamic Linking

SetDllDirectory

SetErrorMode

Send comments about this topic to Microsoft

Build date: 4/24/2008

LoadLibraryEx Function

Maps the specified executable module into the address space of the calling process. The

executable module can be a .dll or an .exe file. The specified module may cause other modules

to be mapped into the address space.

Syntax

HMODULE WINAPI LoadLibraryEx(

__in LPCTSTR lpFileName,

__reserved HANDLE hFile,

__in DWORD dwFlags

);

Parameters

lpFileName

The name of the executable module (either a .dll or an .exe file). This name is the file

name of the executable module; it is not related to the name stored in a 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, and the file name extension is omitted, the

function appends the default library extension .dll to the file name. However, the file

name string can include a trailing point character (.) to indicate that the module name

has no extension.

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.

If mapping the specified module into the address space causes the system to map in

other, associated executable modules, the function can use either the standard search

strategy or an alternate search strategy to find those modules. See the Remarks for

more information.

hFile

This parameter is reserved for future use. It must be NULL.(这个参数为未来的使用所预订,必须设为NULL)

dwFlags

The action to be taken when loading the module.(当加载此模块时,会采用该措施) If no

flags are specified, the behavior of this function is identical to that of the LoadLibrary

function.(如果没有指定任何标记,这个函数的行为和LoadLibrary和行为完全相同) This

parameter can be one of the following values.(这个参数可以使以下值的某一个)

Value

DONT_RESOLVE_DLL_REFERENCES

0x00000001

Meaning

If this value is used, and the executable module is

a DLL, the system does not call DllMain for process

and thread initialization and termination(如果使用该值,并且可执行模块式一个DLL文件,系统就不会调用DllMain来进行进程和线程的初始化和终止过程). Also,

the system does not load additional executable

modules that are referenced by the specified

module.(并且系统不会加载指定的模块所提到的附加的可执行模块)

Note Use extreme care when using this value.(使用这个值务必要十分小心) If LoadLibrary

or LoadLibraryEx is called for the same DLL

after the DLL is loaded, then the DLL

references will not have been resolved.(如果在该DLL加载完之后传入了同样的DLL来调用了LoadLibrary或者LoadLibraryEx,这样该DLL文件引用会无法解析) This is because the second

call to LoadLibrary or LoadLibraryEx simply

returns a handle to the already loaded DLL.(这是因为LoadLibrary或者LoadLibraryEx会简单返回一个已经加载的DLL链接库句柄) In this

case, the process is likely to terminate

abnormally.(如果是这种情况,进程很有肯能会非正常终止)

If you are planning only to access data or

resources in the DLL, it is better to use

LOAD_LIBRARY_AS_DATAFILE_EXCLUSIVE or

LOAD_LIBRARY_AS_IMAGE_RESOURCE or both.(如果你仅仅打算访问DLL的数据或者资源,最好使用LOAD_LIBRARY_AS_DATAFILE_EXCLUSIVE或者LOAD_LIBRARY_AS_IMAGE_RESOURCE)

LOAD_IGNORE_CODE_AUTHZ_LEVEL

0x00000010

If this value is used, the system does not perform

automatic trust comparisons on the DLL or its

dependents when they are loaded.(如果使用这个值,系

统不会会自动表现处对在DLL或者在加载的依赖项的对比信任)

Windows 2000: This value is not supported.(Windows

2000:不支持该值)

LOAD_LIBRARY_AS_DATAFILE

0x00000002

If this value is used, the system maps the file

into the calling process's virtual address space as

if it were a data file.(如果使用该值,系统会像数据文件一样把文件映射到调用进程的虚拟地址空间)

Nothing is done to execute or prepare to execute

the mapped file.(没有做执行或者准备执行映射文件的工作) Therefore, you cannot call functions like

GetModuleHandle or GetProcAddress with this DLL.(因此你可以使用这个DLL来调用像GetModuleHandle或者GetProcAddress这样的函数) Using this value

causes writes to read-only memory to raise an

access violation.(使用该值会造成写入只读内存时引发访问违规) Use this flag when you want to load a

DLL only to extract messages or resources from it.(当你使用一个DLL仅仅是为了提取信息或者资源,可以使用这个标记)

This value can be used with

LOAD_LIBRARY_AS_IMAGE_RESOURCE.(该值可以和LOAD_LIBRARY_AS_IMAGE_RESOURCE一块使用) For more

information, see Remarks.(可以查看注意来获取更多信息)

LOAD_LIBRARY_AS_DATAFILE_EXCLUSIVE Similar to LOAD_LIBRARY_AS_DATAFILE, except that

0x00000040

the DLL file on the disk is opened for exclusive

write access.(该值除了对在磁盘上的DLL文件有独占的写入权限之外,和LOAD_LIBRARY_AS_DATAFILE相同)

Therefore, other processes cannot open the DLL file

for write access while it is in use.(因此,当DLL文件正在使用时,其它进程不能以写入方式打开DLL文件)

However, the DLL can still be opened by other

processes.(然而,该DLL仍可被其它进程打开)

This value can be used with

LOAD_LIBRARY_AS_IMAGE_RESOURCE. (该值可以和LOAD_LIBRARY_AS_IMAGE_RESOURCE一起使用)For more

information, see Remarks.(请查看注意来获取更多的信息)

Windows Server 2003 and Windows XP/2000: This value

is not supported until Windows Vista.(该值不支持Windows Vista之前的版本)

LOAD_LIBRARY_AS_IMAGE_RESOURCE

0x00000020

If this value is used, the system maps the file

into the process's virtual address space as an

image file.(如果使用该值,系统会把文件作为映射文件映射到进程的虚拟地址空间) However, the loader does

not load the static imports or perform the other

usual initialization steps.(然而,加载者并不加载静态导入库,也不执行其它通常的初始化步骤) Use this

flag when you want to load a DLL only to extract

messages or resources from it.(当你仅仅是为了提取信息或者资源文件而加载DLL,使用这个标记)

Unless the application depends on the image layout,

this value should be used with either

LOAD_LIBRARY_AS_DATAFILE_EXCLUSIVE or

LOAD_LIBRARY_AS_DATAFILE.(除非改程序依靠映射设计,该值应该和LOAD_LIBRARY_AS_DATAFILE_EXCLUSIVE或者LOAD_LIBRARY_AS_DATAFILE一起使用) For more

information, see the Remarks section.(查看注意段来

获取更多的信息)

Windows Server 2003 and Windows XP/2000: This value

is not supported until Windows Vista.(该值不支持Windows Vista以前的版本)

LOAD_WITH_ALTERED_SEARCH_PATH

0x00000008

If this value is used, and

lpFileName specifies an

absolute path, the system uses the alternate file

search strategy discussed in the Remarks section to

find associated executable modules that the

specified module causes to be loaded.(如果使用该值,且lpFileName指定了一个绝对路径,系统使用在注意段讨论的供选择的寻找策略来寻找指定模块加载的相关模块) This value does not work with relative paths.(该值对相关路径不起作用)

If this value is not used, or if

lpFileName does

not specify a path, the system uses the standard

search strategy discussed in the Remarks section to

find associated executable modules that the

specified module causes to be loaded.(如果没有使用该值,或者lpFileName没有指定一个路径,系统使用在注意段讨论的标准寻找策略,来寻找指定模块加载的相关的执行模块)

Return Value

If the function succeeds, the return value is a handle to the mapped executable

module.

If the function fails, the return value is NULL. To get extended error information,

call GetLastError.

Remarks

If lpFileName does not include a path and there is more than one loaded module

with the same base name and extension, the function returns a handle to the

module that was loaded first.

The calling process can use the handle returned by this function to identify the

module in calls to the GetProcAddress, FindResource, and LoadResource

functions.

To enable or disable error messages displayed by the loader during DLL loads,

use the SetErrorMode function.

The LoadLibraryEx function is very similar to the LoadLibrary function.(LoadLibraryEx函数和LoadLibrary非常相似) The differences consist of a set of

optional behaviors that LoadLibraryEx provides.(不同之处包含在LoadLibraryEx提供的可选择行为) First, LoadLibraryEx can map a DLL module without calling

the DllMain function of the DLL. (首先LoadLibrary可以在不调用DLL中的DllMain函数的条件下来加载DLL模块)Second, LoadLibraryEx can use either of two file

search strategies to find executable modules that are associated with the

specified module.(第二,LoadLibraryEx可以使用两种寻找策略中的一个来寻找与指定模块相关的可执行模块) Third, LoadLibraryEx can load a module in a way that is

optimized for the case where the module will never be executed, loading the

module as if it were a data file.(第三,LoadLibraryEx可以使用那些模块中永远不会执行的地方所引起的优化的方式来加载模块,好像是加载一个数据文件) You select these

optional behaviors by setting the dwFlags parameter; (你可以通过设置dwFlags参数来选择这些可选行为)if dwFlags is zero, LoadLibraryEx behaves identically to

LoadLibrary. (如果dwFlags为0,LoadLibraryEx行为表现欲LoadLibrary相同)

If the LOAD_LIBRARY_AS_DATAFILE, LOAD_LIBRARY_AS_DATAFILE_EXCLUSIVE,

or LOAD_LIBRARY_AS_IMAGE_RESOURCE value is specified for the dwFlags

parameter, the loader checks whether the DLL module was already loaded by

the process as a normal DLL. (如果参数dwFlags指定为LOAD_LIBRARY_AS_DATAFILE,LOAD_LIBRARY_AS_DATAFILE_EXCLUSIVE或者LOAD_LIBRARY_AS_IMAGE_RESOURCE值,加载者检查指定的模块是否已经被进程以一个普通的DLL文件加载了)If so, this means that it is already mapped into the

virtual address space of the calling process. (如果是这样,这意味着它已经映射到调用进程的虚拟地址空间中)In this case, LoadLibraryEx returns a handle to the DLL

and increments the DLL reference count. (在这种情况下,LoadLibraryEx返回该DLL的句柄并且将DLL的引用计数加一)If the DLL module was not already loaded

as a DLL, the system maps the DLL module as a data or image file and not as a

DLL. (如果DLL模块还没有以一个DLL方式加载,系统会以数据或者映射文件方式映射DLL文件,而不是一个标准的DLL方式)In this case, LoadLibraryEx returns a

handle to the data or image mapping, but this mapping is not reference-counted.(LoadLibraryEx返回数据或者映射文件的句柄,但是这个映射并没有引用计数) If

LoadLibraryEx is called twice for the same file with

LOAD_LIBRARY_AS_DATAFILE, LOAD_LIBRARY_AS_DATAFILE_EXCLUSIVE, or

LOAD_LIBRARY_AS_IMAGE_RESOURCE specified for the dwFlags parameter, two

separate mappings are created for the file.(如果dwFlags参数以相同的LOAD_LIBRARY_AS_DATAFILE,LOAD_LIBRARY_AS_DATAFILE_EXCLUSIVE或者LOAD_LIBRARY_AS_IMAGE_RESOURCE方式调用两次,这将为文件创建两个分开的映射)

LOAD_LIBRARY_AS_DATAFILE does not prevent other processes from modifying

the DLL while it is loaded. (当被加载时,LOAD_LIBRARY_AS_DATAFILE并不阻止其他进程修改DLL)Because this can make your application less secure, you

should use LOAD_LIBRARY_AS_DATAFILE_EXCLUSIVE instead of

LOAD_LIBRARY_AS_DATAFILE when creating a data mapping, unless you

specifically need to use LOAD_LIBRARY_AS_DATAFILE. (因为这样会使程序不安全,在加载数据映射时,你应该使用LOAD_LIBRARY_AS_DATAFILE_EXCLUSIVE而不是LOAD_LIBRARY_AS_DATA_FILE,除非你确实需要使LOAD_LIBRARY_AS_DATA_FILE)Specifying

LOAD_LIBRARY_AS_DATAFILE_EXCLUSIVE prevents other processes from

modifying the DLL while it is loaded.(指定LOAD_LIBRARY_AS_DATAFILE_EXCLUSIVE可以在加载DLL文件时,阻止其他进程修改DLL) Do not specify LOAD_LIBRARY_AS_DATAFILE and

LOAD_LIBRARY_AS_DATAFILE_EXCLUSIVE in the same call.(在同一调用中不要同时指定LOAD_LIBRARY_AS_DATAFILE和

LOAD_LIBRARY_AS_DATAFILE_EXCLUSIVE)

When the LOAD_LIBRARY_AS_IMAGE_RESOURCE value is used, the DLL is

mapped as an image using portable executable (PE) section alignment

expansion.(当使用LOAD_LIBRARY_AS_IMAGE_RESOURCE时,该DLL会作为一个映射文件使用可移植执行模块段对齐扩展方式来映射) Relative virtual addresses (RVA)

do not have to be mapped to disk addresses, so resources can be more quickly

retrieved from the DLL.(相关的虚拟地址不会被映射到磁盘地址,所以资源可以从DLL文件中很快获得) Specifying LOAD_LIBRARY_AS_IMAGE_RESOURCE prevents

other processes from modifying the DLL while it is loaded.(指定LOAD_LIBRARY_AS_IMAGE_RESOURCE可以在加载该DLL时阻止其他进程修改DLL)

Unless an application depends on specific image mapping characteristics, the

LOAD_LIBRARY_AS_IMAGE_RESOURCE value should be used with either

LOAD_LIBRARY_AS_DATAFILE_EXCLUSIVE or LOAD_LIBRARY_AS_DATAFILE.(除非一个应用程序依靠指定的映射文件符号,LOAD_LIBRARY_AS_IMAGE_RESOURCE应该和LOAD_LIBRARY_AS_DATAFILE_EXCLUSIVE或者LOAD_LIBRARY_AS_DATAFILE一块使用) This allows the loader to choose whether to perform the mapping as an

image resource or a data file, selecting whichever option enables the system to

share pages more effectively. (这允许加载者选择是否以映射资源或者数据的方式来加载,无论选择哪种,都可以是系统分享页面更加有效)Resource functions such as

FindResource can use either mapping. (资源函数比如FindResource可以使用两种映射中的任何一种)

To determine how a module was loaded, use one of the following macros to test

the handle returned by LoadLibraryEx. (为了决定一个模块的加载方式,可以使用下面的宏来测试LoadLibraryEx返回的句柄)

#define LDR_IS_DATAFILE(handle) (((ULONG_PTR)(handle)) &

(ULONG_PTR)1)

#define LDR_IS_IMAGEMAPPING(handle) (((ULONG_PTR)(handle)) &

(ULONG_PTR)2)

#define LDR_IS_RESOURCE(handle) (LDR_IS_IMAGEMAPPING(x) ||

LDR_IS_DATAFILE(x))

The following table describes these macros.(下表描述了这些宏)

Macro

LDR_IS_DATAFILE(handle)

Description

If this macro returns TRUE, the module was loaded

with LOAD_LIBRARY_AS_DATAFILE or

LOAD_LIBRARY_AS_DATAFILE_EXCLUSIVE.(如果这个宏返回TRUE,模块以LOAD_LIBRARY_AS_DATAFILE或者LOAD_LIBRARY_AS_DATAFILE_EXCLUSIVE方式加载)

LDR_IS_IMAGEMAPPING(handle)

If this macro returns TRUE, the module was loaded

with LOAD_LIBRARY_AS_IMAGE_RESOURCE.(如果该值返回TRUE,该模块以OAD_LIBRARY_AS_IMAGE_RESOURCE的方式加载)

LDR_IS_RESOURCE(handle)

If this macro returns TRUE, the module was loaded

with LOAD_LIBRARY_AS_IMAGE_RESOURCE and either

LOAD_LIBRARY_AS_DATAFILE or

LOAD_LIBRARY_AS_DATAFILE_EXCLUSIVE.(如果该值返回TRUE,该模块以LOAD_LIBRARY_IMAGE_RESOURCE和LOAD_LIBRARY_AS_DATAFILE或者LOAD_LIBRARY_AS_DATAFILE_EXCLUSIVE方式加载)

When the FreeLibrary function is called, if the handle specified in the hModule

parameter is a data or image mapping handle, the mapping is destroyed. (当调用FreeLibrary函数时,如果hModule参数指定的句柄是一个数据或者映射句柄,该映射将被销毁)Otherwise, the handle is treated as a normal DLL handle, and the DLL

reference count is decremented.(否则,该句柄会以普通的DLL句柄对待,且DLL引用计数减一)

It is not safe to call LoadLibraryEx from DllMain.在DllMain中调用LoadLibraryEx并不安全) For more information, see the Remarks section in

DllMain.(为了获取更多的信息,请查看DllMain中的注意段)

The LoadLibraryEx function performs a standard search for modules if the file

name is specified without a path and the base file name does not match the

base file name of a loaded module, or there is a path specified but

LOAD_WITH_ALTERED_SEARCH_PATH is not used.(如果指定的文件名没有路径或者基文件名和加载模块的及文件名并不相符,或者指定了一个路径但是没有使用LOAD_WITH_ALTERED_SEARCH_PATH,LoadLibraryEx函数会执行标准的模块搜寻方式) If a path is specified and dwFlags is set to

LOAD_WITH_ALTERED_SEARCH_PATH, the LoadLibraryEx function uses an

alternate file search strategy. (如果指定了一个路径并且dwFlags被设置为LOAD_WITH_ALTERED_SEARCH_PATH,LoadLibraryEx函数使用一个可选择的文件搜寻策略)The specified path must be an absolute path. (指定的路径必须是一个绝对路径)LOAD_WITH_ALTERED_SEARCH_PATH does not work with relative paths.(LOAD_WITH_ALTERED_SEARCH_PATH不能和相关路径一起使用) For more

information about alternate search strategies, see Dynamic-Link Library Search

Order.(查看动态链接库搜寻顺序来获取更多可选择的搜寻策略)

The search path can be altered using the SetDllDirectory function. (搜寻路径可以使用SetDllDirectory来更改)This solution is recommended instead of using

SetCurrentDirectory or hard-coding the full path to the DLL.

If a path is specified and there is a redirection file associated with the

application, the LoadLibraryEx function searches for the module in the

application directory. If the module exists in the application directory,

LoadLibraryEx ignores the path specification and loads the module from the

application directory. If the module does not exist in the application directory,

the function loads the module from the specified directory. For more information,

see Dynamic Link Library Redirection.

If you call LoadLibraryEx with the name of an assembly without a path

specification and the assembly is listed in the system compatible manifest, the

call is automatically redirected to the side-by-side assembly.

Visual C++: The Visual C++ 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 LoadLibraryEx on versions of Windows prior to Windows Vista.

If your DLL will be loaded explicitly, you must use the thread local

storage functions instead of _declspec(thread). For an example,

see Using Thread Local Storage in a Dynamic Link Library.

Example Code

For an example, see Looking Up Text for Error Code Numbers.

Requirements

Client

Server

Header

Requires Windows Vista, Windows XP, or Windows 2000 Professional.

Requires Windows Server 2008, Windows Server 2003, or Windows 2000

Server.

Declared in Winbase.h; include Windows.h.

Library

Use .

DLL

Requires .

Unicode

Implemented as LoadLibraryExW (Unicode) and LoadLibraryExA (ANSI).

See Also

DllMain

Dynamic-Link Library Functions

FindResource

FreeLibrary

GetProcAddress

GetSystemDirectory

GetWindowsDirectory

LoadLibrary

LoadResource

OpenFile

Run-Time Dynamic Linking

SearchPath

SetDllDirectory

SetErrorMode

Send comments about this topic to Microsoft

Build date: 4/24/2008

MSDN对LoadLibrary和LoadLibraryEx的详尽的分析使两者的区别不言自明,其主要区别就在于LoadLibraryEx中的dwFlags参数可以设置加载时的一些可选择项。上文对dwFlags参数和注意段的描述我们可以得到详细并且权威的讲解,我不在赘述,就此搁笔!