<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>dotnet thoughts &#187; ILDASM</title>
	<atom:link href="http://www.dotnetthoughts.net/tag/ildasm/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.dotnetthoughts.net</link>
	<description>a dotnet developer&#039;s technical blog</description>
	<lastBuildDate>Wed, 08 Feb 2012 03:18:03 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>ILMerge – A Brief Introduction</title>
		<link>http://www.dotnetthoughts.net/2010/12/09/ilmerge-a-brief-introduction/</link>
		<comments>http://www.dotnetthoughts.net/2010/12/09/ilmerge-a-brief-introduction/#comments</comments>
		<pubDate>Thu, 09 Dec 2010 08:12:06 +0000</pubDate>
		<dc:creator>Anuraj P</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[.Net 3.0 / 3.5]]></category>
		<category><![CDATA[Visual Studio]]></category>
		<category><![CDATA[Windows Forms]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[C#.Net]]></category>
		<category><![CDATA[ILDASM]]></category>
		<category><![CDATA[ILMerge]]></category>
		<category><![CDATA[MSIL]]></category>

		<guid isPermaLink="false">http://www.dotnetthoughts.net/?p=1251</guid>
		<description><![CDATA[Today I read about a nice utility called ILMerge from Microsoft Research, which helps to merge .net assemblies. Normally a .Net application contains various parts, like main assembly, and various referenced assemblies either from Bin folder or from GAC. When &#8230; <a href="http://www.dotnetthoughts.net/2010/12/09/ilmerge-a-brief-introduction/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Today I read about a nice utility called ILMerge from Microsoft Research, which helps to merge .net assemblies. Normally a .Net application contains various parts, like main assembly, and various referenced assemblies either from Bin folder or from GAC. When the application is running the runtime combines all the parts and runs the application. But sometimes it is helpful to combine all the parts of the application and deploy it as a single assembly.</p>
<p>You can get more information about the ILMerge utility from <a href="http://research.microsoft.com/en-us/people/mbarnett/ilmerge.aspx" target="_blank">here</a> and you can download it from <a href="http://www.microsoft.com/downloads/en/details.aspx?FamilyID=22914587-b4ad-4eae-87cf-b14ae6a939b0&#038;displaylang=en" target="_blank">here</a>.</p>
<p>In this post I am writing about embedding a .Net DLL in to an executable using ILMerge.</p>
<p>Here is the code for the executable</p>
<pre class="brush: csharp; title: ; notranslate">
using System;
public class MainClass
{
	static void Main(string[] args)
	{
		Helper.WriteMessage(&quot;Hello World&quot;);
	}
}
</pre>
<p>Here is the code for the library. I am using a static function from the library and using it.</p>
<pre class="brush: csharp; title: ; notranslate">
using System;
public static class Helper
{
	public static void WriteMessage(string message)
	{
		Console.WriteLine(message);
	}
}
</pre>
<p>I compiled the library first using this command</p>
<blockquote><p>csc /t:library helper.cs</p></blockquote>
<p>Then the executable using</p>
<blockquote><p>csc /r:helper.dll main.cs</p></blockquote>
<p>Now I try to deploy the main.exe (which is the output file) without the helper.dll it will throw some error like this</p>
<blockquote><p>Unhandled Exception: System.IO.FileNotFoundException: Could not load file or ass<br />
embly &#8216;helper, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null&#8217; or one of<br />
its dependencies. The system cannot find the file specified.</p></blockquote>
<p>Now I am merging these to assemblies using ILMerge using</p>
<blockquote><p>&#8220;c:\Program Files\Microsoft\ILMerge\ILMerge.exe&#8221; main.exe helper.dll /out:Sample.exe /t:exe</p></blockquote>
<p>It will generate an executable with filename as Sample.exe, and which can be deployed without helper.dll and will work fine. This is the ILDasm screenshots of two.</p>
<p><strong>IL before merging </strong></p>
<p><a href="http://www.dotnetthoughts.net/wp-content/uploads/2010/12/app_before_merge.bmp"><img src="http://www.dotnetthoughts.net/wp-content/uploads/2010/12/app_before_merge.bmp" alt="IL Code before Merge" title="IL Code before Merge" class="aligncenter size-full wp-image-1253" /></a></p>
<p><strong>IL after merging</strong></p>
<p><a href="http://www.dotnetthoughts.net/wp-content/uploads/2010/12/app_after_merge.bmp"><img src="http://www.dotnetthoughts.net/wp-content/uploads/2010/12/app_after_merge.bmp" alt="IL Code after Merging" title="IL Code after Merging" class="aligncenter size-full wp-image-1254" /></a></p>
<p>Happy Coding.</p>
<div class="betterrelated"><p><strong>Related content:</strong></p>
<ol><li> <a href="http://www.dotnetthoughts.net/2009/07/10/consuming-a-c-dll-in-c/" title="Permanent link to Consuming a C++ DLL in C#">Consuming a C++ DLL in C#</a>  </li>
<li> <a href="http://www.dotnetthoughts.net/2010/01/18/exploring-il-assembler/" title="Permanent link to Exploring IL Assembler">Exploring IL Assembler</a>  </li>
<li> <a href="http://www.dotnetthoughts.net/2012/02/01/the-application-has-failed-to-start-because-its-side-by-side-configuration-is-incorrect-please-see-the-application-event-log-or-use-the-command-line-sxstrace-exe-tool-for-more-detail/" title="Permanent link to The application has failed to start because its side-by-side configuration is incorrect. Please see the application event log or use the command-line sxstrace.exe tool for more detail">The application has failed to start because its side-by-side configuration is incorrect. Please see the application event log or use the command-line sxstrace.exe tool for more detail</a>  </li>
<li> <a href="http://www.dotnetthoughts.net/2009/10/17/application-development-using-gtk-in-net/" title="Permanent link to Application development using Gtk# in .Net">Application development using Gtk# in .Net</a>  </li>
<li> <a href="http://www.dotnetthoughts.net/2009/09/30/convert-image-to-icon-using-c/" title="Permanent link to Convert Image to Icon using C#">Convert Image to Icon using C#</a>  </li>
</ol></div>]]></content:encoded>
			<wfw:commentRss>http://www.dotnetthoughts.net/2010/12/09/ilmerge-a-brief-introduction/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Exploring IL Assembler</title>
		<link>http://www.dotnetthoughts.net/2010/01/18/exploring-il-assembler/</link>
		<comments>http://www.dotnetthoughts.net/2010/01/18/exploring-il-assembler/#comments</comments>
		<pubDate>Mon, 18 Jan 2010 17:56:20 +0000</pubDate>
		<dc:creator>Anuraj P</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[.Net 3.0 / 3.5]]></category>
		<category><![CDATA[Visual Studio]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[IL]]></category>
		<category><![CDATA[ILASM]]></category>
		<category><![CDATA[ILDASM]]></category>

		<guid isPermaLink="false">http://www.dotnetthoughts.net/?p=726</guid>
		<description><![CDATA[One of colleague once asked a question to me, like what is the risk by distributing the windows application without obfuscating it. The only problem I found is user can use some tools like .Net Reflector and explore our assemblies. &#8230; <a href="http://www.dotnetthoughts.net/2010/01/18/exploring-il-assembler/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>One of colleague once asked a question to me, like what is the risk by distributing the windows application without obfuscating it. The only problem I found is user can use some tools like .Net Reflector and explore our assemblies. But few days before I found some nice .Net Framework tools, ildasm.exe and ilasm.exe, IL Disassembler and IL Assembler respectively. These tools are available with .Net Framework SDKs. These tools can used to generate IL code for .Net assemblies and re-create assemblies from IL code. You can achieve it in code via .Net Reflection.Emit namespace. (In Community Techdays @ Cochin, one session is on Reflection.Emit. Don&#8217;t miss it.) As it is a vast topic I am only explaining basics <img src='http://www.dotnetthoughts.net/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
<p><strong>Generate IL code using ILDASM.exe</strong></p>
<ol>
<li>First create a HelloWorld.cs and compile it to HelloWorld.exe</li>
<pre class="brush: csharp; title: ; notranslate">
using System;

public class HelloWorld
{
static int Main(string[] args)
	{
Console.WriteLine(&quot;Hello World&quot;);
		return(0);
	}
}
</pre>
<li>Use <code>csc.exe</code>to compile the cs file to exe. &#8211; <code>csc HelloWorld.cs</code></li>
<li>Use <code>ildasm.exe HelloWorld.Exe</code> to view the IL Code. You can find ILDASM in the Microsoft .Net SDK Path
<p><div id="attachment_724" class="wp-caption alignnone" style="width: 310px"><a href="http://www.dotnetthoughts.net/wp-content/uploads/2010/01/ildasm.png"><img class="size-medium wp-image-724" title="ILDASM - Exploring HelloWorld.exe " src="http://www.dotnetthoughts.net/wp-content/uploads/2010/01/ildasm-300x285.png" alt="ILDASM - Exploring HelloWorld.exe " width="300" height="285" /></a><p class="wp-caption-text">ILDASM - Exploring HelloWorld.exe </p></div></li>
<li>To generate the IL code use Select Dump option from File Menu or Press Ctrl+D. It will asks for a location to save the IL code.
<p><div id="attachment_722" class="wp-caption alignnone" style="width: 208px"><a href="http://www.dotnetthoughts.net/wp-content/uploads/2010/01/IL_Code1.png"><img class="size-full wp-image-722" title="IL Code Generated" src="http://www.dotnetthoughts.net/wp-content/uploads/2010/01/IL_Code1.png" alt="IL Code Generated" width="198" height="108" /></a><p class="wp-caption-text">IL Code Generated</p></div></li>
<li>Use any Editor to modify the IL code. You can find all the string values ( &#8220;Hello World&#8221; in this example ) as it is in the IL code. Modify it.( I am modifying it as &#8220;Hello World from IL Code&#8221;).
<p><div id="attachment_725" class="wp-caption alignnone" style="width: 310px"><a href="http://www.dotnetthoughts.net/wp-content/uploads/2010/01/Notepad_IL.png"><img class="size-medium wp-image-725" title="IL Code - Notepad" src="http://www.dotnetthoughts.net/wp-content/uploads/2010/01/Notepad_IL-300x150.png" alt="IL Code - Notepad" width="300" height="150" /></a><p class="wp-caption-text">IL Code - Notepad</p></div></li>
</ol>
<p>So we created the IL code and modified.<br />
<strong>Generate Assembly from IL code using ILASM.exe</strong></p>
<ol>
<li>Invoke the ILASM.exe with IL file as the parameter. <code>ilasm helloworld_il.il</code></li>
<li>It will show some details about assembling the IL code to assembly. And if the operation is successful you will get an exe in the location with HelloWorld_IL.exe.
<p><div id="attachment_723" class="wp-caption alignnone" style="width: 310px"><a href="http://www.dotnetthoughts.net/wp-content/uploads/2010/01/IL_Code3.png"><img class="size-medium wp-image-723" title="Assembling the IL code to EXE" src="http://www.dotnetthoughts.net/wp-content/uploads/2010/01/IL_Code3-300x151.png" alt="Assembling the IL code to EXE" width="300" height="151" /></a><p class="wp-caption-text">Assembling the IL code to EXE</p></div></li>
<li>If you invoke the exe from command prompt it will display the modified string instead of Hello World.(In this example &#8220;Hello World from IL Code&#8221;).</li>
</ol>
<p>You can do more if you know how IL works. Happy IL Programming <img src='http://www.dotnetthoughts.net/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
<div class="betterrelated"><p><strong>Related content:</strong></p>
<ol><li> <a href="http://www.dotnetthoughts.net/2010/12/09/ilmerge-a-brief-introduction/" title="Permanent link to ILMerge – A Brief Introduction">ILMerge – A Brief Introduction</a>  </li>
<li> <a href="http://www.dotnetthoughts.net/2011/06/02/how-to-integrate-fxcop-to-visual-studio-2010-professional/" title="Permanent link to How to integrate FxCop to Visual Studio 2010 Professional">How to integrate FxCop to Visual Studio 2010 Professional</a>  </li>
<li> <a href="http://www.dotnetthoughts.net/2011/09/25/getting-code-coverage-using-open-cover-and-nunit/" title="Permanent link to Getting Code coverage using Open Cover and NUnit">Getting Code coverage using Open Cover and NUnit</a>  </li>
<li> <a href="http://www.dotnetthoughts.net/2009/07/10/consuming-a-c-dll-in-c/" title="Permanent link to Consuming a C++ DLL in C#">Consuming a C++ DLL in C#</a>  </li>
<li> <a href="http://www.dotnetthoughts.net/2012/02/01/the-application-has-failed-to-start-because-its-side-by-side-configuration-is-incorrect-please-see-the-application-event-log-or-use-the-command-line-sxstrace-exe-tool-for-more-detail/" title="Permanent link to The application has failed to start because its side-by-side configuration is incorrect. Please see the application event log or use the command-line sxstrace.exe tool for more detail">The application has failed to start because its side-by-side configuration is incorrect. Please see the application event log or use the command-line sxstrace.exe tool for more detail</a>  </li>
</ol></div>]]></content:encoded>
			<wfw:commentRss>http://www.dotnetthoughts.net/2010/01/18/exploring-il-assembler/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

