Lars Holm Jensen's Code Blog

Just another WordPress codeblog on C#, Silverlight and all things .NET

Embed managed dlls easily in a .NET assembly

with 15 comments

Here’s a very quick and dirty way to include managed dlls in your .exe-file. Just right-click your project in Visual Studio, choose Project Properties -> Resources -> Add Resource -> Add Existing File

Add all your dependencies and finally include the code below in your App.xaml.cs or equivalent.. everything else is taken care of.

public App()
{
    AppDomain.CurrentDomain.AssemblyResolve +=new ResolveEventHandler(CurrentDomain_AssemblyResolve);
}

System.Reflection.Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
{
    string dllName = args.Name.Contains(',') ? args.Name.Substring(0, args.Name.IndexOf(',')) : args.Name.Replace(".dll","");

    dllName = dllName.Replace(".", "_");

    if (dllName.EndsWith("_resources")) return null;

    System.Resources.ResourceManager rm = new System.Resources.ResourceManager(GetType().Namespace + ".Properties.Resources", System.Reflection.Assembly.GetExecutingAssembly());

    byte[] bytes = (byte[])rm.GetObject(dllName);

    return System.Reflection.Assembly.Load(bytes);
}

The code is a little bit more complicated than need be. But I wanted to avoid using a named namespace in order to keep the code copy-paste-ready. By the way LINQPad uses a similar method, though Joe Albahari has went a step further and encrypted his resources.

Written by larsholm

June 15th, 2011 at 8:10 pm

Posted in .NET

Tagged with , , , , , ,

15 Responses to 'Embed managed dlls easily in a .NET assembly'

Subscribe to comments with RSS or TrackBack to 'Embed managed dlls easily in a .NET assembly'.

  1. Hello Larsholm,

    I do not understand how much. Could You spend more details?

    I have a dll C# that use another C++/CLI, how to use it with your code?

    thanks,

    Cobaia

    21 Jul 11 at 4:12 am

  2. Does only work for managed dlls, not for native c dlls unfortunately.

    Frank

    26 Jul 11 at 12:15 pm

  3. You’re right. I didn’t test this with native dlls and it does indeed not work. I will fix my post.

    larsholm

    26 Jul 11 at 9:27 pm

  4. [quote]App.xaml.cs or equivalent[/quote]
    what could be the equivalent?

    Naim ZARD

    28 Jul 11 at 10:25 am

  5. One of your constructors that are called before you use the external library. Or you can use your Main method in a Console or Forms application too, but then you have to modify the code above because the Main method is static.
    GetType().Namespace + “.Properties.Resources” will not work. It would have to be changed to “[YourNamespace].Properties.Resources”

    larsholm

    28 Jul 11 at 5:55 pm

  6. Thank you. I have written a WPF application which requires that I deploy with MySql.Data.dll. I am a bit of a minimalist and prefer the idea of having a single executable out there rather than the exe and associated 3rd party libs.

    This worked very well.

    AshRowe

    21 Aug 11 at 2:21 pm

  7. Correction, it works very well unless you are running a custom theme in Windows 7…. then it tries to resolve the PresentationFramework.Theme assembly, crashed and dies haha. Solution, take the stupid theme off, however this leads me to wonder, is there a way using the above method to resolve only a required dll, rather than every time we think we need to find a dll, call this handler?
    I’ll stop spamming your blog now :|

    AshRowe

    21 Aug 11 at 2:41 pm

  8. I’m so silly, sorry please ignore by above rants. If I’d actually taken two seconds to try and resolve the issue myself, I would have been fine :)

    It’s dirty, but by checking if the strDllName contains “PresentationFramework” and returning null if it does, I get past my issue. Thanks once again for this useful solution mate. Now I promise to stop spamming your blog.

    AshRowe

    22 Aug 11 at 2:09 pm

  9. No problem and thanks for sharing your solution.

    larsholm

    22 Aug 11 at 6:54 pm

  10. Can you please tell me how to use this technique with Windows Forms?

    Frenky

    29 Nov 11 at 2:56 pm

  11. I tried this with a console application and it doesn’t work. All of my references to classes in the dlls will not resolve. The code doesn’t compile.

    Josh

    1 Dec 11 at 10:38 pm

  12. @Josh: At compile time your project needs to have a reference to the dll.

    @Frenky: See my second comment on this page.

    larsholm

    1 Dec 11 at 11:45 pm

  13. “@Josh: At compile time your project needs to have a reference to the dll.”

    What do you mean? How do I reference it from a resource?

    Jason

    21 Jan 12 at 11:38 pm

  14. You dont need to reference the copy in the resources, just add a reference to any copy of the dll as you normally would.

    larsholm

    22 Jan 12 at 2:39 am

  15. Hi,
    Does this work for console app? I tried doing this for my console app, but when I still run the .exe file, it still throws an error, which on debug says ‘Could not load file , or assembly ‘xxxx’ …..’.

    I had added the dll as a reference to my project and also added it to my resources.

    Any suggestions?

    cheerios121

    6 Feb 12 at 7:17 pm

Leave a Reply

Spam Protection by WP-SpamFree