.NET: Microsoft Technologies based on the .NET software framework. Runtime: An environment required to run apps that aren't compiled to machine language.
Hello @McBride, Casey ,
Thanks for your question.
Since publish as single file still relies on the Windows OS loader for native P/Invoke calls, you should explicitly instruct .NET on where to find these files rather than relying on the default Windows search order.
I recommend intercepting the DLL request and forcing the application to load it directly from the secure Windows system folder by using NativeLibrary.SetDllImportResolver at the very startup of your application.
using System;
using System.Reflection;
using System.Runtime.InteropServices;
using System.IO;
public static class SecurityConfig
{
public static void SecureDllLoading()
{
NativeLibrary.SetDllImportResolver(Assembly.GetExecutingAssembly(), CustomDllResolver);
}
private static IntPtr CustomDllResolver(string libraryName, Assembly assembly, DllImportSearchPath? searchPath)
{
if (libraryName.Equals("kernel32.dll", StringComparison.OrdinalIgnoreCase))
{
string securePath = Path.Combine(Environment.SystemDirectory, "kernel32.dll");
return NativeLibrary.Load(securePath);
}
return IntPtr.Zero;
}
}
For more details, you can reference the official documentation for NativeLibrary.SetDllImportResolver.
I hope this addresses your question. If this response was helpful, please consider following the guidance to provide feedback.