Possible to merge a DLL into a .NET EXE? [duplicate]
Asked Answered
I

3

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)

Imperium answered 8/1, 2011 at 0:31 Comment(1)
Changed the title as DLL injection is an overloaded term.Slater
G
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.

Gert answered 8/1, 2011 at 0:33 Comment(0)
S
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.

Slater answered 8/1, 2011 at 0:44 Comment(4)
why would one ever do this over using ILMerge?Limann
@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.Slater
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.Limann
@Pauli Yes it's a nice trick - now vote me up! :)Slater
J
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
}
Jobi answered 28/3, 2012 at 22:21 Comment(4)
where does the output of the Eazfuscator lie?Cybele
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.Jobi
@Jobi 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)Ander
I have nothing to do with eazfuscator. Also, I made this post before they started charging for it.Jobi

© 2022 - 2024 — McMap. All rights reserved.