Side by Side Assembly介绍--manifest文件的使用
2013-12-19 16:48
239 查看
什么是Side-by-Side Assembly?
Side-by-Side Assembly(建称SxS)是微软在Visual Studio 2005(Windows 2000?)中引入的技术,用来解决Windows平台上的DLL Hell问题。DLL Hell的介绍可以看Wikipedia的文章。简单的说,DLL Hell窘境包括了Windows应用程序依赖的DLL带来的若干问题,包括同名DLL、DLL升级、DLL载入顺序等等。
Side-by-Side Assembly按照我的理解,是一种特殊的DLL,按照Side-by-Side Assembly的要求开发的,并用XML格式的manifest和policy文件描述的。所有的系统Side-by-Side Assembly都安装在Windows目录下的WinSxS子目录里,有一堆的目录、DLL和XML文件。
Side-by-Side Assembly的使用参见MSDN。但MSDN有把简单问题复杂化的毛病,原理讲的很多,实际例子举的很少,不看也罢。
使用Side-by-Side Assembly包括两个方面,一方面是自己开发的应用程序和DLL如何依赖Side-by-Side Assembly,另一方面是如何开发自己的Side-by-Side Assembly。如果只关心第一个方面,问题要简单的多。不需要关心第二个方面的原因如下。
Visual Studio 2010要取消对Side-by-Side Assembly的默认支持?
消息来源于微软的博客,根据文章介绍,2010要改变对应用默认采用的SxS发布方式,回到类似2003的方式,同时支持对CRT的静态和动态联编,对DLL的搜索顺序也是常用的:先搜索应用程序目录,然后System32,然后PATH。
当然SxS还是有的,应该只是Visual Studio不再在开发应用时默认采用。
应用如何依赖Side-by-Side Assembly
cl.exe在链接生成EXE的时候,会同时生成一个同名的manifest文件。该文件是XML格式的,描述了EXE对SxS的依赖。下面是一个manifest的例子:
<?xmlversion="2.0"encoding="UTF-8"standalone="yes"?>
<assemblyxmlns="urn:schemas-microsoft-com:asm.v1"manifestversion="1.0">
<trustinfoxmlns="urn:schemas-microsoft-com:asm.v3">
<security>
<requestedprivileges>
<requestedexecutionleveluiaccess="false"level="asInvoker"></requestedexecutionlevel>
</requestedprivileges>
</security>
</trustinfo>
<dependency>
<dependentassembly>
<assemblyidentityname="Microsoft.VC90.CRT"publickeytoken="1fc8b3b9a1e18e3b"processorarchitecture="x86"version="9.0.21022.8"type="win32"></assemblyidentity>
</dependentassembly>
</dependency>
</assembly>
如果删除这个manifest文件,运行该EXE会出现类似下面的错误:
这是因为缺少manifest文件的描述,程序不知道如何载入WinSxS目录下的DLL。所以最好把manifest文件嵌入EXE中,可以用mt.exe工具完成这一工作:
mt.exe -manifest <manifest-file> -outputresource:<exe-file>;#1
用IDE开发不存在这个问题,IDE会自动调用mt.exe将manifest嵌入EXE
DLL如何依赖Side-by-Side Assembly
同上述原因,DLL生成之后,最好也用mt.exe将manifest文件嵌入。方法与上面类似,不同之处在资源ID应该是2而不是1,其中的区别参见【1】【2】,硬记也行,没什么道理。
mt.exe -manifest <manifest-file> -outputresource:<dll-file>;#2
用IDE开发也不存在这个问题,IDE会自动调用mt.exe将manifest嵌入DLL
如何绕过Side-by-Side Assembly?
Side-by-Side Assembly带来了很多不便,比如说,依赖SxS MSVCR90.dll的程序在没有安装VC redistributable的系统中不能运行,像是WinPE环境。因此最好有一种绕过SxS的方法,将SxS的Assembly和EXE放在一个目录下,避免依赖系统WinSxS目录下的Assembly。
1. 首先,我们需要manifest文件,如果手头没有EXE和DLL的manifest文件,可以用mt.exe工具从EXE和DLL中导出manifest文件
导出EXE的manifest
mt.exe -inputresource:<exe-file>;#1 -out:<manifest-file>
导出DLL的manifest
mt.exe -outputresource:<dll-file>;#2 -out:<manifest-file>
2. 根据manifest中的信息,创建若干新的manifest文件。新manifest文件名和EXE中依赖的Assembly名字对应,例如按照前面的manifest例子,应该对应创建一个Microsoft.VC90.CRT.manifest,内容格式如下:
<?xmlversion="2.0"encoding="UTF-8"standalone="yes"?>
<assemblyxmlns="urn:schemas-microsoft-com:asm.v1"manifestversion="1.0">
<noinheritable></noinheritable>
<assemblyidentityname="Microsoft.VC90.CRT"publickeytoken="1fc8b3b9a1e18e3b"processorarchitecture="x86"version="9.0.21022.8"type="win32"></assemblyidentity>
<filename="msvcr90.dll"></file>
<filename="msvcp90.dll"></file>
</assembly>
其中assemblyIdentity和原EXE的内容要完全一样。
3. 根据manifest文件中的assemblyIdentity信息,到系统的WinSxS目录下的子目录里找DLL,子目录名的格式是<processorArchchitecture>-<name>-<publicKeyToken>-<version>-none-xxx。把子目录下的DLL拷贝到EXE所在的目录下。把DLL的名字写入新manifest的file标签下。
至此,已经把Assembly放到EXE所在目录下,EXE也不再依赖系统WinSxS目录下的Assembly了。
VC 2005和VC 2008相关的SxS打包
我把自己系统中WinSxS目录下所有x86_micosoft.vc开头的目录全部拷贝出来,打了个包,以备不时之需。(下载链接)
Side-by-Side Assembly(建称SxS)是微软在Visual Studio 2005(Windows 2000?)中引入的技术,用来解决Windows平台上的DLL Hell问题。DLL Hell的介绍可以看Wikipedia的文章。简单的说,DLL Hell窘境包括了Windows应用程序依赖的DLL带来的若干问题,包括同名DLL、DLL升级、DLL载入顺序等等。
Side-by-Side Assembly按照我的理解,是一种特殊的DLL,按照Side-by-Side Assembly的要求开发的,并用XML格式的manifest和policy文件描述的。所有的系统Side-by-Side Assembly都安装在Windows目录下的WinSxS子目录里,有一堆的目录、DLL和XML文件。
Side-by-Side Assembly的使用参见MSDN。但MSDN有把简单问题复杂化的毛病,原理讲的很多,实际例子举的很少,不看也罢。
使用Side-by-Side Assembly包括两个方面,一方面是自己开发的应用程序和DLL如何依赖Side-by-Side Assembly,另一方面是如何开发自己的Side-by-Side Assembly。如果只关心第一个方面,问题要简单的多。不需要关心第二个方面的原因如下。
Visual Studio 2010要取消对Side-by-Side Assembly的默认支持?
消息来源于微软的博客,根据文章介绍,2010要改变对应用默认采用的SxS发布方式,回到类似2003的方式,同时支持对CRT的静态和动态联编,对DLL的搜索顺序也是常用的:先搜索应用程序目录,然后System32,然后PATH。
当然SxS还是有的,应该只是Visual Studio不再在开发应用时默认采用。
应用如何依赖Side-by-Side Assembly
cl.exe在链接生成EXE的时候,会同时生成一个同名的manifest文件。该文件是XML格式的,描述了EXE对SxS的依赖。下面是一个manifest的例子:
<?xmlversion="2.0"encoding="UTF-8"standalone="yes"?>
<assemblyxmlns="urn:schemas-microsoft-com:asm.v1"manifestversion="1.0">
<trustinfoxmlns="urn:schemas-microsoft-com:asm.v3">
<security>
<requestedprivileges>
<requestedexecutionleveluiaccess="false"level="asInvoker"></requestedexecutionlevel>
</requestedprivileges>
</security>
</trustinfo>
<dependency>
<dependentassembly>
<assemblyidentityname="Microsoft.VC90.CRT"publickeytoken="1fc8b3b9a1e18e3b"processorarchitecture="x86"version="9.0.21022.8"type="win32"></assemblyidentity>
</dependentassembly>
</dependency>
</assembly>
如果删除这个manifest文件,运行该EXE会出现类似下面的错误:
这是因为缺少manifest文件的描述,程序不知道如何载入WinSxS目录下的DLL。所以最好把manifest文件嵌入EXE中,可以用mt.exe工具完成这一工作:
mt.exe -manifest <manifest-file> -outputresource:<exe-file>;#1
用IDE开发不存在这个问题,IDE会自动调用mt.exe将manifest嵌入EXE
DLL如何依赖Side-by-Side Assembly
同上述原因,DLL生成之后,最好也用mt.exe将manifest文件嵌入。方法与上面类似,不同之处在资源ID应该是2而不是1,其中的区别参见【1】【2】,硬记也行,没什么道理。
mt.exe -manifest <manifest-file> -outputresource:<dll-file>;#2
用IDE开发也不存在这个问题,IDE会自动调用mt.exe将manifest嵌入DLL
如何绕过Side-by-Side Assembly?
Side-by-Side Assembly带来了很多不便,比如说,依赖SxS MSVCR90.dll的程序在没有安装VC redistributable的系统中不能运行,像是WinPE环境。因此最好有一种绕过SxS的方法,将SxS的Assembly和EXE放在一个目录下,避免依赖系统WinSxS目录下的Assembly。
1. 首先,我们需要manifest文件,如果手头没有EXE和DLL的manifest文件,可以用mt.exe工具从EXE和DLL中导出manifest文件
导出EXE的manifest
mt.exe -inputresource:<exe-file>;#1 -out:<manifest-file>
导出DLL的manifest
mt.exe -outputresource:<dll-file>;#2 -out:<manifest-file>
2. 根据manifest中的信息,创建若干新的manifest文件。新manifest文件名和EXE中依赖的Assembly名字对应,例如按照前面的manifest例子,应该对应创建一个Microsoft.VC90.CRT.manifest,内容格式如下:
<?xmlversion="2.0"encoding="UTF-8"standalone="yes"?>
<assemblyxmlns="urn:schemas-microsoft-com:asm.v1"manifestversion="1.0">
<noinheritable></noinheritable>
<assemblyidentityname="Microsoft.VC90.CRT"publickeytoken="1fc8b3b9a1e18e3b"processorarchitecture="x86"version="9.0.21022.8"type="win32"></assemblyidentity>
<filename="msvcr90.dll"></file>
<filename="msvcp90.dll"></file>
</assembly>
其中assemblyIdentity和原EXE的内容要完全一样。
3. 根据manifest文件中的assemblyIdentity信息,到系统的WinSxS目录下的子目录里找DLL,子目录名的格式是<processorArchchitecture>-<name>-<publicKeyToken>-<version>-none-xxx。把子目录下的DLL拷贝到EXE所在的目录下。把DLL的名字写入新manifest的file标签下。
至此,已经把Assembly放到EXE所在目录下,EXE也不再依赖系统WinSxS目录下的Assembly了。
VC 2005和VC 2008相关的SxS打包
我把自己系统中WinSxS目录下所有x86_micosoft.vc开头的目录全部拷贝出来,打了个包,以备不时之需。(下载链接)
相关文章推荐
- 专题四—介绍Manifest文件的使用
- Android深入浅出系列课程---Lesson4-AAF110429_介绍Manifest文件的使用
- 使用maven-assembly-plugin打包,assembly的语法介绍(同时打多个包、排除依赖包、文件更改别名、自定义路径)
- Linux 查看磁盘分区、文件系统、磁盘的使用情况相关的命令和工具介绍 by 北南南北
- 困兽之斗!使用JavaScript执行客户端的exe文件(excute the .exe file on client-side machine by JavaScript)
- Spring.NET使用assembly方式设置配置文件
- 普元 ESB 6.6,在开发的中介流中需要使用queryByNamedSql方法,这个方法对应的namedSql文件应该放在什么位置下?
- AndroidManifest.xml清单配置文件的相关介绍一
- AndroidManifest.xml 文件里面的内容介绍
- 11.MariaDB笔记——cmake使用介绍六动态产生代码文件
- 15.Hello World 项目创建与项目配置文件介绍 - IntelliJ IDEA 使用教程
- 【Android】简易音乐播放器(介绍使用Service和Broadcast播放音乐文件)
- 普通用户使用的命令-文件和目录管理类命令基本用法介绍
- Python open()文件处理使用介绍
- .NET 4.0新功能介绍:In Process Side By Side
- android的AndroidManifest.xml文件介绍
- 关于AndroidManifest.xml配置文件的使用
- 详细介绍附代码:使用jquery,和php文件构建一个简单的在线聊天室,通过ip显示googlemap
- Android的Manifest配置文件介绍
- SWFUpload介绍 使用SWFUpload上传文件