如何通过LoadLibrary或任何其他方法加载具有依赖性的dll

问题描述 投票:0回答:1

我正在尝试加载要与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!");
        }
c# dll interop silverlight-5.0
1个回答
-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);
}
© www.soinside.com 2019 - 2024. All rights reserved.