Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Embedding one dll inside another as an embedded resource and then calling it from my code

Tags:

c#

.net-3.5

dll

I've got a situation where I have a DLL I'm creating that uses another third party DLL, but I would prefer to be able to build the third party DLL into my DLL instead of having to keep them both together if possible.

This with is C# and .NET 3.5.

The way I would like to do this is by storing the third party DLL as an embedded resource which I then place in the appropriate place during execution of the first DLL.

The way I originally planned to do this is by writing code to put the third party DLL in the location specified by System.Reflection.Assembly.GetExecutingAssembly().Location.ToString() minus the last /nameOfMyAssembly.dll. I can successfully save the third party .DLL in this location (which ends up being

C:\Documents and Settings\myUserName\Local Settings\Application Data\assembly\dl3\KXPPAX6Y.ZCY\A1MZ1499.1TR\e0115d44\91bb86eb_fe18c901

), but when I get to the part of my code requiring this DLL, it can't find it.

Does anybody have any idea as to what I need to be doing differently?

like image 443
Lawrence Johnston Avatar asked Sep 18 '08 20:09

Lawrence Johnston


People also ask

How do I insert a DLL into another DLL?

Add DLL As Embedded Resource First, add the DLL as Reference. Then, add the same DLL as file into the project. Right click the project's name > Add > Existing Item... The same DLL will exist twice in different folder in the project.

What is DLL in Embedded?

a .exe formatted file. Normally the code for a microcontroller or an embedded processor would be self-contained and stand-alone. A DLL is a shared library and to use it requires runtime intervention.

Can a DLL contain another DLL?

You can certainly embed another DLL as a resource and extract and load it at runtime if that's what you need.

Can we get source code from DLL?

Press Ctrl + O and select your DLL File. Dll will be shown in left pane. Right click on Dll and select Export Source Code.


1 Answers

Once you've embedded the third-party assembly as a resource, add code to subscribe to the AppDomain.AssemblyResolve event of the current domain during application start-up. This event fires whenever the Fusion sub-system of the CLR fails to locate an assembly according to the probing (policies) in effect. In the event handler for AppDomain.AssemblyResolve, load the resource using Assembly.GetManifestResourceStream and feed its content as a byte array into the corresponding Assembly.Load overload. Below is how one such implementation could look like in C#:

AppDomain.CurrentDomain.AssemblyResolve += (sender, args) => {     var resName = args.Name + ".dll";         var thisAssembly = Assembly.GetExecutingAssembly();         using (var input = thisAssembly.GetManifestResourceStream(resName))     {         return input != null               ? Assembly.Load(StreamToBytes(input))              : null;     } }; 

where StreamToBytes could be defined as:

static byte[] StreamToBytes(Stream input)  {     var capacity = input.CanSeek ? (int) input.Length : 0;     using (var output = new MemoryStream(capacity))     {         int readLength;         var buffer = new byte[4096];          do         {             readLength = input.Read(buffer, 0, buffer.Length);             output.Write(buffer, 0, readLength);         }         while (readLength != 0);          return output.ToArray();     } } 

Finally, as a few have already mentioned, ILMerge may be another option to consider, albeit somewhat more involved.

like image 65
Atif Aziz Avatar answered Nov 09 '22 21:11

Atif Aziz