0

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!");
        }
WoF_Angel
  • 2,569
  • 9
  • 35
  • 54
  • 1
    possible duplicate of [How can I specify a \[DllImport\] path at runtime?](http://stackoverflow.com/questions/8836093/how-can-i-specify-a-dllimport-path-at-runtime) – Simon Mourier Apr 10 '14 at 17:06
  • I can load the dll via a dynamic path, but what about the dependecies? – WoF_Angel Apr 10 '14 at 17:08
  • All DLLs are loaded the same way. Read the link with SetDllDirectory function – Simon Mourier Apr 10 '14 at 17:09
  • Hmm, having a browser write executable files to disk and execute them. What could possibly go wrong? – Hans Passant Apr 10 '14 at 17:22
  • It's OOB only. And there are no executables, just dll's. Also not having any success with SetDllDirectory, it seems to be doing nothing, but i will keep trying. – WoF_Angel Apr 10 '14 at 17:28

1 Answers1

-1

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);
}
Dan
  • 45
  • 1
  • 8
WoF_Angel
  • 2,569
  • 9
  • 35
  • 54