possible to embed dll inside c# app

Started by
10 comments, last by NoahAdler 18 years, 9 months ago
Yes I am wondering if it possible to combine a dll and a c# executable into one file.
Advertisement
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?

Wielder of the Sacred Wands
[Work - ArenaNet] [Epoch Language] [Scribblings]

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.
If you wrote the DLL, why not just incorporate the code in the executable? Granted the executable will be larger, but you won't have this headache.

HTH!
~del
It is a CLI/C++ wrapper around a large unmanaged c++ library.
Are you sure it's not because they don't have .Net installed?
Yes I have tested with a couple friends, they can run my app when I remove the reference to my dll, but not when I do include it.
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
-bodisiw
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.

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
-bodisiw

This topic is closed to new replies.

Advertisement