Question

I'm trying to load a library, to use with interop, that has some dll dependencies, all names are known to me, but the location is dynamic. I can call LoadLibrary on the "main" dll, and it works fine if the others are on the working directory, which isn't the case. I tried calling LoadLibrary on everybody but its useless.

Any ideas? Is there an alternative to LoadLibrary?

.NET 4.5 siverlight5 project, interop with 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!");
        }
Was it helpful?

Solution

Had to call LoadLibrary and use SetDllDirectory for it to work, using DllImport instead of LoadLibrary does not work.

    [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);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top