Skip to content
This repository has been archived by the owner on Nov 1, 2020. It is now read-only.

Assembly Load(byte[]) with native compiled exe/dll #6958

Closed
homer90 opened this issue Feb 5, 2019 · 10 comments
Closed

Assembly Load(byte[]) with native compiled exe/dll #6958

homer90 opened this issue Feb 5, 2019 · 10 comments
Labels

Comments

@homer90
Copy link

homer90 commented Feb 5, 2019

I am really excited and fascinated by CoreRT!
Porting one of my existing projects I struggle with this code:

var bytes = File.ReadAllBytes(@"C:\corert\native_helper.dll");
Console.WriteLine($"bytes: {bytes.Length}");
var assembly = Assembly.Load(bytes);

My main program/exe (c# compiled to native code) wants to load my library "native_helper.dll" bytes (also c# compiled to native code). It throws this error:

bytes: 3379120
"Unhandled Exception: System.PlatformNotSupportedException: Operation is not supported on this platform."

In System.Private.CoreLib.dll it checks for "AppDomain.CheckLoadByteArraySupported();" and I guess that's where it crashes.

I searched through the samples and tried serveral options in .csproj. I ended up with those:

<OutputType>Library</OutputType>
<NativeLib>Shared</NativeLib>

How can I load assembly bytes into my program like in .NET Framework (code above)? Is this even possible after compiling it into native code? Thanks guys!

@jkotas
Copy link
Member

jkotas commented Feb 5, 2019

This is not possible in CoreRT today. Take a look at the discussion in #6949 about the different options.

Duplicate of #6949

@jkotas jkotas closed this as completed Feb 5, 2019
@jkotas jkotas added the Question label Feb 5, 2019
@homer90
Copy link
Author

homer90 commented Feb 6, 2019

@jkotas: I saw #6949 but it doesn't address loading an assembly by bytes, does it? How would I accomplish it with CoreRT?

@jkotas
Copy link
Member

jkotas commented Feb 6, 2019

It does not matter whether the assembly is bytes in memory or file on disk. The solution is same for both.

@homer90
Copy link
Author

homer90 commented Feb 6, 2019

@jkotas, @MichalStrehovsky
Sorry I still don't get it. Aren't those solutions adressing loading a .NET Core managed library that hasn't been compiled to native code with CoreRT?

English isn't my first language so apologies for that, I try to rephrase my goal:
What I want to do is use Assembly.Load(byte[]) and load a CoreRT native compiled library.

@MichalStrehovsky
Copy link
Member

Once managed code is compiled to native code, the resulting executable file is native. The file is no different from a native executable generated by e.g. C or C++.

You cannot load it back into a CoreRT process and expect to be able to do things with it. It will not work, same way it would not work to e.g. Assembly.Load an executable produced with C.

@homer90
Copy link
Author

homer90 commented Feb 6, 2019

@MichalStrehovsky Thanks for clearing that up. Could you please give an example on how to execute a native assembly loaded from bytes? I appreciate it.

@MichalStrehovsky
Copy link
Member

Could you please give an example on how to execute a native assembly loaded from bytes? I appreciate it.

It's not possible on Windows; you have to write the buffer to a file on disk and load from there. I don't know about Linux/macOS.

@homer90
Copy link
Author

homer90 commented Feb 6, 2019

@MichalStrehovsky, @jkotas : So there is no equivalent to MemoryModule (1000+ stars https://github.com/fancycode/MemoryModule) in CoreRT? Maybe we see it in future?

@MichalStrehovsky
Copy link
Member

You can build MemoryModule as a library and p/invoke into it from C#.

We are not planning to add similar functionality to CoreRT proper. Bypassing the operating system's module loader means that the operating system is not aware of the module. Lots of things will work, but there will be things that don't work as expected. We don't want to take on that support burden.

@homer90
Copy link
Author

homer90 commented Feb 6, 2019

@MichalStrehovsky: Fair enough. So after all there is no way to load a native assembly from bytes in .NET Core. Thanks and have a great day!

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

No branches or pull requests

3 participants