Allow C# to export functions and constants to native code #308
Replies: 27 comments 7 replies
-
I could use this to get rid of a managed c++ project if it was possible to to pass strings. |
Beta Was this translation helpful? Give feedback.
-
+1 to have the possibility to automatically marshall at least String as a C char*. |
Beta Was this translation helpful? Give feedback.
-
I always support this feature but I just think why is this being language feature not a compiler feature with |
Beta Was this translation helpful? Give feedback.
-
I'm not sure I understand. Even if this feature required no new syntax and used a |
Beta Was this translation helpful? Give feedback.
-
@HaloFour You are right.
That so I think |
Beta Was this translation helpful? Give feedback.
-
I still think that it belongs here as it does affect the C# language and specification. It's like caller-info attributes which also didn't require any new syntax. |
Beta Was this translation helpful? Give feedback.
-
And there very well may be spec changes that are reasonable to make for method signatures which have this attribute. For example you probably don't want any random class in the signature (only native arrays, primitives, native structures or pointers). |
Beta Was this translation helpful? Give feedback.
-
+1 Simple said: I support, because it would save development time and increase productivity! There are a few things I would like to note here: |
Beta Was this translation helpful? Give feedback.
-
IL supports dllexports since at least .net 2.0
Would be extremely cool to get this added to C# specs, having to have extra build process that IL's the binary, adding a few lines and recompiling is a bit of a drag. |
Beta Was this translation helpful? Give feedback.
-
+1. Having this would even make it easier to make use of some .NET only libraries on other languages that support native functions. |
Beta Was this translation helpful? Give feedback.
-
Incidentally, has anyone of the LDT mentioned or considered this feature request? Who are the exact LDT members who could consider the matter? (@sharwell? @jaredpar?) |
Beta Was this translation helpful? Give feedback.
-
CoreRT has this feature- https://github.com/dotnet/corert/tree/master/samples/NativeLibrary |
Beta Was this translation helpful? Give feedback.
-
Well, we know that CoreRT has it, but it's still CoreRT: A bit limited and you have to marshal the data yourself, which isn't accessible for most and I feel that we shouldn't risk it. We also know that IL has what's needed. I really like this suggestion because it brings a lot of unique ways to let other languages interop with the CLR. |
Beta Was this translation helpful? Give feedback.
-
For starters, it requires loading the entire .NET runtime into memory. I want to say that there are other issues with marshaling and that it doesn't quite work like P/Invoke in reverse, but it's been a long time since I've read the book. I'd like to hear more about specific use cases for adding this feature. It's not that I'm opposed to it, it's just that "why shouldn't we" isn't generally accepted as a reason to add new features. Does CoreCLR even support this? |
Beta Was this translation helpful? Give feedback.
-
Being able to write plugins/extensions for native application in managed is a big thing for people who have minimal experience in unmanaged languages like me :). |
Beta Was this translation helpful? Give feedback.
-
This is actually a very common use case in my opinion and I believe that with the announcement of .NET 5 two days ago with a Specifically comes to mind development of plugins and applications in Nonetheless, as for myself I actually have a relatively niche use case which was not brought up anywhere else and it's DLL Injection on the Windows platform. For the time being 3F/DllExport technically works however is limiting to .NET Framework 3F/DllExport#90 and the old In my specific case of DLL Injection it would be nice to be able in the future potentially not have to resort to any potential tricks such as trying to manually load the CLR from a C++ DLL and then trying to tell the CLR to create and execute an interface instance or a method marked with As for implementation, I do agree with others that this should be a compiler feature with |
Beta Was this translation helpful? Give feedback.
-
Given it's been around half a year since the original question has been asked, I'd figure that someone should re-throw the question. I can't unfortunately say if @sharwell and @jaredpar are the appropriate LDT members to ask myself as I don't track the language development on this repository but it is worth a try. Is there any hope of making this a possibility given existing partial implementation? Is it necessary for more people to identify this as a necessity before any potential consideration? Nonetheless, apologies for stealing a small slice of your busy day fine gentlemen. |
Beta Was this translation helpful? Give feedback.
-
is this not added the dll export attribute? i want to use on windows but am curious if when will be available for dotnet core? and also would like to know if its cross platform support to any os. |
Beta Was this translation helpful? Give feedback.
-
In my opinion unmanaged exports would be a great feature in C#. Exports have a very common use case in plugin development in native applications, personally I have been writing plugins for |
Beta Was this translation helpful? Give feedback.
-
Having unmanaged exports would be a really cool feature to be added in C#. We already have a messy 3rd party library that we have to always install to handle this and it proves that native applications can access stuff like functions really well. Having this in the language itself would be really useful. |
Beta Was this translation helpful? Give feedback.
-
It appears this is being put into consideration with .NET 5 👍 |
Beta Was this translation helpful? Give feedback.
-
That's fantastic news!! |
Beta Was this translation helpful? Give feedback.
-
No special syntax or emit strategy from the compiler. We will understand the attribute for the purposes of forbidding direct calls and calling convention compatibility for function pointers, but the actual work is done at JIT-time. |
Beta Was this translation helpful? Give feedback.
-
Is this a thing now? I wrote a small class library with:
And a small app to run it:
And I get this:
So do we require to use DNNE or UnmanagedExports project, and it isn't bundled in .NET 6 unless we use the NativeAOT project? |
Beta Was this translation helpful? Give feedback.
-
It seems to work only when running dotnet publish, but not dotnet build (or when building from visual studio) Please tell me how I can use the dll just through a build, or a build script that builds the native dll as a part of my project's standard build process? |
Beta Was this translation helpful? Give feedback.
-
Poke I just ran into this myself. I was trying to do basically the same scenario, but unfortunately, it does not seem like the feature that would be useful for us in is going to be implemented. "Not enough desire from the community?". UnmanagedCallersOnly only works for AOT builds, not for non AOT builds. Placing the .export into the generated IL does seem like it works at a feasibility level to export the function directly from the managed assembly. I think its probably a matter of garnering enough support to have somebody implement this on the .NET teams rather than it not being possible. Being able to export direct from the managed assembly would be extremely useful for native hosting scenarios that "reverse P/Invoke" back to the host's functions. The existing solutions are very cludgy. Here is my inquiry on the issue: Here is somebody decompiling the managed assembly, simply putting the .export in the IL code and re assembling it. Poof it works. Still a managed assembly. |
Beta Was this translation helpful? Give feedback.
-
looks export emthod symbol has soloved but how export constants symbol in C#? |
Beta Was this translation helpful? Give feedback.
-
This is a copy/pasta from dotnet/roslyn#11771
(The title nearly says it all)
I would like to propose the ability to export a function written in a C#-project to native code (C++ etc.) -- quasi the opposite of a P/Invoke-call.
As the CLR has this already implemented (See Serge Lindin's book about the .NET 2.0 Assembler, chapter 18, pages 380 and following) since v2.0, no CLR changes have to be made for this feature.
An "exported" function must be marked as
static
and should be non-private. A possible example could be:which would compile into something like this:
which could be called from other (native) asseblies.
If one would like to export the function under a different name (e.g. as
MyAdd
instead ofAdd
), it could be done as follows:Which would result in the following CIL-code:
Of course, all parameters and return values must be value types (native arrays, primitives, native structures or pointer).
This comment suggested the usage of compiler-intrinsic pseudo-attributes instead of the new keyword
export
, which I think would have more consistency with theDllImport
-Attribute:Also take a look at this comment: dotnet/roslyn#11771 (comment), in which I suggest adding the possibility also to export constants.
would be equivalent to:
which would compile to:
Beta Was this translation helpful? Give feedback.
All reactions