6

I have a DLL that stores classes common to two applications. I'd like to keep my application limited to one EXE file and would like to see if I can somehow embed this DLL within my main EXE.

How can I embed the external DLL into my application? (if possible)

Tim Lloyd
  • 36,374
  • 9
  • 92
  • 127
halfbit
  • 54,462
  • 46
  • 195
  • 426

3 Answers3

15

http://www.microsoft.com/downloads/en/details.aspx?FamilyID=22914587-b4ad-4eae-87cf-b14ae6a939b0&displaylang=en

ILMerge is a utility for merging multiple .NET assemblies into a single .NET assembly. It works on executables and dlls alike. It comes with several options for controlling the processing and format of the output, see the accompanying documentation for details.

Pauli Østerø
  • 6,728
  • 1
  • 28
  • 48
9

An alternative to ILMerge is to embed dependent assemblies into the executable as embedded resouces and leverage the assembly resolve mechanism to load them as resource streams. An example of how to do this can be found here:

http://blog.magenic.com/blogs/brante/archive/2008/04/14/Embedded-Assembly-Linker.aspx

I use this pattern myself which works well. ILMerge sometimes has issues so your milage may vary.

Tim Lloyd
  • 36,374
  • 9
  • 92
  • 127
  • why would one ever do this over using ILMerge? – Pauli Østerø Jan 08 '11 at 00:50
  • 1
    @Pauli As I mentioned ILMerge has issues with some scenarios e.g. it struggles with some sorts of obfuscation (we have obfuscated assemblies), it has trouble with WPF Xaml resources, it has trouble merging multiple WPF assemblies as the Xaml compiler places a helper type in each assembly with the same name in the same namespace which causes ILMerge to puke on duplicate types. These are just the ones I have personally across. – Tim Lloyd Jan 08 '11 at 00:52
  • its true that having types with the same full name (namespace + typename) gives trouble... i didn't know it was allowed to load several assemblies containing types with the same full name into one AppDomain like this either? its a nice trick though, allowing to have assemblies stored in a database or downloaded over the internet. – Pauli Østerø Jan 08 '11 at 01:00
  • @Pauli Yes it's a nice trick - now vote me up! :) – Tim Lloyd Jan 08 '11 at 01:01
1

Eazfuscator.NET is a wonderful tool that also allows both dll merging AND embedding, as well as it's normal obfuscation functions. It also does some neat optimizing on obfuscated code. Instead of messing with ilmerge you just add one class annotation and eazfuscator will do everything for you. It's wonderful!

[assembly: System.Reflection.Obfuscation(Feature = "encrypt symbol names with password PAS$", Exclude = false)]
[assembly: System.Reflection.Obfuscation(Feature = "embed nLog.dll", Exclude = false)]
public class MyClass {
  //blah
}
ogggre
  • 1,895
  • 18
  • 18
scaryman
  • 1,812
  • 1
  • 19
  • 30
  • 1
    where does the output of the Eazfuscator lie? – Vikneshwar Oct 10 '12 at 11:13
  • 1
    Eazfuscator, when integrated with visual studio, just operates in-place on the output of the compiler. So whatever is in your bin/Release folder. You can also script it manually with msbuild if you want to do something customizable. – scaryman Oct 11 '12 at 15:50
  • @scaryman Can you disclose presence/absence of affiliation when using 'wonderful' please? (Not saying it's anything but that -- absence of such an affiliation would make it go up in my estimation *as would disclosure*) – Ruben Bartelink Sep 16 '13 at 12:20
  • I have nothing to do with eazfuscator. Also, I made this post before they started charging for it. – scaryman Sep 16 '13 at 15:55