-
Notifications
You must be signed in to change notification settings - Fork 4.8k
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
Support PInvoke exports via regular LoadLibrary/etc. without setting up runtime first #3750
Comments
/cc @jkoritzinsky |
We currently do not have plans to support loading a .NET Core assembly via LoadLibrary the same way as in .NET Framework. It is possible to do by manually editing the PE imports table in the .dll file to point to the new |
Related: dotnet/csharplang#308 Personally I would love this feature too, even if just only for Windows however not everyone can always have the cake they want. If there are technical reasons as to why exports cannot work on a multiplatform level (or simply would take considerable effort to do so) then I do accept the possibility of the developers not wanting to discriminate against other OS. There are a lot of uses for such a feature in my opinion, but also dangers. An interesting use coming to mind is being able to write plugins/extensions for native applications in a managed environment which may be beneficial for many programmers who find greatly enhanced productivity in the .NET ecosystem. An interesting danger is isolation. Implementing such an export mechanism should (probably) Random arbitrary thing: On Windows, it is currently not possible to get managed code execution in a native application in a synchronous way. If you do try, for example as given above write a plugin with a native library providing an interface for a managed component, the managed component couldn't be initialized inside of the native component's This is due to the Windows loader lock. Startup is still possible if you create a separate thread and allow Well... hacking around this is still possible, but very dangerous and non-recommended for anyone. Two ideas come to mind: To random bypassers looking for alternatives. If executing managed code in a native process is the goal, the officially supported and endorsed approach to doing to at the current moment in time with Core 3.X/.NET 5 (as OP mentions with An example of this can be found on MSDN: https://docs.microsoft.com/en-us/dotnet/core/tutorials/netcore-hosting with samples available in the following Github repository https://github.com/dotnet/samples/tree/master/core/hosting . The samples are relatively straightforward in my opinion, however one thing of note that I would add is that the signature of the managed function to execute needs to match the provided function signature in the example It's a bit of effort but you can still thankfully glue some C/C++/Other Native code to expose your .NET Library/Program to the native world. Good luck! |
I got it working in snoopwpf without a custom host and special setup on the "hosting" side, aside from loading ijwhost. The setup to create a working core dll also isn't that much different from the steps needed in full framework. The main difference is that i have to rewrite the dllmain of the core dll to point to ijwhost instead of mscoree. It still does not work for self contained core apps because hostfxr currently prevents this scenario. |
@Sewer56 - just a comment on your suggestion that such load should involve some amount of isolation. Currently all the ways to load an assembly into a process (through the native hosting APIs in |
I see. Thank you. I've never seen mention of isolation anywhere in the documentation so I assumed the behaviour was similar to NetFX/Framework where assemblies with exports get loaded into the default AppDomain. Glad to hear that I've been wrong. |
Not sure where in the official docs this is mentioned (if anywhere actually - we still have a lot of work left to cleanup the official docs) - it is definitely in the design doc: https://github.com/dotnet/core-setup/blob/master/Documentation/design-docs/native-hosting.md#loading-managed-components and for IJW: https://github.com/dotnet/core-setup/blob/master/Documentation/design-docs/IJW-activation.md#loading-the-assembly-into-the-runtime |
@MeikTranel I have created a project that might help with some of this. It requires the latest .NET 5.0 though. It uses the The project is at https://github.com/AaronRobinsonMSFT/DNNE and is also published https://www.nuget.org/packages/DNNE. |
I like C99 touch although I don't like installed dependencies, but that's something we can work around. What drives the sweat out of me is the idea of having to supply the marshalling, but I guess that was kind of bound to happen. One last question: have you considered the deployment scenarios yet? Single file standalone, single file framework dependent, regular multi dll output etc? Are there any theoretical barriers off the top of your head? |
I assume you mean the requirements for VS or clang on the path? If so, you can use the
As in you want the runtime supply that? If so, going the
That is apart of the larger hosting issues. The self contained runtime et al is a hard problem and I am unsure where we are there for components - @vitek-karas could comment more. What would be nice is having the single file solution for libraries, not just applications. If that was done, then we could embed everything for the FDD scenario and have a single native binary. |
Single file framework dependent is definitely doable. .NET 5 should have support for creating applications like that, currently we don't have any plans to support components. That said I don't see any special problems with it. Especially when combined with the native exports per @AaronRobinsonMSFT 's project this could work very well. Single file self-contained is very problematic. The plans are for .NET 5 to really only have a true single file for applications on Linux/Mac. Windows will for now still need the runtime to live in a separate .dll (limitations of the diagnostics stack if I remember correctly) but eventually we think it's also doable - for applications. Components are a different story though. The main issue is that we don't support loading multiple .NET Core runtimes into one process. Generic solution for self-contained components would have to allow that somehow. There are also problems with diagnostics (similar to the above) and couple of other cases. Multi file should work just fine in all cases except self-contained components - same problems as mentioned above. There's a discussion around a special case of a self-contained component: Basically the host only ever wants to load one managed component - in that case the issues with self-contained and multiple runtimes goes away. There's an interesting discussion on this topic here: #35465. |
Due to lack of recent activity, this issue has been marked as a candidate for backlog cleanup. It will be closed if no further activity occurs within 14 more days. Any new comment (by anyone, not necessarily the author) will undo this process. This process is part of our issue cleanup automation. |
As i understand, as of .NET Core 3.0 calling .NET library functions from something like a native wrapper is supported only via the new external host model, where a slew of APIs have to be called before actually being able to hit the .NET Core dll C API endpoint (but this time without the need for rewriting the assembly post-build).
I have been working on a modern replacement for the
UnmanagedExports
NuGet package, which reweaves attributed methods for VTFixUp entries and thus i am naturally interested. Seehttps://github.com/MeikTranel/NXPorts for more info.
Can we clarify whether it is a goal to support this feature in parallel to how it worked in .NET Framework.
The text was updated successfully, but these errors were encountered: