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

Consider using Native Library Loading #226

Closed
eXpl0it3r opened this issue May 2, 2023 · 3 comments
Closed

Consider using Native Library Loading #226

eXpl0it3r opened this issue May 2, 2023 · 3 comments
Labels
Feature New functionality or extending of an existing one

Comments

@eXpl0it3r
Copy link
Member

Using p/invoke with DllImportAttribute is a bit cumbersome in a cross-platform manner.

As such, it might make sense to use the NativeLibrary class instead.

References

@eXpl0it3r eXpl0it3r added the Feature New functionality or extending of an existing one label May 2, 2023
@DemoXinMC
Copy link
Contributor

DemoXinMC commented Jan 3, 2024

I'm a bit confused on the benefit of this.

In addition to the large scale rewrite of P/Invoke definitions, wouldn't this also either require users to manually call some static method we create like SFML.Modules.LoadLibrary()(name just made up) or add their own NativeLibrary.Load("sfml-system.dll"); at the start of their program?

What do we gain out of this change?

@eXpl0it3r
Copy link
Member Author

eXpl0it3r commented May 18, 2024

Having to workaround limitations of DllImport again (see #247), I seem to understand the underlying problem a bit better.

We can provide a single name to DllImport and then have to hope that InteropServices does the correct thing in resolving the libraries cross-platform. As an example for csfml-graphics it would need to find on Windows csfml-graphics.dll, on Linux libcsfml-graphics.so, and on macOS libcsfml-graphics.dylib, but that in a specific directory, that is the bin/<Config>/<TargetFramework>/runtimes/<RID>/native/.

While it actually gets pretty close, looks in all those directories, it fails to look for the right combination - in my opinion a bug. Thus it forces us to ship libraries without suffix, e.g. libcsfml-graphics on Linux and macOS.

There's one potential workaround and that is to use write our own Custom Import Resolver, so we can define ourselves what is being looked for: https://learn.microsoft.com/en-us/dotnet/standard/native-interop/native-library-loading#custom-import-resolver

The use of Native Library Loading was just another idea, that I don't fully understand (yet) and might not be suitable.

I'm wrong. I think the main confusing with all of this is, that if the native lib is found, but then fails to load due to a missing dependency, DllImport will simply continue loading other libraries. The messaging around this is not very clear, so at a glance it appears as if the specific library was never considered, while it was actually always a dependency issue.

This now also allowed me to revert the change made regarding extension fixing in CSFML.

@eXpl0it3r
Copy link
Member Author

I don't necessarily think Native Library Loading is needed and may actually not make sense. If we need to influence the loading behavior we may probably first want to write our own Custom Import Resolver.

As such, I'll close this issue for now.

@eXpl0it3r eXpl0it3r closed this as not planned Won't fix, can't repro, duplicate, stale May 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Feature New functionality or extending of an existing one
Projects
None yet
Development

No branches or pull requests

2 participants