如何使用LoadLibrary或任何其他方法加载带有依赖项的dll

时间:2014-04-10 17:03:52

标签: c# dll interop silverlight-5.0

我正在尝试加载一个库,与interop一起使用,它有一些dll依赖项,所有名称都为我所知,但该位置是动态的。我可以在" main"上调用LoadLibrary。 dll,如果其他人在工作目录上,它可以正常工作,但事实并非如此。我尝试在所有人身上调用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!");
        }

1 个答案:

答案 0 :(得分:-1)

必须调用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);
}