-
Notifications
You must be signed in to change notification settings - Fork 5.2k
Consolidate NativeLibrary API to managed implementation #121058
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
base: main
Are you sure you want to change the base?
Conversation
|
Tagging subscribers to this area: @dotnet/interop-contrib |
|
|
||
| // Disable the OS dialogs when failing to load. This matches CoreCLR. | ||
| uint prev; | ||
| bool set = Interop.Kernel32.SetThreadErrorMode(Interop.Kernel32.SEM_FAILCRITICALERRORS | Interop.Kernel32.SEM_NOOPENFILEERRORBOX, out prev); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
According to https://devblogs.microsoft.com/oldnewthing/20170330-00/ , SEM_NOOPENFILEERRORBOX only has effect on Win16 API.
| // Resolve using the AssemblyLoadContext.ResolvingUnmanagedDll event | ||
| if (ret == IntPtr.Zero) | ||
| { | ||
| ret = LoadNativeLibraryViaAssemblyLoadContextEvent(assembly, libraryName); | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This change aligns the behavior that ALC.ResolveUnmanagedDll will be called for the LoadBySearch path.
| const int loadLibrarySearchFlags = (int)DllImportSearchPath.UseDllDirectoryForDependencies | ||
| | (int)DllImportSearchPath.ApplicationDirectory | ||
| | (int)DllImportSearchPath.UserDirectories | ||
| | (int)DllImportSearchPath.System32 | ||
| | (int)DllImportSearchPath.SafeDirectories; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| const int loadLibrarySearchFlags = (int)DllImportSearchPath.UseDllDirectoryForDependencies | |
| | (int)DllImportSearchPath.ApplicationDirectory | |
| | (int)DllImportSearchPath.UserDirectories | |
| | (int)DllImportSearchPath.System32 | |
| | (int)DllImportSearchPath.SafeDirectories; | |
| const int loadLibrarySearchFlags = (int)(DllImportSearchPath.UseDllDirectoryForDependencies | |
| | DllImportSearchPath.ApplicationDirectory | |
| | DllImportSearchPath.UserDirectories | |
| | DllImportSearchPath.System32 | |
| | DllImportSearchPath.SafeDirectories); |
| if (!libNameIsRelativePath) | ||
| { | ||
| // LOAD_WITH_ALTERED_SEARCH_PATH is incompatible with LOAD_LIBRARY_SEARCH flags. Remove those flags if they are set. | ||
| int flags = loadWithAlteredPathFlags | (dllImportSearchPathFlags & ~loadLibrarySearchFlags); | ||
| ret = LoadLibraryHelper(currLibNameVariation, flags, ref errorTracker); | ||
| if (ret != IntPtr.Zero) | ||
| { | ||
| return ret; | ||
| } | ||
| } | ||
| else if ((callingAssembly != null) && searchAssemblyDirectory) | ||
| { | ||
| // LOAD_WITH_ALTERED_SEARCH_PATH is incompatible with LOAD_LIBRARY_SEARCH flags. Remove those flags if they are set. | ||
| int flags = loadWithAlteredPathFlags | (dllImportSearchPathFlags & ~loadLibrarySearchFlags); | ||
|
|
||
| // Try to load the module alongside the assembly where the PInvoke was declared. | ||
| // For PInvokes where the DllImportSearchPath.AssemblyDirectory is specified, look next to the application. | ||
| ret = LoadLibraryHelper(Path.Combine(AppContext.BaseDirectory, currLibNameVariation), flags, ref errorTracker); | ||
| if (ret != IntPtr.Zero) | ||
| { | ||
| return ret; | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The code here seems mostly duplicated?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's from NativeAOT and simply replicating the coreclr behavior. I aim to keep the change less.
Taking NativeAOT implementation and adapting coreclr behavior. The remaining code for P/Invoke resolver isn't changed.
Mono implementation can be update too, but I'm unsure about how the fallback registered for wasm should be handled.