Sign in to follow this  
Khaos Dragon

possible to embed dll inside c# app

Recommended Posts

ApochPiQ    23062
It is probably technically possible, but only with some extremely advanced and arcane trickery. In the end, it would amount to compacting the two files and extracting the DLL into a temporary file before it was loaded. DLLs are Dynamically Linked Libraries. They are designed to be separate from the main program code and loaded separately, so that multiple programs can share them, and only have to load them when needed.

What are you wanting to use such a combined file for? Perhaps there is an alternative solution?

Share this post


Link to post
Share on other sites
Khaos Dragon    196
The reason is because I am shipping my c# app with a dll I wrote (both in the same directory). While this works fine on my computer and many other computers, my app cannot find the dll on other computers. It appears that about half of computers can find the dll and half can't. I just figured that if it can't even find the dll when it is the same local directory, it would be forced to find it when its embedded but this won't seem to work.

Share this post


Link to post
Share on other sites
Khaos Dragon    196
Quote:
Original post by NoahAdler
If the .dll is a .NET assembly, you can certainly embed it as a resource, and then load it using Assembly.Load(byte[]), which just loads an assembly from a raw COFF image.

-bodisiw


This sounds like exactly what I want to do. I am not sure what to pass to the Assembly.Load method though.

Share this post


Link to post
Share on other sites
NoahAdler    290
A quick example:


class CoffLoader {
private static void OnAssemblyLoad(object o, AssemblyLoadEventArgs args) {
Console.WriteLine("Loaded assembly {0}", args.LoadedAssembly);
}

public static void Main() {
Stream stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("CoffAssembly.dll");
byte[] coffImage = new byte[stream.Length];
stream.Read(coffImage, 0, coffImage.Length);

AppDomain.CurrentDomain.AssemblyLoad += new AssemblyLoadEventHandler(OnAssemblyLoad);
Assembly coffAssembly = Assembly.Load(coffImage);
}
}




Just make sure you embed the assembly (called CoffAssembly.dll in the example above) as a resource when you compile, in addition to referencing it.

Hope this helps.

-bodisiw

Share this post


Link to post
Share on other sites
Khaos Dragon    196
Thank you so much Noah, the dll is fully statically embedded.
My friends who were having trouble before are not on so they can't test but I had one more question.

In order to be load an assembly from a resource, I had to strong name the assembly. Will it have to be strong named on my friends computers?

Share this post


Link to post
Share on other sites
NoahAdler    290
Quote:
Original post by Khaos Dragon
In order to be load an assembly from a resource, I had to strong name the assembly. Will it have to be strong named on my friends computers?


Strange, I haven't encountered this being necessary. In my test program I didn't bother to sign either the embedded assembly nor the executable itself, and it still seemed to work fine for me, so I can't really comment on you having to do that. However, since the assemblies are distributed by you anyway, if you give them strong names before giving them to your friends it shouldn't hurt anything, and my guess is they should run fine.

-bodisiw

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this