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

Improve finding frameworks in DllImport #21238

Open
rolfbjarne opened this issue Sep 13, 2024 · 0 comments
Open

Improve finding frameworks in DllImport #21238

rolfbjarne opened this issue Sep 13, 2024 · 0 comments
Labels
enhancement The issue or pull request is an enhancement
Milestone

Comments

@rolfbjarne
Copy link
Member

rolfbjarne commented Sep 13, 2024

Currently DllImports to a function in a framework needs to use this as the library:

[DllImport ("@rpath/MyFramework.framework/MyFramework")]
static extern void MyFunction ();

This is rather complex and not intuitive for .NET developers, and it gets complicated for NuGets when the same code is supposed to work on multiple platforms, when other platforms use a different library name (it's not possible to compile the code once and share it).

One idea would be to introduce a build step that rewrites the library for DllImports if there's a matching framework included in the build.

Say a DllImport has LibraryName="MyFramework", and a MyFramework.framework/MyFramework is included in the build - then we'd rewrite the DllImport to point to @rpath/MyFramework.framework/MyFramework instead.

Downsides:

  • It might slow down debug builds (since all the methods in all assemblies would have to be scanned (although we might have a list of assemblies we know we don't have to scan, so it would only be user assemblies).

An existing solution is for customers to use the NaiveLibrary API (https://learn.microsoft.com/en-us/dotnet/api/system.runtime.interopservices.nativelibrary?view=netcore-3.1), but it's not very discoverable. It also adds runtime cost for something we can do at compile time.

@rolfbjarne rolfbjarne added the enhancement The issue or pull request is an enhancement label Sep 13, 2024
@rolfbjarne rolfbjarne added this to the Future milestone Sep 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement The issue or pull request is an enhancement
Projects
None yet
Development

No branches or pull requests

1 participant