原文地址:http://msdn.microsoft.com/library/en-us/dllproc/base/dynamic_link_library_redirection.asp
参考: http://msdn2.microsoft.com/en-us/library/aa376414.aspx
Dll重定向(动态链接库)


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 (OS) updates such as service packs.

Windows 2000:  Known DLLs can be redirected in this OS.

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

Windows 2000:  Manifests do not affect DLL redirection.

 

  • 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.


原文地址 : http://msdn2.microsoft.com/en-us/library/aa376307.aspx
Side-by-side Assemblies

A Windows side-by-side assembly is described by manifests. A side-by-side assembly contains a collection of resources—a group of DLLs, Windows classes, COM servers, type libraries, or interfaces—that are always provided to applications together. These are described in the assembly manifest.

 

Typically, a side-by-side assembly is a single DLL. For example, the Microsoft COMCTL32 assembly is a single DLL with a manifest whereas the Microsoft Visual C++ development system run-time libraries assembly contains multiple files. Manifests contain metadata that describes side-by-side assemblies and side-by-side assembly dependencies.

Side-by-side assemblies are used by the operating system as fundamental units of naming, binding, versioning, deployment, and configuration. Every side-by-side assembly has a unique identity. One of the attributes of the assembly identity is its version. For more information, see Assembly Versions.

Starting with Windows XP, multiple versions of side-by-side assemblies can be used by applications running at the same time. Manifests, and the assembly version number, are used by the loader to determine the correct binding of assembly versions to applications. Side-by-side assemblies and manifests work with applications and the side-by-side manager as illustrated in the following figure.


In the preceding example, both Comctl32.DLL version 6.0 and Comctl32.DLL version 5.0 are in the side-by-side assembly cache and available to applications. When an application calls to load the DLL, the side-by-side manager determines whether the application has a version dependence described in a manifest. If there is no relevant manifest, the system loads the default version of the assembly. For Windows XP, version 5.0 of Comctl32.DLL is the system default. If the side-by-side manager finds a dependence on version 6.0 stated in a manifest, that version is loaded to run with the application.


Manifest File Schema

The following is the complete listing of the manifest file schema.

 

 

