sdk icon indicating copy to clipboard operation
sdk copied to clipboard

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

Open dcharkes opened this issue 1 year ago • 2 comments

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 https://github.com/dart-lang/native/issues/190#issuecomment-2297309519 Originally posted by @dcharkes in https://github.com/dart-lang/native/issues/190#issuecomment-2304121000

Adding a tracking issue for the Dart SDK work.

dcharkes avatar Aug 22 '24 08:08 dcharkes

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.

blaugold avatar Aug 22 '24 09:08 blaugold

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 avatar Aug 22 '24 10:08 dcharkes

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

blaugold avatar Dec 12 '24 17:12 blaugold