如何使用LoadLibrary或任何其他方法加载dll及其依赖项
本文关键字:dll 依赖 加载 方法 LoadLibrary 何使用 任何 其他 | 更新日期: 2023-09-27 17:57:43
我正在尝试加载一个库,用于interop,该库具有一些dll依赖项,所有名称我都知道,但位置是动态的。我可以在"main"dll上调用LoadLibrary,如果其他的都在工作目录上,它就可以正常工作,但事实并非如此。我试着对每个人调用LoadLibrary,但没用。
有什么想法吗?有LoadLibrary的替代方案吗?
.NET 4.5 sivellight5项目,与fortran互操作。
var interopPath = CreateDllFromResource(dllName);
var d1=CreateDllFromResource("libgcc_s_dw2-1.dll");
var d2 = CreateDllFromResource("libgfortran-3.dll");
var d3 = CreateDllFromResource("libquadmath-0.dll");
pDll = NativeMethods.LoadLibrary(interopPath);
NativeMethods.LoadLibrary(d1);
NativeMethods.LoadLibrary(d2);
NativeMethods.LoadLibrary(d3);
//oh dear, error handling here
if (pDll == IntPtr.Zero)
{
throw new Exception("Dll Pointer is null! // Path: " + interopPath + "// Error: " + WSAGetLastError());
}
IntPtr pAddressOfFunctionToCall = NativeMethods.GetProcAddress(pDll, "ingammaextern_");
if (pAddressOfFunctionToCall == IntPtr.Zero)
{
throw new Exception("Function Pointer is null!");
}
必须调用LoadLibrary
并使用SetDllDirectory
才能工作,使用DllImport
而不是LoadLibrary
不起作用。
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
private delegate void ingammaextern_(StringBuilder resulfilesparam);
{
var dllName = "SubsCalculator1.dll";
var interopPath = CreateDllFromResource(dllName);
var d1 = CreateDllFromResource("libgcc_s_dw2-1.dll");
var d2 = CreateDllFromResource("libgfortran-3.dll");
var d3 = CreateDllFromResource("libquadmath-0.dll");
string folderPath ="C:'folder'
SetDllDirectory(folderPath);
pDll = NativeMethods.LoadLibrary(interopPath);
}