<?xml version="1.0" ?>
<Schema xmlns="urn:schemas-microsoft-com:xml-data" xmlns:dt="urn:schemas-microsoft-com:datatypes" name="urn:schemas-microsoft-com:asm.v1">
<!--  Attributes
-->
<AttributeType name="manifestVersion" dt:type="enumeration" dt:values="1.0" />
<AttributeType name="name" dt:type="string" />
<AttributeType name="type" dt:type="string" />
<AttributeType name="publicKeyToken" dt:type="bin.hex" />
<AttributeType name="language" dt:type="string" />
<AttributeType name="processorArchitecture" dt:type="string" />
<AttributeType name="version" dt:type="string" />
<AttributeType name="optional" dt:type="enumeration" dt:values="yes no" />
<AttributeType name="clsid" dt:type="string" />
<AttributeType name="description" dt:type="string" />
<AttributeType name="threadingModel" dt:type="string" />
<AttributeType name="tlbid" dt:type="string" />
<AttributeType name="progid" dt:type="string" />
<AttributeType name="helpdir" dt:type="string" />
<AttributeType name="iid" dt:type="string" />
<AttributeType name="numMethods" dt:type="ui4" />
<AttributeType name="resourceid" dt:type="string" />
<AttributeType name="flags" dt:type="enumeration" dt:values="control hidden restricted hasdiskimage" />
<AttributeType name="loadFrom" dt:type="string" />
<AttributeType name="hashalg" dt:type="enumeration" dt:values="SHA1 SHA MD5 MD4 MD2" />
<AttributeType name="hash" dt:type="bin.hex" />
<AttributeType name="proxyStubClsid32" dt:type="string" />
<AttributeType name="baseInterface" dt:type="string" />
<AttributeType name="versioned" dt:type="enumeration" dt:values="yes no" />
<AttributeType name="oldVersion" dt:type="string" />
<AttributeType name="newVersion" dt:type="string" />
<AttributeType name="size" dt:type="ui8" />
<AttributeType name="runtimeVersion" dt:type="string" />
<!--  Elements
-->
<ElementType name="assembly" order="seq" model="closed" content="eltOnly">
<attribute type="manifestVersion" required="yes" />
<group order="many" minOccurs="0" maxOccurs="*">
<element type="assemblyIdentity" minOccurs="1" maxOccurs="1" />
<element type="noInherit" minOccurs="0" maxOccurs="1" />
<element type="noInheritable" minOccurs="0" maxOccurs="1" />
</group>
<group order="many" minOccurs="0" maxOccurs="*">
<element type="description" minOccurs="0" maxOccurs="1" />
<element type="noInherit" minOccurs="0" maxOccurs="1" />
<element type="noInheritable" minOccurs="0" maxOccurs="1" />
<element type="comInterfaceExternalProxyStub" minOccurs="0" />
<element type="dependency" minOccurs="0" />
<element type="file" minOccurs="0" />
<element type="clrClass" minOccurs="0" />
<element type="clrSurrogate" minOccurs="0" />
</group>
</ElementType>
<ElementType name="clrClass" model="closed" content="eltOnly">
<attribute type="name" required="yes" />
<attribute type="clsid" required="yes" />
<attribute type="progid" required="no" />
<attribute type="tlbid" required="no" />
<attribute type="description" required="no" />
<attribute type="runtimeVersion" required="no" />
<attribute type="threadingModel" required="no" />
<element type="progid" minOccurs="0" maxOccurs="*" />
</ElementType>
<ElementType name="clrSurrogate" model="closed" content="empty">
<attribute type="clsid" required="yes" />
<attribute type="name" required="yes" />
<attribute type="runtimeVersion" required="no" />
</ElementType>
<ElementType name="assemblyIdentity" model="closed">
<attribute type="name" required="yes" />
<attribute type="version" required="no" />
<attribute type="type" required="no" />
<attribute type="processorArchitecture" required="no" />
<attribute type="publicKeyToken" required="no" />
<attribute type="language" required="no" />
</ElementType>
<ElementType name="comInterfaceProxyStub" model="closed">
<attribute type="iid" required="yes" />
<attribute type="name" required="yes" />
<attribute type="tlbid" required="no" />
<attribute type="numMethods" required="no" />
<attribute type="proxyStubClsid32" required="no" />
<attribute type="baseInterface" required="no" />
</ElementType>
<ElementType name="description" />
<ElementType name="dependency" model="closed" content="eltOnly">
<element type="dependentAssembly" minOccurs="0" maxOccurs="1" />
<attribute type="optional" required="no" />
</ElementType>
<ElementType name="dependentAssembly" model="closed" content="eltOnly">
<element type="assemblyIdentity" minOccurs="1" maxOccurs="1" />
<element type="bindingRedirect" minOccurs="0" maxOccurs="*" />
</ElementType>
<ElementType name="bindingRedirect" model="closed" content="empty">
<attribute type="oldVersion" required="yes" />
<attribute type="newVersion" required="yes" />
</ElementType>
<ElementType name="file" model="closed" content="eltOnly">
<attribute type="name" required="yes" />
<attribute type="hash" required="no" />
<attribute type="hashalg" required="no" />
<attribute type="loadFrom" required="no" />
<attribute type="size" required="no" />
<group order="many" minOccurs="0" maxOccurs="*">
<element type="comClass" minOccurs="0" />
<element type="comInterfaceProxyStub" minOccurs="0" />
<element type="typelib" minOccurs="0" />
<element type="windowClass" minOccurs="0" />
</group>
</ElementType>
<ElementType name="comClass" model="closed" content="eltOnly">
<attribute type="clsid" required="yes" />
<attribute type="threadingModel" required="no" />
<attribute type="progid" required="no" />
<attribute type="tlbid" required="no" />
<attribute type="description" required="no" />
<element type="progid" minOccurs="0" maxOccurs="*" />
</ElementType>
<ElementType name="comInterfaceExternalProxyStub" model="closed" content="empty">
<attribute type="iid" required="yes" />
<attribute type="name" required="yes" />
<attribute type="tlbid" required="no" />
<attribute type="numMethods" required="no" />
<attribute type="proxyStubClsid32" required="no" />
<attribute type="baseInterface" required="no" />
</ElementType>
<ElementType name="typelib" model="closed" content="empty">
<attribute type="tlbid" required="yes" />
<attribute type="version" required="yes" />
<attribute type="helpdir" required="yes" />
<attribute type="resourceid" required="no" />
<attribute type="flags" required="no" />
</ElementType>
<ElementType name="windowClass" model="closed" content="textOnly">
<attribute type="versioned" required="no" />
</ElementType>
<ElementType name="noInherit" model="closed" content="empty" />
<ElementType name="noInheritable" model="closed" content="empty" />
<ElementType name="progid" />
</Schema>
posted on 2007-05-29 10:24 孤独的夜 阅读(849) 评论(3)  编辑 收藏 引用 所属分类: Windows SDK
只有注册用户登录后才能发表评论。