• Advertisement
Sign in to follow this  

possible to embed dll inside c# app

This topic is 4622 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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?

Share this post


Link to post
Share on other sites
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
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!

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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
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
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
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
Sign in to follow this  

  • Advertisement