Skip to content

Conversation

@huoyaoyuan
Copy link
Member

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.

@github-actions github-actions bot added the needs-area-label An area label is needed to ensure this gets routed to the appropriate area owners label Oct 24, 2025
@dotnet-policy-service dotnet-policy-service bot added the community-contribution Indicates that the PR has been added by a community member label Oct 24, 2025
@huoyaoyuan huoyaoyuan added area-System.Runtime.InteropServices and removed needs-area-label An area label is needed to ensure this gets routed to the appropriate area owners labels Oct 24, 2025
@dotnet-policy-service
Copy link
Contributor

Tagging subscribers to this area: @dotnet/interop-contrib
See info in area-owners.md if you want to be subscribed.


// 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);
Copy link
Member Author

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.

Comment on lines +311 to +315
// Resolve using the AssemblyLoadContext.ResolvingUnmanagedDll event
if (ret == IntPtr.Zero)
{
ret = LoadNativeLibraryViaAssemblyLoadContextEvent(assembly, libraryName);
}
Copy link
Member Author

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.

Comment on lines +378 to +382
const int loadLibrarySearchFlags = (int)DllImportSearchPath.UseDllDirectoryForDependencies
| (int)DllImportSearchPath.ApplicationDirectory
| (int)DllImportSearchPath.UserDirectories
| (int)DllImportSearchPath.System32
| (int)DllImportSearchPath.SafeDirectories;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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);

Comment on lines +394 to +416
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;
}
}
Copy link
Contributor

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?

Copy link
Member Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-System.Runtime.InteropServices community-contribution Indicates that the PR has been added by a community member

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants