runtime icon indicating copy to clipboard operation
runtime copied to clipboard

Support mixed-mode assemblies on Windows

Open morganbr opened this issue 8 years ago • 107 comments

This issue will track progress toward supporting loading and running mixed-mode assemblies on CoreCLR. The main goal is to provide support for WPF and other existing C++/CLI code on .NET Core. Some of the work will be dependent on updates to the MSVC compiler.

Steps include:

  • [x] Documenting how mixed-mode assemblies work in general
  • [x] Proposing a .NET Core design
  • [x] Loading mixed-mode assemblies into running managed code
  • [x] Starting the runtime from native code in mixed-mode assemblies

This is related to dotnet/runtime#4116, but cross-platform support is outside of the scope of this issue as it's a compiler feature, not a runtime feature.

morganbr avatar May 16 '18 00:05 morganbr

Paint.NET author here ... you've got my attention :D I have 50 KLOCs of C++/CLI code ... let me know if you need a guinea pig :)

rickbrew avatar May 21 '18 18:05 rickbrew

Thanks, @rickbrew. It'll be a while until we're ready for Paint.NET, but we'll keep you in mind.

morganbr avatar May 24 '18 22:05 morganbr

I think we can start by reverting https://github.com/dotnet/coreclr/commit/1aa7d6b8796f7e28a63162117c5bb16a207a472b to at least allow the ability to load multi module assemblies.

mjsabby avatar Jun 28 '18 20:06 mjsabby

@mjsabby, multi-module assemblies are orthogonal to mixed-mode. (Multi-module means you linked together netmodules using alink.exe, mixed mode means you have a combination of managed and native code in the same assembly). If you have a requirement for multi-module assemblies, I'd suggest filing a separate issue outlining what you need.

morganbr avatar Jun 28 '18 20:06 morganbr

@morganbr Thanks. I'm not sure why I concluded that C++/CLI needs netmodule support.

mjsabby avatar Jun 28 '18 21:06 mjsabby

Awesome we're making progress on this ground. C++/CLI is what we use to bridge native service to the awesome .NET world. So far no other alternative achieves the elegance of C++/CLI as a glue language so it's fair to say that we're going to rely on C++/CLI as an integral part of our service.

Some time ago in the spirit of searching for performance gains by migrating from .NET framework to .NET core I sadly found that .NETCore doesn't go together with C++/CLI (even though we don't care about cross-platform yet).

And now this thread lifts my mood 👍 Looking forward to the release (as while I searched for solutions I saw a lot of people had the same need with C++/CLI in their own projects)

pongba avatar Sep 15 '18 02:09 pongba

@morganbr given that "Loading mixed-mode assemblies into running managed code" is done, should it be possible to use latest .NET Core 3.0 preview together with mixed mode assemblies on Windows? E.g. from a C# application?

We intent to use .NET Core 3.0 with WPF and have quite a lot of C++/CLI dependencies/libraries (a lot third party too), so looking forward to this.

nietras avatar Oct 18 '18 20:10 nietras

@nietras, I'm glad to hear you're eager to give this a shot. It's probably a little early to try it with the latest preview though. While the runtime can run mixed-mode assemblies now, the C++ compiler also needs some changes to make everything work smoothly.

In case anyone's interested in details, the main C++ compiler changes are:

  1. Be able to build against .NET Core assemblies instead of .NET Framework (this isn't strictly blocking, but can cause issues if the assembly references a type that isn't in Core or wants to use a type that isn't' in Framework)
  2. An assembly built with the current release of the C++ compiler will load and call into mscoree.dll on startup, which starts up .NET Framework. As you might imagine, having both runtimes think they loaded the assembly can cause lots of problems.

morganbr avatar Oct 18 '18 20:10 morganbr

@morganbr will the changes to C++(CLI) compiler that supports .NETCore be released together with the next version of Visual Studio, or will it be released out-of-band?

pongba avatar Oct 23 '18 21:10 pongba

@pongba, I don't think we're ready to say exactly when or how we'll first distribute that version of the compiler, but it will eventually get distributed with Visual Studio.

morganbr avatar Oct 23 '18 21:10 morganbr

So should it be possible to load assemblies which have ddlexports (vtfixup etc.) in them with the current preview of .net core 3.0? I am asking this because i just get a BadImageFormat exception when trying to load such assembly which i compiled with .net core 3.0 and used ildasm and ilasm to add the exports. The same code works when compiling with .net 4.6.2. I can provide the il code if required as a repro.

batzen avatar Dec 08 '18 00:12 batzen

Considering of C++/WinRT, is it possible to write a standard C++ projection for .NET Core?

Berrysoft avatar Dec 10 '18 09:12 Berrysoft

@morganbr Would be nice to get a response from you. Not being able to at least load assemblies which contain an dll export prevents me from porting https://github.com/cplotts/snoopwpf to .NET core 3.0.

batzen avatar Dec 21 '18 10:12 batzen

@batzen, you could be hitting a few different issues. You might be able to narrow them a bit by seeing if directly calling LoadLibrary on your assembly in the .NET Core process works. Some possible issues:

  1. Processor architecture mismatch (in particular, .NET Core is x64 by default while .NET Framework is x86 by default)
  2. Depending on how exactly your imports/exports are set up, your assembly might be causing mscoree.dll to load. If that happens, it tries to start .NET Framework and all kinds of bad things (including BadImageFormat) can happen. You should be able to spot mscoree.dll loading in a debugger if you have mixed-mode debugging enabled.
  3. Something specific to your ildasm/ilasm process. You'd need to debug further into the error to find out if there's something .NET Framework-specific that you're relying on.

morganbr avatar Jan 02 '19 23:01 morganbr

@Berrysoft, .NET Core supports WinRT, although some of the features may only be available in UWP applications.

morganbr avatar Jan 02 '19 23:01 morganbr

@morganbr Oh, no, I mean, the current grammar of C++/CLI is non-standard. It is possible to make a projection of .NET Core with standard C++ grammar, so that other standard C++ compilers will compile the code, just like what C++/WinRT did for WinRT?

Berrysoft avatar Jan 03 '19 00:01 Berrysoft

@morganbr

  1. Architecture is ok. Tried both, x86 and x64.
  2. It's a pure .net core 3.0 assembly. No reference to mscoree. Just one method in one class. IL code can be found at https://gist.github.com/batzen/8b1ed3e7269b4aaaacee846f188ef347
  3. I just run ilasm ManagedCore.il /outfile=ManagedCoreExported.dll /dll on the IL code from the gist.

Doing exactly the same for the equivalent IL code for .NET framework, which then of course contains mscoree references, works without an BadImageFormatException. Loading of the modified assembly is done via Assembly.LoadFile.

If mixed mode assembly loading is supported in .NET core 3.0 are there any samples available? Some UnitTests you can point me to?

batzen avatar Jan 03 '19 01:01 batzen

As this issue is assigned now, i guess you were able to reproduce it?

batzen avatar Feb 02 '19 13:02 batzen

@batzen Mixed mode assemblies aren't supported in .NET Core 3.0 yet. This issue is tracking that support. @jkoritzinsky is actively working on investigating what needs to happen and the changes that will need to be introduced. Look for a design doc on this in the next week or two.

AaronRobinsonMSFT avatar Feb 04 '19 06:02 AaronRobinsonMSFT

@AaronRobinsonMSFT if loading mixed mode assemblies not currently not support the task point "Loading mixed-mode assemblies into running managed code" in this issue should be unchecked, shouldn't it?

batzen avatar Feb 04 '19 20:02 batzen