Skip to main content
GameDev.net gamedev.net
🔒 Locked

possible to embed dll inside c# app

Started by Khaos Dragon Jun 28, 2005 at 5:42 PM 10 replies 6.6k views
Original Post
Khaos Dragon
Khaos Dragon
Yes I am wondering if it possible to combine a dll and a c# executable into one file.
ApochPiQ
ApochPiQ
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?
Khaos Dragon
Khaos Dragon
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.
Del Snd of Thndr
Del Snd of Thndr
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
Khaos Dragon
Khaos Dragon
It is a CLI/C++ wrapper around a large unmanaged c++ library.
outRider
outRider
Are you sure it's not because they don't have .Net installed?
Khaos Dragon
Khaos Dragon
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.
NoahAdler
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
-bodisiw
Khaos Dragon
Khaos Dragon
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.

NoahAdler
NoahAdler
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
Khaos Dragon
Khaos Dragon
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?
NoahAdler
NoahAdler
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
-bodisiw

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.