地狱怪客

动态链接库重定向

动态链接库重定向

https://msdn.microsoft.com/en-us/library/windows/desktop/ms682600(v=vs.85).aspx

应用程序可能依赖于特定版本的共享DLL,并且如果使用相同DLL的较新版本或旧版本安装了另一个应用程序,则该应用程序将开始失败。有两种方法可以确保您的应用程序使用正确的DLL:DLL重定向和并行组件。开发人员和管理员应该为现有的应用程序使用DL​​L重定向,因为它不需要对应用程序进行任何更改。如果您正在创建新应用程序或更新应用程序,并希望将应用程序与潜在问题隔离,请创建一个并排组件

要使用DLL重定向,请为您的应用程序创建一个重定向文件。重定向文件必须命名如下:App_name .local。例如,如果应用程序名称是Editor.exe,则重定向文件应该命名为Editor.exe.local。您必须在应用程序目录中安装.local文件。您还必须将DLL安装在应用程序目录中。

重定向文件的内容将被忽略,但是它的存在会导致Windows在加载DLL时首先检查应用程序目录,而不管指定给LoadLibrary或 LoadLibraryEx的路径如何。如果在应用程序目录中没有找到该DLL,那么这些函数使用它们通常的搜索顺序。例如,如果应用程序c:\ myapp \ myapp.exe 使用以下路径调用 LoadLibrary

c:\ program files \ common files \ system \ mydll.dll

而且,如果同时存在c:\ myapp \ myapp.exe.local和c:\ myapp \ mydll.dll, LoadLibrary将加载c:\ myapp \ mydll.dll。否则, LoadLibrary将加载c:\ program files \ common files \ system \ mydll.dll。

或者,如果名为c:\ myapp \ myapp.exe.local的目录存在并且包含mydll.dll, LoadLibrary会加载c:\ myapp \ myapp.exe.local \ mydll.dll。

已知的DLL不能被重定向。有关已知DLL的列表,请参阅以下注册表项:HKEY_LOCAL_MACHINE \ SYSTEM \ CurrentControlSet \ Control \ Session Manager \ KnownDLLs。系统使用Windows文件保护来确保诸如此类的系统DLL不会被更新或删除,除非通过操作系统更新(如Service Pack)。

如果应用程序具有清单,则任何.local文件都将被忽略。

如果您正在使用DLL重定向,并且应用程序无法访问搜索顺序中的所有驱动器和目录,则一旦访问被拒绝,LoadLibrary将停止搜索。(如果您不使用DLL重定向,则LoadLibrary会 跳过它无法访问的目录,然后继续搜索。)

将应用程序DLL安装在包含该应用程序的相同目录中是一种很好的做法,即使您不使用DLL重定向。这确保安装应用程序不会覆盖DLL的其他副本,并导致其他应用程序失败。另外,如果遵循这一良好的做法,其他应用程序不会覆盖您的DLL副本,并导致您的应用程序失败。

Dynamic-Link Library Redirection

Applications can depend on a specific version of a shared DLL and start to fail if another application is installed with a newer or older version of the same DLL. There are two ways to ensure that your application uses the correct DLL: DLL redirection and side-by-side components. Developers and administrators should use DLL redirection for existing applications, because it does not require any changes to the application. If you are creating a new application or updating an application and want to isolate your application from potential problems, create a side-by-side component.

To use DLL redirection, create a redirection file for your application. The redirection file must be named as follows: App_name.local. For example, if the application name is Editor.exe, the redirection file should be named Editor.exe.local. You must install the .local file in the application directory. You must also install the DLLs in the application directory.

The contents of a redirection file are ignored, but its presence causes Windows to check the application directory first whenever it loads a DLL, regardless of the path specified to LoadLibrary or LoadLibraryEx. If the DLL is not found in the application directory, then these functions use their usual search order. For example, if the application c:\myapp\myapp.exe calls LoadLibrary using the following path:

c:\program files\common files\system\mydll.dll

And, if both c:\myapp\myapp.exe.local and c:\myapp\mydll.dll exist, LoadLibrary loads c:\myapp\mydll.dll. Otherwise, LoadLibrary loads c:\program files\common files\system\mydll.dll.

Alternatively, if a directory named c:\myapp\myapp.exe.local exists and contains mydll.dll, LoadLibrary loads c:\myapp\myapp.exe.local\mydll.dll.

Known DLLs cannot be redirected. For a list of known DLLs, see the following registry key: HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\KnownDLLs. The system uses Windows File Protection to ensure that system DLLs such as these are not updated or deleted except by operating system updates such as service packs.

If the application has a manifest, then any .local files are ignored.

If you are using DLL redirection and the application does not have access to all drives and directories in the search order, LoadLibrary stops searching as soon as access is denied. (If you are not using DLL redirection, LoadLibrary skips directories that it cannot access and then continues searching.)

It is good practice to install application DLLs in the same directory that contains the application, even if you are not using DLL redirection. This ensures that installing the application does not overwrite other copies of the DLL and cause other applications to fail. Also, if you follow this good practice, other applications do not overwrite your copy of the DLL and cause your application to fail.

 

码字很辛苦,转载请注明来自人生在世《动态链接库重定向》

评论