<?xml version="1.0" encoding="utf-8"?><feed xmlns="http://www.w3.org/2005/Atom"><title type="text">博客园_airwolf2026</title><subtitle type="text"/><id>http://feed.cnblogs.com/blog/u/31241/rss</id><updated>2011-10-16T02:49:43Z</updated><author><name>airwolf2026</name><uri>http://www.cnblogs.com/airwolf2026/</uri></author><generator>feed.cnblogs.com</generator><link rel="alternate" type="text/html" href="http://www.cnblogs.com/airwolf2026/"/><link rel="self" type="application/atom+xml" href="http://feed.cnblogs.com/blog/u/31241/rss"/><entry><id>http://www.cnblogs.com/airwolf2026/archive/2010/06/28/1767058.html</id><title type="text">嵌入Dll 到.net 程序中的方法</title><summary type="text">我们经常会写一些小程序给自己或者他人用,而这些程序时长又会去引用一些第三方的Dll,比如开源的ICSharpCode.SharpZipLib.dll等,为了让程序保持整洁,或者给对方的时候方便,就想把...</summary><published>2010-06-28T14:34:00Z</published><updated>2010-06-28T14:34:00Z</updated><author><name>airwolf2026</name><uri>http://www.cnblogs.com/airwolf2026/</uri></author><link rel="alternate" href="http://www.cnblogs.com/airwolf2026/archive/2010/06/28/1767058.html"/><link rel="alternate" type="text/html" href="http://www.cnblogs.com/airwolf2026/archive/2010/06/28/1767058.html"/><content type="html">&lt;p&gt;&lt;strong&gt;我们经常会写一些小程序给自己或者他人用,而这些程序时长又会去引用一些第三方的Dll,比如开源的ICSharpCode.SharpZipLib.dll等,为了让程序保持整洁,或者给对方的时候方便,就想把这些dll给嵌入到EXE中去,这样在不打包的情况下,只要丢一个文件给对方就能用了.最近研究了下可行性,目前有如下两种方法:&lt;/strong&gt;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;ol&gt;&lt;li&gt;&lt;p&gt;&lt;strong&gt;方法1：把相关的第三方dll作为程序资源嵌入到EXE中,在程序运行的时候,从资源文件中输出到程序执行目录即可 &lt;/strong&gt;&lt;/p&gt;&lt;p&gt;&lt;a href="http://images.cnblogs.com/cnblogs_com/airwolf2026/WindowsLiveWriter/Dll.net_12E7B/image_2.png"&gt;&lt;img height="264" width="289" src="http://images.cnblogs.com/cnblogs_com/airwolf2026/WindowsLiveWriter/Dll.net_12E7B/image_thumb.png" alt="image" border="0" title="image" style="margin: 10px auto 0px; display: block; float: none; border-width: 0px;" /&gt;&lt;/a&gt;(图1：示例项目,ThirdPartydlldemo.dll作为第三方资源.Build Action属性设置为" Embedded Resource")&lt;/p&gt;&lt;p&gt;然后在Program.cs里面声明个静态构造函数,在该方法里面把第三方dll输出到程序目录,这样在调用第三方dll方法的时候,相关环境已经初始化完毕了.&lt;/p&gt;&lt;div id="codeSnippetWrapper"&gt;&lt;div id="codeSnippet" style="text-align: left; line-height: 12pt; background-color: #f4f4f4; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"&gt;&lt;span id="lnum1" style="color: #606060;"&gt;   1:&lt;/span&gt; &lt;span style="color: #0000ff;"&gt;private&lt;/span&gt; &lt;span style="color: #0000ff;"&gt;static&lt;/span&gt; &lt;span style="color: #0000ff;"&gt;void&lt;/span&gt; ExtractResourceToFile(&lt;span style="color: #0000ff;"&gt;string&lt;/span&gt; resourceName, &lt;span style="color: #0000ff;"&gt;string&lt;/span&gt; filename)&lt;!--CRLF--&gt;&lt;span id="lnum2" style="color: #606060;"&gt;   2:&lt;/span&gt;    {&lt;!--CRLF--&gt;&lt;span id="lnum3" style="color: #606060;"&gt;   3:&lt;/span&gt;          &lt;span style="color: #0000ff;"&gt;if&lt;/span&gt; (!System.IO.File.Exists(filename))&lt;!--CRLF--&gt;&lt;span id="lnum4" style="color: #606060;"&gt;   4:&lt;/span&gt;             &lt;span style="color: #0000ff;"&gt;using&lt;/span&gt; (System.IO.Stream s = System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream(resourceName))&lt;!--CRLF--&gt;&lt;span id="lnum5" style="color: #606060;"&gt;   5:&lt;/span&gt;             &lt;span style="color: #0000ff;"&gt;usng&lt;/span&gt; (System.IO.FileStream fs = &lt;span style="color: #0000ff;"&gt;new&lt;/span&gt; System.IO.FileStream(filename, System.IO.FileMode.Create))&lt;!--CRLF--&gt;&lt;span id="lnum6" style="color: #606060;"&gt;   6:&lt;/span&gt;             {&lt;!--CRLF--&gt;&lt;span id="lnum7" style="color: #606060;"&gt;   7:&lt;/span&gt;                    &lt;span style="color: #0000ff;"&gt;byte&lt;/span&gt;[] b = &lt;span style="color: #0000ff;"&gt;new&lt;/span&gt; &lt;span style="color: #0000ff;"&gt;byte&lt;/span&gt;[s.Length];&lt;!--CRLF--&gt;&lt;span id="lnum8" style="color: #606060;"&gt;   8:&lt;/span&gt;                    s.Read(b, 0, b.Length);&lt;!--CRLF--&gt;&lt;span id="lnum9" style="color: #606060;"&gt;   9:&lt;/span&gt;                    fs.Write(b, 0, b.Length);&lt;!--CRLF--&gt;&lt;span id="lnum10" style="color: #606060;"&gt;  10:&lt;/span&gt;              }&lt;!--CRLF--&gt;&lt;span id="lnum11" style="color: #606060;"&gt;  11:&lt;/span&gt;     }&lt;!--CRLF--&gt;&lt;/div&gt;&lt;/div&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;div id="codeSnippetWrapper"&gt;&lt;div id="codeSnippet" style="text-align: left; line-height: 12pt; background-color: #f4f4f4; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"&gt;&lt;span id="lnum1" style="color: #606060;"&gt;   1:&lt;/span&gt; &lt;span style="color: #0000ff;"&gt;static&lt;/span&gt; Program()&lt;!--CRLF--&gt;&lt;span id="lnum2" style="color: #606060;"&gt;   2:&lt;/span&gt;       {&lt;!--CRLF--&gt;&lt;span id="lnum3" style="color: #606060;"&gt;   3:&lt;/span&gt;           ExtractResourceToFile(&lt;span style="color: #006080;"&gt;"EmbeddedDLL2ExeDemo.ThirdPartyDllDemo.dll"&lt;/span&gt;,&lt;!--CRLF--&gt;&lt;span id="lnum4" style="color: #606060;"&gt;   4:&lt;/span&gt;               &lt;span style="color: #006080;"&gt;"ThirdPartyDllDemo.dll"&lt;/span&gt;);&lt;!--CRLF--&gt;&lt;span id="lnum5" style="color: #606060;"&gt;   5:&lt;/span&gt;       }&lt;!--CRLF--&gt;&lt;/div&gt;&lt;/div&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p&gt;这样就ok了.&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;/li&gt;&lt;li&gt;&lt;p&gt;&lt;strong&gt;方法2：是用&lt;a target="_blank" href="http://www.microsoft.com/downloads/details.aspx?familyid=22914587-b4ad-4eae-87cf-b14ae6a939b0&amp;amp;displaylang=en"&gt;Ilmerge&lt;/a&gt;这个微软提供的工具,直接把相关的dll嵌入到目标exe中,而且程序运行时候,不像方法1会把相关的dll输出到可执行目录下,它直接让.net运行时到程序的资源中去找相关的dll引用,以下是&lt;a target="_blank" href="http://www.microsoft.com/downloads/details.aspx?familyid=22914587-b4ad-4eae-87cf-b14ae6a939b0&amp;amp;displaylang=en"&gt;Ilmerge&lt;/a&gt;的介绍: &lt;/strong&gt;&lt;/p&gt;&lt;blockquote style='border:2px solid #EFEFEF;color:#333333;padding:5px 10px;'&gt;&lt;p&gt;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.&lt;/p&gt;&lt;p&gt;ILMerge is packaged as a console application. But all of its functionality is also accessible programmatically. Note that Visual Studio &lt;strong&gt;does&lt;/strong&gt; allow one to add an executable as a reference, so you can write a client that uses ILMerge as a library.&lt;/p&gt;&lt;p&gt;ILMerge takes a set of &lt;i&gt;input assemblies&lt;/i&gt; and merges them into one &lt;i&gt;target assembly&lt;/i&gt;. The first assembly in the list of input assemblies is the &lt;i&gt;primary assembly&lt;/i&gt;. 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.&lt;/p&gt;&lt;p&gt;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.&lt;/p&gt;&lt;p&gt;Any Win32 Resources in the primary assembly are copied over into the target assembly.&lt;/p&gt;&lt;p&gt;There are many options that control the behavior of ILMerge. These are described in the next section.&lt;/p&gt;&lt;/blockquote&gt;&lt;p&gt;&lt;a target="_blank" href="http://www.microsoft.com/downloads/details.aspx?familyid=22914587-b4ad-4eae-87cf-b14ae6a939b0&amp;amp;displaylang=en"&gt;Ilmerge&lt;/a&gt; 相关的命令行参数是:&lt;/p&gt;&lt;blockquote style='border:2px solid #EFEFEF;color:#333333;padding:5px 10px;'&gt;&lt;p&gt;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:&amp;lt;version&amp;gt;[,&amp;lt;platformdir&amp;gt;]]|v1|v1.1|v2|v4) [/useFullPublicKeyForReferences] [/zeroPeKind] [/wildcards] [/allowDup[:typename]]* [/allowDuplicateResources] [/union] [/align:n] /&lt;strong&gt;out&lt;/strong&gt;:filename &amp;lt;primary assembly&amp;gt; [&amp;lt;other assemblies&amp;gt;...]&lt;/p&gt;&lt;/blockquote&gt;&lt;p&gt;其中目标exe或者程序集,要放在输入的程序集里面的第一位置,其他dll放在它之后.其中/out:参数是必须的,其他参数可以参考文档&lt;/p&gt;&lt;p&gt;如图1 示例,命令行参数是 &lt;/p&gt;&lt;p&gt;ilmerge EmbeddedDLL2ExeDemo.exe ThirdPartyDllDemo.dll /ndebug /out:EmbeddedDll2Ex       &lt;br /&gt;eDemo.exe&lt;/p&gt;&lt;p&gt;这样既可,该方法比方法1更完美,不过这个&lt;a target="_blank" href="http://www.microsoft.com/downloads/details.aspx?familyid=22914587-b4ad-4eae-87cf-b14ae6a939b0&amp;amp;displaylang=en"&gt;Ilmerge&lt;/a&gt; 在使用的时候还有一些不足的地方,比如/ver:version,这个参数设置后没有效果;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p&gt;&lt;strong&gt;ilmerge就用自己把它引用到的两个dll嵌软到它自身里面了.&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;&lt;/ol&gt;&lt;p&gt;&lt;strong&gt;方法3：使用.Net混淆器都附带这样的功能，可以把多个dll整合到一个可执行文件中。 感谢&amp;nbsp;&lt;a id="Comments1_CommentList_ctl09_NameLink" href="http://www.cnblogs.com/SkyD/" target="_blank" style="color: #666666; text-decoration: none; font-weight: normal; padding: 0px; margin: 0px;"&gt;斯克迪亚&lt;/a&gt;&amp;nbsp;提供&lt;/strong&gt;&lt;/p&gt;&lt;img src="http://www.cnblogs.com/airwolf2026/aggbug/1767058.html?type=1" width="1" height="1" alt=""/&gt;&lt;p&gt;&lt;a href="http://www.cnblogs.com/airwolf2026/archive/2010/06/28/1767058.html" target="_blank"&gt;本文链接&lt;/a&gt;&lt;/p&gt;</content></entry><entry><id>http://www.cnblogs.com/airwolf2026/archive/2009/02/05/1384725.html</id><title type="text">基于Armv4I WINCE 5.0 应用程序开发环境搭建(C# VS2005或者VS2008)</title><summary type="text">这里说的开发环境是指:VS2005或者2008这样的IDE,开发语言C#,也就是在WINCE上进行.NET CF开发 主要分为两步: a.准备必要的目标平台SDK;针对目标硬件平台通过Platform</summary><published>2009-02-05T08:36:00Z</published><updated>2009-02-05T08:36:00Z</updated><author><name>airwolf2026</name><uri>http://www.cnblogs.com/airwolf2026/</uri></author><link rel="alternate" href="http://www.cnblogs.com/airwolf2026/archive/2009/02/05/1384725.html"/><link rel="alternate" type="text/html" href="http://www.cnblogs.com/airwolf2026/archive/2009/02/05/1384725.html"/><content type="text">这里说的开发环境是指:VS2005或者2008这样的IDE,开发语言C#,也就是在WINCE上进行.NET CF开发 主要分为两步: a.准备必要的目标平台SDK;针对目标硬件平台通过Platform</content></entry><entry><id>http://www.cnblogs.com/airwolf2026/archive/2008/09/17/FZBusLineSpider.html</id><title type="text">福州公交数据采集器含源码(可为'公交查询''都市行'等手机软件提供数据)</title><summary type="text">  近来玩手机的一些公交查询软件的时候,发现它们都存在城市公交数据不足,或者数据很少的情况,比如'来电通'作者的'公交查询'城市就比较少,主要由第三方个人维护;'都市行'虽然有福州公交 数据,但是很多路数据都没有,比如没有606,55等这些大学城线路.现在提供一个俺自己写的,供有需要的个人(手机玩家)或者软件作者使用</summary><published>2008-09-17T01:52:00Z</published><updated>2008-09-17T01:52:00Z</updated><author><name>airwolf2026</name><uri>http://www.cnblogs.com/airwolf2026/</uri></author><link rel="alternate" href="http://www.cnblogs.com/airwolf2026/archive/2008/09/17/FZBusLineSpider.html"/><link rel="alternate" type="text/html" href="http://www.cnblogs.com/airwolf2026/archive/2008/09/17/FZBusLineSpider.html"/><content type="text">  近来玩手机的一些公交查询软件的时候,发现它们都存在城市公交数据不足,或者数据很少的情况,比如'来电通'作者的'公交查询'城市就比较少,主要由第三方个人维护;'都市行'虽然有福州公交 数据,但是很多路数据都没有,比如没有606,55等这些大学城线路.现在提供一个俺自己写的,供有需要的个人(手机玩家)或者软件作者使用</content></entry><entry><id>http://www.cnblogs.com/airwolf2026/archive/2008/04/28/1175102.html</id><title type="text">在连接到SQL Server2005时,在默认的设置下SQL Server不允许进行远程连接可能会导致此失败--的解决</title><summary type="text">   "在连接到SQL Server2005时,在默认的设置下SQL Server不允许进行远程连接可能会导致此失败"相信大家刚接触SQL Server2005的时候,可能就遇到这样的问题,下面就是解决方法.Sql Server2008还没有见过,不知道是不是这样设置</summary><published>2008-04-28T11:59:00Z</published><updated>2008-04-28T11:59:00Z</updated><author><name>airwolf2026</name><uri>http://www.cnblogs.com/airwolf2026/</uri></author><link rel="alternate" href="http://www.cnblogs.com/airwolf2026/archive/2008/04/28/1175102.html"/><link rel="alternate" type="text/html" href="http://www.cnblogs.com/airwolf2026/archive/2008/04/28/1175102.html"/><content type="text">   "在连接到SQL Server2005时,在默认的设置下SQL Server不允许进行远程连接可能会导致此失败"相信大家刚接触SQL Server2005的时候,可能就遇到这样的问题,下面就是解决方法.Sql Server2008还没有见过,不知道是不是这样设置</content></entry></feed>
