Skip to content
Merged
Changes from 1 commit
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Load hostfxr library from dotnet root so it can be found from NativeAOT
  • Loading branch information
dsplaisted committed Nov 13, 2025
commit df83f3fe4c6b65036c7a8adbdf5019c9aa0a1874
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,55 @@ namespace Microsoft.DotNet.NativeWrapper
{
public class NETBundlesNativeWrapper : INETBundleProvider
{
// lpFileName passed to LoadLibraryEx must be a full path.
private const int LOAD_WITH_ALTERED_SEARCH_PATH = 0x8;

[DllImport("kernel32.dll", CharSet = CharSet.Unicode, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
private static extern IntPtr LoadLibraryExW(string lpFileName, IntPtr hFile, int dwFlags);

public NetEnvironmentInfo GetDotnetEnvironmentInfo(string dotnetExeDirectory)
{
PreloadHostFxrLibrary(dotnetExeDirectory);

var info = new NetEnvironmentInfo();
IntPtr reserved = IntPtr.Zero;
IntPtr resultContext = IntPtr.Zero;

int errorCode = Interop.hostfxr_get_dotnet_environment_info(dotnetExeDirectory, reserved, info.Initialize, resultContext);
// If directory doesn't exist, we may not be able to load hostfxr, so treat as if there are no installed frameworks/SDKs.
if (Directory.Exists(dotnetExeDirectory))
{
int errorCode = Interop.hostfxr_get_dotnet_environment_info(dotnetExeDirectory, reserved, info.Initialize, resultContext);
}

return info;
}

private void PreloadHostFxrLibrary(string dotnetExeDirectory)
{
string? hostFxrPath = FindHostFxrLibrary(dotnetExeDirectory);
if (hostFxrPath != null)
{
LoadLibraryExW(hostFxrPath, IntPtr.Zero, LOAD_WITH_ALTERED_SEARCH_PATH);
}
}

private static string? FindHostFxrLibrary(string installRoot)
{
string hostFxrDirectory = Path.Combine(installRoot, "host", "fxr");
if (!Directory.Exists(hostFxrDirectory))
{
return null;
}

string libraryName = RuntimeInformation.IsOSPlatform(OSPlatform.Windows)
? "hostfxr.dll"
: RuntimeInformation.IsOSPlatform(OSPlatform.OSX)
? "libhostfxr.dylib"
: "libhostfxr.so";

return Directory.EnumerateFiles(hostFxrDirectory, libraryName, SearchOption.AllDirectories)
.OrderByDescending(File.GetLastWriteTimeUtc)
.FirstOrDefault();
}
}
}
Loading