Пример #1
0
        private IntPtr CheckCustomSearchPath(string fileName, string platformName)
        {
            var baseDirectory = CustomSearchPath;

            if (!String.IsNullOrEmpty(baseDirectory))
            {
                LibraryLoaderTrace.TraceInformation("Checking custom search location '{0}' for '{1}' on platform {2}.", baseDirectory, fileName, platformName);
                return(InternalLoadLibrary(baseDirectory, platformName, fileName));
            }
            else
            {
                LibraryLoaderTrace.TraceInformation("Custom search path is not defined, skipping.");
                return(IntPtr.Zero);
            }
        }
Пример #2
0
        /// <summary>
        /// Special test for web applications.
        /// </summary>
        /// <remarks>
        /// Note that this makes a couple of assumptions these being:
        ///
        /// <list type="bullet">
        ///     <item>That the current application domain's location for web applications corresponds to the web applications root directory.</item>
        ///     <item>That the tesseract\leptonica dlls reside in the corresponding x86 or x64 directories in the bin directory under the apps root directory.</item>
        /// </list>
        /// </remarks>
        /// <param name="fileName"></param>
        /// <param name="platformName"></param>
        /// <returns></returns>
        private IntPtr CheckCurrentAppDomainBin(string fileName, string platformName)
        {
            var baseDirectory = Path.Combine(Path.GetFullPath(AppDomain.CurrentDomain.BaseDirectory), "bin");

            if (Directory.Exists(baseDirectory))
            {
                LibraryLoaderTrace.TraceInformation("Checking current application domain's bin location '{0}' for '{1}' on platform {2}.", baseDirectory, fileName, platformName);
                return(InternalLoadLibrary(baseDirectory, platformName, fileName));
            }
            else
            {
                LibraryLoaderTrace.TraceInformation("No bin directory exists under the current application domain's location, skipping.");
                return(IntPtr.Zero);
            }
        }
Пример #3
0
 public bool FreeLibrary(string fileName)
 {
     fileName = FixUpLibraryName(fileName);
     lock (syncLock) {
         if (!IsLibraryLoaded(fileName))
         {
             LibraryLoaderTrace.TraceWarning("Failed to free library \"{0}\" because it is not loaded", fileName);
             return(false);
         }
         if (logic.FreeLibrary(loadedAssemblies[fileName]))
         {
             loadedAssemblies.Remove(fileName);
             return(true);
         }
         return(false);
     }
 }
