我的托管应用程序对非托管DLL有一些依赖性。即使我的非托管DLL部署在我的bin文件夹中,它们也不会被加载。 I already found a (hopefully) outdated SO post which points out two possible solutions:
但是,我不喜欢任何一个解决方案,因为它基本上在我的部署文件夹之外添加了一个依赖项(=在我的管辖范围之外)。我不能让IIS知道加载非托管DLL(例如通过web.config)吗?
答案 0 :(得分:0)
您可以加载非托管DLL,并将其所在的完整路径传递到LoadLibraryEx
:
[DllImport("kernel32.dll", SetLastError = true)]
public static extern IntPtr LoadLibraryEx(string lpFileName, IntPtr hReservedNull, LoadLibraryFlags dwFlags);
//...
//get the physical path of the root directory of the application
string rootPath = System.Web.HttpContext.Current.Server.MapPath("~");
string libPath = Path.Combine(rootPath, "bin", "relative path to your DLL");
var h = LoadLibraryEx(libPath, IntPtr.Zero, NativeDlls.LoadLibraryFlags.LOAD_WITH_ALTERED_SEARCH_PATH);
if (h == IntPtr.Zero)
{
throw new Exception("Error in LoadLibrary: " + Marshal.GetLastWin32Error());
}
应将库设置为复制到内部版本的输出目录。