嵌入Dll 到.net 程序中的方法

我们经常会写一些小程序给自己或者他人用,而这些程序时长又会去引用一些第三方的Dll,比如开源的ICSharpCode.SharpZipLib.dll等,为了让程序保持整洁,或者给对方的时候方便,就想把这些dll给嵌入到EXE中去,这样在不打包的情况下,只要丢一个文件给对方就能用了.最近研究了下可行性,目前有如下两种方法:

 

  1. 方法1:把相关的第三方dll作为程序资源嵌入到EXE中,在程序运行的时候,从资源文件中输出到程序执行目录即可

    image(图1:示例项目,ThirdPartydlldemo.dll作为第三方资源.Build Action属性设置为" Embedded Resource")

    然后在Program.cs里面声明个静态构造函数,在该方法里面把第三方dll输出到程序目录,这样在调用第三方dll方法的时候,相关环境已经初始化完毕了.

       1: private static void ExtractResourceToFile(string resourceName, string filename)
       2:    {
       3:          if (!System.IO.File.Exists(filename))
       4:             using (System.IO.Stream s = System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream(resourceName))
       5:             usng (System.IO.FileStream fs = new System.IO.FileStream(filename, System.IO.FileMode.Create))
       6:             {
       7:                    byte[] b = new byte[s.Length];
       8:                    s.Read(b, 0, b.Length);
       9:                    fs.Write(b, 0, b.Length);
      10:              }
      11:     }

     

       1: static Program()
       2:       {
       3:           ExtractResourceToFile("EmbeddedDLL2ExeDemo.ThirdPartyDllDemo.dll",
       4:               "ThirdPartyDllDemo.dll");
       5:       }

     

    这样就ok了.

     

  2. 方法2:是用Ilmerge这个微软提供的工具,直接把相关的dll嵌入到目标exe中,而且程序运行时候,不像方法1会把相关的dll输出到可执行目录下,它直接让.net运行时到程序的资源中去找相关的dll引用,以下是Ilmerge的介绍:

    This document describes the ILMerge utility which merges multiple .NET assemblies into a single assembly. However, some .NET assemblies may not be able to be merged because they may contain features such as unmanaged code. I would highly recommend using peverify (the .NET Framework SDK tool) on the output of ILMerge to guarantee that the output is verifiable and will load in the .NET runtime.

    ILMerge is packaged as a console application. But all of its functionality is also accessible programmatically. Note that Visual Studio does allow one to add an executable as a reference, so you can write a client that uses ILMerge as a library.

    ILMerge takes a set of input assemblies and merges them into one target assembly. The first assembly in the list of input assemblies is the primary assembly. When the primary assembly is an executable, then the target assembly is created as an executable with the same entry point as the primary assembly. Also, if the primary assembly has a strong name, and a .snk file is provided, then the target assembly is re-signed with the specified key so that it also has a strong name.

    Note that anything that depended upon any of the names of the input assemblies, e.g., configuration files, must be updated to refer instead to the name of the target assembly.

    Any Win32 Resources in the primary assembly are copied over into the target assembly.

    There are many options that control the behavior of ILMerge. These are described in the next section.

    Ilmerge 相关的命令行参数是:

    ilmerge [/lib:directory]* [/log[:filename]] [/keyfile:filename [/delaysign]] [/internalize[:filename]] [/t[arget]:(library|exe|winexe)] [/closed] [/ndebug] [/ver:version] [/copyattrs [/allowMultiple]] [/xmldocs] [/attr:filename] ([/targetplatform:<version>[,<platformdir>]]|v1|v1.1|v2|v4) [/useFullPublicKeyForReferences] [/zeroPeKind] [/wildcards] [/allowDup[:typename]]* [/allowDuplicateResources] [/union] [/align:n] /out:filename <primary assembly> [<other assemblies>...]

    其中目标exe或者程序集,要放在输入的程序集里面的第一位置,其他dll放在它之后.其中/out:参数是必须的,其他参数可以参考文档

    如图1 示例,命令行参数是

    ilmerge EmbeddedDLL2ExeDemo.exe ThirdPartyDllDemo.dll /ndebug /out:EmbeddedDll2Ex
    eDemo.exe

    这样既可,该方法比方法1更完美,不过这个Ilmerge 在使用的时候还有一些不足的地方,比如/ver:version,这个参数设置后没有效果;

     

    ilmerge就用自己把它引用到的两个dll嵌软到它自身里面了.

方法3:使用.Net混淆器都附带这样的功能,可以把多个dll整合到一个可执行文件中。 感谢 斯克迪亚 提供
posted @ 2010-06-28 22:34 airwolf2026 阅读(3733) 评论(27) 编辑 收藏

 回复 引用 查看   
#1楼2010-06-28 22:42 | iseeyouyou      
这个方法不错
 回复 引用 查看   
#2楼2010-06-28 22:49 | 布尔运算      
注册AssemblyResolve就可以在运行时CLR找不到类库的时候动态的加载或者生成类库,你可以在里面返回从资源或者其他数据来源存储的二进制数据集,可以附加加密之类的功能
 回复 引用 查看   
#3楼[楼主]2010-06-29 00:04 | airwolf2026      
@布尔运算
好的,我去看看.谢谢分享

 回复 引用 查看   
#4楼2010-06-29 00:13 | Tony Chi      
不需要写入文件之后再加载。可以直接通过流内数据加载。

