Embed and unpack referenced assembly purely in memory

917 views Asked by At

Say I have the given build structure:

  • MyApp.exe - .NET module
  • MyLib.Dll - C++/CLI module

How do I pack MyLib.dll into MyApp.exe as an embedded resource, and unpack MyLib.dll at startup without writing the Dll into the file system? (thus keeping it purely in memory)

  1. I can embed the Dll using the Solution Explorer in VS
  2. At runtime I unpack the Dll using GetManifestResourceStream
  3. Then what? How do I "integrate" this unpacked Dll into the current context such that the rest of the program finds the assembly classes, without writing the Dll to disk?

Edit : Dependency merging tools such as ILMerge cannot be used, since the Dll is a mixed-mode assembly, and no typical tools are able to merge it.

Edit : I can unpack the Dll to a file (documented here) and use Assembly.LoadFile() But I still cannot do this purely in memory, writing the Dll to a file making it vulnerable to thieves who can find the Dll and use it in their own app.

1

There are 1 answers

1
Robin Rodricks On

There are multiple ways to embed and unpack mixed-mode assemblies.

  • Costura - automated embedding and unpacking of all used assemblies marked "Copy Local"

  • Manual method - mark assembly "Embedded resource" and unpack using this or this