Embedding Dlls in a Compiled Executable

Embedding DLLs in a compiled executable

I highly recommend to use Costura.Fody - by far the best and easiest way to embed resources in your assembly. It's available as NuGet package.

Install-Package Costura.Fody

After adding it to the project, it will automatically embed all references that are copied to the output directory into your main assembly. You might want to clean the embedded files by adding a target to your project:

Install-CleanReferencesTarget

You'll also be able to specify whether to include the pdb's, exclude certain assemblies, or extracting the assemblies on the fly. As far as I know, also unmanaged assemblies are supported.

Update

Currently, some people are trying to add support for DNX.

Update 2

For the lastest Fody version, you will need to have MSBuild 16 (so Visual Studio 2019). Fody version 4.2.1 will do MSBuild 15. (reference: Fody is only supported on MSBuild 16 and above. Current version: 15)

How do you make an exe run without needing all the dll files in the same directory?

Option 1: .NET Core >3.x, .NET 5

From Single-file deployment you can edit your project file to contain the following

<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net5.0</TargetFramework>
<PublishSingleFile>true</PublishSingleFile>
<SelfContained>true</SelfContained>
<RuntimeIdentifier>win-x64</RuntimeIdentifier>
<PublishTrimmed>true</PublishTrimmed>
<PublishReadyToRun>true</PublishReadyToRun>
</PropertyGroup>

</Project>

Which corresponds to running the CLI tool:
dotnet publish -r win-x64 -p:PublishSingleFile=true --self-contained true

Option 2: .NET Framework

Before the single-file deployment was available (in .NET Framework), I personally used Fody Costura. Fody is an assembly weaver which, after installing, puts some commands into the MSBuild configuration of your project that enable you to do many things. One add-in is Costura, it weaves your dependencies into a single assembly.

Embed dlls and contents into exe in monogame

Actually, it can be achieved by 2 steps.

  1. Embedding the dlls.
  2. Including the contents.

Embedding the dlls

This can be easily achieved by installing Costura.Fody using NuGet.

Type the following line to the Package Manager Console

Install-Package Costura.Fody

Source: Embedding DLLs in a compiled executable.

Including the contents

  1. Copy your compiled content files (.xnb files and other contents) to your Content folder.
  2. Go to the project property page and add your compiled content files to your resources.
  3. Replace the code below:

    public Game1()
    {
    graphics = new GraphicsDeviceManager(this);
    Content.RootDirectory = "Content";
    }

    with

    public Game1()
    {
    graphics = new GraphicsDeviceManager(this);
    ResourceContentManager resxContent;
    resxContent = new ResourceContentManager(Services, Resources.ResourceManager);
    Content = resxContent;
    }
  4. Finished! You can now load content using the old method

    Content.Load<Texture2D>("Image1");

Source: Loading Content Within a Game Library

How to embed a DLL into a DLL?

If you truly want to embed B.dll inside of A.dll, you can define B.dll as a resource of A.dll via an .rc script in A.dll's project. Then you can use B.dll at runtime by first using (Find|Load|Lock)Resource() to access the bytes for B.dll's resource and writing them to a tempory file using (Create|Write)File() before then loading that file with LoadLibrary().



Related Topics



Leave a reply



Submit