byte[] assembly = File.ReadAllBytes(path);

//app domain load
AppDomain appDomain = AppDomain.CreateDomain("Test Domain");
loadedAssembly = appDomain.Load(assembly);
obj = loadedAssembly.CreateInstance("ClassLibrary1.Class1");
mInfo = obj.GetType().GetMethod("SayHello");
result = mInfo.Invoke(obj, null);
Console.WriteLine("Result: {0}", result.ToString());

//Assembly load sample
loadedAssembly = Assembly.Load(assembly);
obj = loadedAssembly.CreateInstance("ClassLibrary1.Class1");
mInfo = obj.GetType().GetMethod("SayHello");
result = mInfo.Invoke(obj, null);
Console.WriteLine("Result: {0}", result.ToString());

 回复 引用 查看   
#5楼2010-06-29 04:18 | 布尔运算      
@airwolf2026
我怎么记得当时打了AppDomain

这个事件是AppDomain的,在main方法开始时就可以注册了,不过有点儿需要注意的是这个事件不符合标准的事件处理范式,程序集是需要return出去的,也就是MulticaseDelegate只有最后一个事件处理程序可以成功返回程序集

 回复 引用 查看   
#6楼2010-06-29 08:15 | Geerry      
不错,好样的,顶一下:)
 回复 引用 查看   
#7楼2010-06-29 09:07 | YoungCoder      
0.0
 回复 引用 查看   
#8楼2010-06-29 09:11 | 北方的狼      
这个方法好!
 回复 引用 查看   
#9楼2010-06-29 09:19 | 周公      
byte[] b = new byte[s.Length];//s.Length类型为long
s.Read(b, 0, b.Length); //Read(byte[],int,int)
fs.Write(b, 0, b.Length);//Write(byte[],int,int)

参数类型都不一致吧?

 回复 引用 查看   
#10楼2010-06-29 09:39 | 斯克迪亚      
方法三是使用第三方工具拉,通常.Net混淆器都附带这样的功能,可以把多个dll整合到一个可执行文件中。
 回复 引用 查看   
#11楼2010-06-29 10:46 | PointNet      
方法不错,但这样做,是不是在编写代码时候就没有提示了呢
 回复 引用 查看   
#12楼2010-06-29 12:06 | euler      
顶!
 回复 引用 查看   
#13楼2010-06-29 12:08 | loveu0508      
我怎么看不明白呢 水平太差了,我只知道添加引用...
 回复 引用 查看   
#14楼[楼主]2010-06-29 12:44 | airwolf2026      
@Tony Chi
你说的是反射的方式哈.我这边的不缺失类型检测什么的哈.

 回复 引用 查看   
#15楼[楼主]2010-06-29 12:46 | airwolf2026      
@PointNet
有的呀.就是都保证有类型检测的.和平时一样的,
4楼同学的方法是没有的哈.

 回复 引用 查看   
#16楼2010-06-29 13:41 | longware      
如果dll嵌入的太多的话,exe文件体积会增大,会不会影响exe的启动和运行速度
 回复 引用 查看   
#17楼2010-06-29 13:54 | onlyugly      
为什么要把Dll嵌入到exe中,以后维护不方便,性能也不好,可读性差,
随便看那个程序里都是dll分开的。版本控制也方便些。


 回复 引用 查看   
#18楼2010-06-29 15:28 | 過期香煙      
这个方法不错

 回复 引用 查看   
#19楼[楼主]2010-06-29 19:12 | airwolf2026      
@onlyugly
就是本文开头说的小工具之类的小程序哈.为了方便分发

 回复 引用 查看   
#20楼[楼主]2010-06-29 21:13 | airwolf2026      
@周公
哈哈...b转换了呀.byte[] 的lenth是int

 回复 引用 查看   
#21楼2010-07-01 17:50 | 站在天空下的猪      
ilmerge 不支持中文目录,报9009错误
 回复 引用 查看   
#22楼[楼主]2010-07-01 21:42 | airwolf2026      
@站在天空下的猪
嗯,ilmerge还是有一些bug的哈.可以关注最新版.如果有需要的话.
另外俺开发的机器比较少取中文目录名哈.

 回复 引用 查看   
#23楼2010-07-03 08:57 | V_Lad@Microsoft      
请问楼主,如何将自己写的dll文件加入到.netFrameWork自带的文件夹下面,提供给程序引用?我试着直接将DLL文件拷贝到了那个文件夹,然后VS添加引用时,在。NET标签下面也能看到我的DLL,但是添加完引用之后,运行程序还是会报找不到那个DLL的错误,除非我在那个DLL属性里面设置拷贝到本地为True才可以,如何不用设置拷贝到当前BIn目录而被引用?谢谢
 回复 引用 查看   
#24楼[楼主]2010-07-03 10:37 | airwolf2026      
@V_Lad@Microsoft
你可以在代码里面调用这个函数AppDomainSetup .PrivateBinPath()
指定搜索路径.然后就可以了.

另外,你这个方法觉得不妥.你那个dll又不是.net 的,你放到那边做啥呢?

 回复 引用 查看   
#25楼2010-07-08 11:54 | wade black      
反射比文件流更好些
 回复 引用 查看   
#26楼2010-10-19 16:35 | 1号店      
Dill 放在bin文件夹里不就行了吗?
 回复 引用 查看   
#27楼[楼主]2010-10-19 20:18 | airwolf2026      
@1号店
呵呵.