Dynamic DLL loading (C#) in IIS7 fails
===

We have two web services Running on separate application pools in IIS7. The web services are identical, what differs is that one connects to the test Database and the other to the live database. 

The Web services require a DLL which was written in Delphi to churn out some of the business logic required by the system, which we are loading using Dynamic DLL Loading like so:

    public static class DynamicLinking
        {
            private static int libHandle;
            private static string dllName;
    
            public static void init(String pDllName)
            {
                dllName = pDllName;
                libHandle = LoadLibrary(pDllName);
            }
    
            public static void fini()
            {
                FreeLibrary(libHandle);
            }
    
            [DllImport("kernel32.dll", EntryPoint = "LoadLibrary")]
            static extern int LoadLibrary([MarshalAs(UnmanagedType.LPStr)] string lpLibFileName);
    
            [DllImport("kernel32.dll", EntryPoint = "GetProcAddress")]
            static extern IntPtr GetProcAddress(int hModule, [MarshalAs(UnmanagedType.LPStr)] string lpProcName);
    
            [DllImport("kernel32.dll", EntryPoint = "FreeLibrary")]
            static extern bool FreeLibrary(int hModule);
    
            [UnmanagedFunctionPointer(CallingConvention.StdCall, CharSet = CharSet.Ansi)]
            delegate bool QryFunction(string domainString, string qryString,
                                         int outputStringBufferSize, ref string outputStringBuffer);
            public static void ExecuteDLLFunction(string pfunctionName, int bufferSize,
                                                   string pDomain, string inputXMLString,
                                                    out string outputString)
            {
    
                if (libHandle == 0)
                    throw new Exception(string.Format("Could not load library \"{0}\"", dllName));
    
                var delphiFunctionAddress = GetProcAddress(libHandle, pfunctionName);
                if (delphiFunctionAddress == IntPtr.Zero)
                    throw new Exception(string.Format("Can't find function \"{0}\" in library \"{1}\"", pfunctionName, dllName));
    
                var queryFunction = (QryFunction)Marshal.GetDelegateForFunctionPointer(delphiFunctionAddress, typeof(QryFunction));
    
                var outputStringBuffer = new String('\x00', bufferSize);
                var errorMsgBuffer = new String('\x00', bufferSize);
    
                if (!queryFunction(pDomain, inputXMLString,
                                    bufferSize, ref outputStringBuffer))
                    throw new Exception(errorMsgBuffer);
    
                outputString = outputStringBuffer;   
                
            }
        }

Now the problem lies here: when we start the web the websites, which ever site gets started last will fail to load the dll, while the first one to load will run fine.

Any insight on this ?

Thanks in advance.