Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Need to use it in C# with and unmanaged dll #99

Open
ShayanFiroozi opened this issue May 24, 2020 · 8 comments
Open

Need to use it in C# with and unmanaged dll #99

ShayanFiroozi opened this issue May 24, 2020 · 8 comments

Comments

@ShayanFiroozi
Copy link

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.

@Elmue
Copy link

Elmue commented Jun 22, 2020

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);

@ahdung
Copy link

ahdung commented Apr 28, 2021

So how to load MemoryModule.dll from memory?

@frankli0324
Copy link

this is a chicken-egg problem eventually

@Elmue
Copy link

Elmue commented Aug 13, 2021

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

@trungnt2910
Copy link

trungnt2910 commented Aug 24, 2021

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.

@ahdung
Copy link

ahdung commented Aug 25, 2021

@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?

@trungnt2910
Copy link

trungnt2910 commented Aug 25, 2021

@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.

@ahdung
Copy link

ahdung commented Aug 26, 2021

@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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants