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

How can one call/consume a .NET Assembly from a native binary? #34571

Closed
brimonk opened this issue Apr 6, 2020 · 4 comments
Closed

How can one call/consume a .NET Assembly from a native binary? #34571

brimonk opened this issue Apr 6, 2020 · 4 comments

Comments

@brimonk
Copy link

brimonk commented Apr 6, 2020

Put simply, I would like to attempt to call some C# code from an existing C/C++ codebase. In my experience in the past, I have generally found this to be hard, calling into a managed runtime from an unmanaged runtime, but I expected it might be possible as it seemed that dotnet compiled down into a .dll/.so of some sort, and was just called from a loading program.

Just for the record, I am trying to do this on Ubuntu 19.04.

So, I created a simple classlib by using, dotnet classlib. I filled out an example class within the given namespace that looked something like this: public class Foo { public int Add(int a, int b) { return a + b; } }. Then I attempted to use dotnet publish to get something that would be close to a Unix shared object (.so), and to my surprise, there wasn't one.

All I got was a .dll.

According to the documentation for the command, you can supposedly use the -r flag to set the runtime. So, I tried thelinux-x64, osx-x64 and win-x64 and only got PE32+ .dll's.

What's going on? Is it impossible to "just get a binary" from the dotnet core compiler? I know that C# compiles to an intermediate representation from within that .dll, but is there a way to skip that step, and compile a shared C# library into a "regular" library?

@Dotnet-GitSync-Bot
Copy link
Collaborator

I couldn't figure out the best area label to add to this issue. Please help me learn by adding exactly one area label.

@Dotnet-GitSync-Bot Dotnet-GitSync-Bot added the untriaged New issue has not been triaged by the area owner label Apr 6, 2020
@jkotas
Copy link
Member

jkotas commented Apr 6, 2020

There is no straightforward way to publish .NET library as unmanaged library with C/C++ entrypoints today.

The closest thing that works today is hosting the runtime. The documentation is at
https://docs.microsoft.com/dotnet/core/tutorials/netcore-hosting

@jkotas jkotas added the question Answer questions and provide assistance, not an issue with source code or documentation. label Apr 6, 2020
@jeffschwMSFT jeffschwMSFT added this to the Future milestone Apr 6, 2020
@jeffschwMSFT jeffschwMSFT removed the untriaged New issue has not been triaged by the area owner label Apr 6, 2020
@AaronRobinsonMSFT AaronRobinsonMSFT changed the title Unexpected Results from dotnet publish How can one call/consume a .NET Assembly from a native binary? Apr 7, 2020
@jkotas jkotas removed the question Answer questions and provide assistance, not an issue with source code or documentation. label Apr 16, 2020
@AaronRobinsonMSFT
Copy link
Member

@brimonk I have been working on a project for exposing C exports for a .NET assembly. It doesn't yet create a single binary for distribution, but it is a start. I mentioned it at #3750 (comment).

@AaronRobinsonMSFT
Copy link
Member

As mentioned, the prototype project DNNE demonstrates an example of what is suggested in #34571 (comment).

@ghost ghost locked as resolved and limited conversation to collaborators Dec 9, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

5 participants