Skip to content

[native assets] Use AddDllDirectory on native assets dir(s) to enable dynamic linking #56551

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

Closed
dcharkes opened this issue Aug 22, 2024 · 3 comments
Labels
area-native-interop Used for native interop related issues, including FFI. os-windows P2 A bug or feature request we're likely to work on

Comments

@dcharkes
Copy link
Contributor

To make dynamic linking between native assets possible (a dll opening another dll at runtime because they have been linked at build) on Windows, we need to specify where the dynamic linked dlls can be found.

Yes, dart.exe and dartaotruntime.exe would have to call AddDllDirectory with the directory where all the native asset libraries have been copied to. For dart run and dart test that could be a directory in .dart_tool and for the executable created by dart build the lib directory relative to it.

To know the directory (or directories), we should probably add that info to native_assets.yaml. Then the embedder (Dart standalone or Flutter) who's bundling the native assets mapping can also specify the dll directories instead of trying to guess in the VM or in the embedder without knowing what command is being run. And the most natural time to add the dll directories would probably be just before loading the first dll we're loading with @Native external functions. For keeping the responsibilities in the right place, it should probably be done as another callback in NativeAssetsApi that the VM calls before the first dlopen. (Relevant code: https://dart-review.googlesource.com/c/sdk/+/361881 )

Originally posted by @blaugold in dart-lang/native#190 (comment)
Originally posted by @dcharkes in dart-lang/native#190 (comment)

Adding a tracking issue for the Dart SDK work.

@dcharkes dcharkes added os-windows P2 A bug or feature request we're likely to work on area-native-interop Used for native interop related issues, including FFI. labels Aug 22, 2024
@blaugold
Copy link
Contributor

An alternative would be to use LoadLibraryExW with LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR. I found this option while working on https://dart-review.googlesource.com/c/sdk/+/381580. This flag causes the directory that the DLL is being loaded from to be added to the search path for the duration of the LoadLibraryExW call. That would not require pluming through the DLL directorie(s) from native_assets.yaml, but would be less flexible.

@dcharkes
Copy link
Contributor Author

I think that in 95% of the use cases, the libraries would be right next to each other in the same directory. So for that, I'm all in favor of a simpler solution!

And then we need to document somewhere (in native_toolchain_c if we add some API for dynamic linking) that dlls that want to dynamically link should be in the same dir.

@dcharkes dcharkes added this to the Native Assets v1.0 milestone Sep 3, 2024
@blaugold
Copy link
Contributor

With https://dart-review.googlesource.com/c/sdk/+/381580 having landed, this issue can be closed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-native-interop Used for native interop related issues, including FFI. os-windows P2 A bug or feature request we're likely to work on
Projects
Status: Done
Development

No branches or pull requests

2 participants