我正在尝试加载要与interop一起使用的库,该库具有一些dll依赖项,我知道所有名称,但是位置是动态的。我可以在“主” dll上调用LoadLibrary,如果其他文件位于工作目录中,则工作正常,情况并非如此。我尝试了对所有人调用LoadLibrary,但没有用。
有什么想法吗?是否有替代LoadLibrary的功能?
。NET 4.5 siverlight5项目,与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);
}