MemoryModule icon indicating copy to clipboard operation
MemoryModule copied to clipboard

Need to use it in C# with and unmanaged dll

Open ShayanFiroozi opened this issue 5 years ago • 8 comments
trafficstars

Hi , I need to use this library in C# to load a unmanaged (C++) dll from memory not from file.

is this possible with this library and if it is please tell me how ?

Thank you.

ShayanFiroozi avatar May 24 '20 08:05 ShayanFiroozi

That is very easy. Compile this project into a DLL and export a function

__declspec(dllexport) BOOL MemoryLoadLibrary(BYTE* data, int size);

In C# you call this with a Byte[] buffer which contains the binary data of the DLL that you want to load to memory:

[DllImport("MemoryModule.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern bool MemoryLoadLibrary(Byte[] data, int size);

Elmue avatar Jun 22 '20 16:06 Elmue

So how to load MemoryModule.dll from memory?

ahdung avatar Apr 28 '21 03:04 ahdung

this is a chicken-egg problem eventually

frankli0324 avatar Aug 13 '21 08:08 frankli0324

It is not a chicken-egg problem. It is a nonsese question.

Elmue avatar Aug 13 '21 14:08 Elmue

So how to load MemoryModule.dll from memory?

Translate it to C#! It's quite easy actually, just more than 1000 lines, and I've got it working on .NET Standard 2.0.

Try this: https://github.com/trungnt2910/MemoryModule.NET I might release a nuget package when I'm less busy, and when I find the cause of this issue.

trungnt2910 avatar Aug 24 '21 14:08 trungnt2910

@trungnt2910 Great! But I think the nuget package is unpopular, why embed dll, because developers(at least I am) only wants a exe, if we can accept managed dll, why can't accept unmanaged dll, isn't it?

ahdung avatar Aug 25 '21 01:08 ahdung

@ahdung

if we can accept managed dll, why can't accept unmanaged dll, isn't it?

Embedding managed dlls and loading it from memory is much more simple in .NET It's as simple as a Assembly.Load(byte[]) statement, while loading unmanaged dlls are more difficult. Of course, you can always extract them to a temporary location, and then load them from files, but there are a few problems:

  • What will clean them, when the app crashes?
  • How can we check whether we could clean this library? Some other process might be accessing it?

But a clean solution is available:

  • Embed this package, at a cost of 100kb, as an embedded resource (or merge it using ILMerge or Fody.Costura,...)
  • Load this package from memory, using Assembly.Load(byte[]).
  • Load as many unmanaged dlls as you want, from embedded resources.

If the process crashes, the OS automatically reclaims all allocated library. No wasted disk space, no file permission headaches!

But I think the nuget package is unpopular

I am neither Microsoft nor James Newton-King, so I don't expect my packages to get popular quickly, but if it solves some problem, I'll maintain it.

trungnt2910 avatar Aug 25 '21 10:08 trungnt2910

@trungnt2910 You're right, I forgot the Assembly.Load(byte[]) can handle managed thing. I understand the benefits of embeding dll, that's why I'm here. Looking forward to your great work.

ahdung avatar Aug 26 '21 01:08 ahdung