[native assets] Use `AddDllDirectory` on native assets dir(s) to enable dynamic linking
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.exeanddartaotruntime.exewould have to callAddDllDirectorywith the directory where all the native asset libraries have been copied to. Fordart runanddart testthat could be a directory in.dart_tooland for the executable created bydart buildthelibdirectory 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.
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.
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.
With https://dart-review.googlesource.com/c/sdk/+/381580 having landed, this issue can be closed.