Skip to content

Commit

Permalink
Merge pull request #1370 from veleek/native-assembly-resolve
Browse files Browse the repository at this point in the history
Load unmanaged assemblies the same way as managed assemblies.
  • Loading branch information
OsirisTerje authored Jan 3, 2024
2 parents 945090c + 462d9d2 commit 887516a
Showing 1 changed file with 41 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#if NETCOREAPP3_1_OR_GREATER

using System.Reflection;
using System.Runtime.InteropServices;
using System.Runtime.Loader;
using System.IO;
using System;
Expand Down Expand Up @@ -56,7 +57,7 @@ protected override Assembly Load(AssemblyName name)
if (loadedAssembly != null)
{
log.Info("Assembly {0} ({1}) is loaded using the TestAssembliesResolver", name, GetAssemblyLocationInfo(loadedAssembly));

return loadedAssembly;
}

Expand All @@ -79,6 +80,45 @@ protected override Assembly Load(AssemblyName name)
return loadedAssembly;
}

protected override IntPtr LoadUnmanagedDll(string name)
{
log.Debug("Loading {0} unmanaged dll", name);

IntPtr loadedDllHandle = base.LoadUnmanagedDll(name);
if (loadedDllHandle != IntPtr.Zero)
{
log.Info("Unmanaged DLL {0} is loaded using default base.LoadUnmanagedDll()", name);
return loadedDllHandle;
}

string runtimeResolverPath = _runtimeResolver.ResolveUnmanagedDllToPath(name);
if (string.IsNullOrEmpty(runtimeResolverPath) == false &&
File.Exists(runtimeResolverPath))
{
loadedDllHandle = LoadUnmanagedDllFromPath(runtimeResolverPath);
}

if (loadedDllHandle != IntPtr.Zero)
{
log.Info("Unmanaged DLL {0} ({1}) is loaded using the deps.json info", name, runtimeResolverPath);
return loadedDllHandle;
}

string unmanagedDllPath = Path.Combine(_basePath, name + ".dll");
if (File.Exists(unmanagedDllPath))
{
loadedDllHandle = LoadUnmanagedDllFromPath(unmanagedDllPath);
}

if (loadedDllHandle != IntPtr.Zero)
{
log.Info("Unmanaged DLL {0} ({1}) is loaded using base path", name, unmanagedDllPath);
return loadedDllHandle;
}

return IntPtr.Zero;
}

private static string GetAssemblyLocationInfo(Assembly assembly)
{
if (assembly.IsDynamic)
Expand Down

0 comments on commit 887516a

Please sign in to comment.