Пример #4
0
        public IntPtr LoadLibrary(string fileName, string platformName = null)
        {
            fileName = FixUpLibraryName(fileName);
            lock (syncLock) {
                if (!loadedAssemblies.ContainsKey(fileName))
                {
                    if (platformName == null)
                    {
                        platformName = SystemManager.GetPlatformName();
                    }

                    LibraryLoaderTrace.TraceInformation("Current platform: " + platformName);

                    IntPtr dllHandle = CheckCustomSearchPath(fileName, platformName);
                    if (dllHandle == IntPtr.Zero)
                    {
                        dllHandle = CheckExecutingAssemblyDomain(fileName, platformName);
                    }
                    if (dllHandle == IntPtr.Zero)
                    {
                        dllHandle = CheckCurrentAppDomain(fileName, platformName);
                    }
                    if (dllHandle == IntPtr.Zero)
                    {
                        dllHandle = CheckCurrentAppDomainBin(fileName, platformName);
                    }
                    if (dllHandle == IntPtr.Zero)
                    {
                        dllHandle = CheckWorkingDirecotry(fileName, platformName);
                    }

                    if (dllHandle != IntPtr.Zero)
                    {
                        loadedAssemblies[fileName] = dllHandle;
                    }
                    else
                    {
                        throw new DllNotFoundException(string.Format("Failed to find library \"{0}\" for platform {1}.", fileName, platformName));
                    }
                }

                return(loadedAssemblies[fileName]);
            }
        }
 public bool FreeLibrary(IntPtr libraryHandle)
 {
     try {
         LibraryLoaderTrace.TraceInformation("Trying to free native library with handle {0} ...", libraryHandle);
         var isSuccess = WindowsFreeLibrary(libraryHandle);
         if (isSuccess)
         {
             LibraryLoaderTrace.TraceInformation("Successfully freed native library with handle {0}.", libraryHandle);
         }
         else
         {
             LibraryLoaderTrace.TraceError("Failed to free native library with handle {0}.\r\nCheck windows event log.", libraryHandle);
         }
         return(isSuccess);
     } catch (Exception e) {
         var lastError = WindowsGetLastError();
         LibraryLoaderTrace.TraceError("Failed to free native library with handle {0}.\r\nLast Error:{1}\r\nCheck inner exception and\\or windows event log.\r\nInner Exception: {2}", libraryHandle, lastError, e.ToString());
         return(false);
     }
 }
        public IntPtr LoadLibrary(string fileName)
        {
            var libraryHandle = IntPtr.Zero;

            try {
                LibraryLoaderTrace.TraceInformation("Trying to load native library \"{0}\"...", fileName);
                libraryHandle = WindowsLoadLibrary(fileName);
                if (libraryHandle != IntPtr.Zero)
                {
                    LibraryLoaderTrace.TraceInformation("Successfully loaded native library \"{0}\", handle = {1}.", fileName, libraryHandle);
                }
                else
                {
                    LibraryLoaderTrace.TraceError("Failed to load native library \"{0}\".\r\nCheck windows event log.", fileName);
                }
            } catch (Exception e) {
                var lastError = WindowsGetLastError();
                LibraryLoaderTrace.TraceError("Failed to load native library \"{0}\".\r\nLast Error:{1}\r\nCheck inner exception and\\or windows event log.\r\nInner Exception: {2}", fileName, lastError, e.ToString());
            }

            return(libraryHandle);
        }
 public IntPtr GetProcAddress(IntPtr libraryHandle, string functionName)
 {
     try {
         LibraryLoaderTrace.TraceInformation("Trying to load native function \"{0}\" from the library with handle {1}...",
                                             functionName, libraryHandle);
         var functionHandle = WindowsGetProcAddress(libraryHandle, functionName);
         if (functionHandle != IntPtr.Zero)
         {
             LibraryLoaderTrace.TraceInformation("Successfully loaded native function \"{0}\", function handle = {1}.",
                                                 functionName, functionHandle);
         }
         else
         {
             LibraryLoaderTrace.TraceError("Failed to load native function \"{0}\", function handle = {1}",
                                           functionName, functionHandle);
         }
         return(functionHandle);
     } catch (Exception e) {
         var lastError = WindowsGetLastError();
         LibraryLoaderTrace.TraceError("Failed to free native library with handle {0}.\r\nLast Error:{1}\r\nCheck inner exception and\\or windows event log.\r\nInner Exception: {2}", libraryHandle, lastError, e.ToString());
         return(IntPtr.Zero);
     }
 }
        public IntPtr GetProcAddress(IntPtr libraryHandle, string functionName)
        {
            UnixGetLastError(); // Clearing previous errors
            LibraryLoaderTrace.TraceInformation("Trying to load native function \"{0}\" from the library with handle {1}...",
                                                functionName, libraryHandle);
            var functionHandle = UnixGetProcAddress(libraryHandle, functionName);
            var errorPointer   = UnixGetLastError();

            if (errorPointer != IntPtr.Zero)
            {
                throw new Exception("dlsym: " + Marshal.PtrToStringAnsi(errorPointer));
            }
            if (functionHandle != IntPtr.Zero && errorPointer == IntPtr.Zero)
            {
                LibraryLoaderTrace.TraceInformation("Successfully loaded native function \"{0}\", function handle = {1}.",
                                                    functionName, functionHandle);
            }
            else
            {
                LibraryLoaderTrace.TraceError("Failed to load native function \"{0}\", function handle = {1}, error pointer = {2}",
                                              functionName, functionHandle, errorPointer);
            }
            return(functionHandle);
        }