Example #1
0
        // Not yet implemented.
        // static readonly Lazy<ConcurrentDictionary<string, IntPtr>> LibraryHandlesByFullPath = new Lazy<ConcurrentDictionary<string, IntPtr>>(() => new ConcurrentDictionary<string, IntPtr>(StringComparer.OrdinalIgnoreCase), LazyThreadSafetyMode.PublicationOnly);

        /// <summary>
        /// Searches known directories for the provided file basename (or returns true if one is already loaded)
        /// basename 'imageflow' -> imageflow.dll, libimageflow.so, libimageflow.dylib.
        /// Basename is case-sensitive
        /// </summary>
        /// <param name="basename">The library name sans extension or "lib" prefix</param>
        /// <param name="log">Where to log attempts at assembly search and load</param>
        /// <param name="handle">Where to store the loaded library handle</param>
        /// <param name="customSearchDirectory">Provide this if you want a custom search folder</param>
        /// <returns>True if previously or successfully loaded</returns>
        public static bool TryLoadByBasename(string basename, ILibraryLoadLogger log, out IntPtr handle, IEnumerable <string> customSearchDirectories = null)
        {
            if (string.IsNullOrEmpty(basename))
            {
                throw new ArgumentNullException("filenameWithoutExtension");
            }

            if (LibraryHandlesByBasename.Value.TryGetValue(basename, out handle))
            {
                log.NotifyAttempt(basename, null, true, true, 0);
                return(true);
            }
            lock (LibraryHandlesByBasename)
            {
                if (LibraryHandlesByBasename.Value.TryGetValue(basename, out handle))
                {
                    log.NotifyAttempt(basename, null, true, true, 0);
                    return(true);
                }
                var success = TryLoadByBasenameInternal(basename, log, out handle, customSearchDirectories);
                if (success)
                {
                    LibraryHandlesByBasename.Value[basename] = handle;
                }
                return(success);
            }
        }
Example #2
0
        // Not yet implemented.
        // static readonly Lazy<ConcurrentDictionary<string, IntPtr>> LibraryHandlesByFullPath = new Lazy<ConcurrentDictionary<string, IntPtr>>(() => new ConcurrentDictionary<string, IntPtr>(StringComparer.OrdinalIgnoreCase), LazyThreadSafetyMode.PublicationOnly);

        /// <summary>
        /// Searches known directories for the provided file basename (or returns true if one is already loaded)
        /// basename 'imageflow' -> imageflow.exe, imageflow
        /// Basename is case-sensitive
        /// </summary>
        /// <param name="basename">The executable name sans extension</param>
        /// <param name="log">Where to log attempts at assembly search and load</param>
        /// <param name="customSearchDirectory">Provide this if you want a custom search folder</param>
        /// <returns>True if previously or successfully loaded</returns>
        internal static bool TryLoadByBasename(string basename, ILibraryLoadLogger log, out string exePath,
                                               IEnumerable <string> customSearchDirectories = null)
        {
            if (string.IsNullOrEmpty(basename))
            {
                throw new ArgumentNullException("filenameWithoutExtension");
            }

            if (ExecutablePathsByName.Value.TryGetValue(basename, out exePath))
            {
                return(true);
            }

            var filename = GetFilenameWithoutDirectory(basename);

            exePath = null;
            foreach (var path in RuntimeFileLocator.SearchPossibilitiesForFile(filename, customSearchDirectories))
            {
                if (!File.Exists(path))
                {
                    log.NotifyAttempt(basename, path, false, false, 0);
                }
                else
                {
                    exePath = path;
                    ExecutablePathsByName.Value[basename] = exePath;
                    return(true);
                }
            }
            return(false);
        }
Example #3
0
        private static bool TryLoadByBasenameInternal(string basename, ILibraryLoadLogger log, out IntPtr handle, IEnumerable <string> customSearchDirectories = null)
        {
            var filename = GetFilenameWithoutDirectory(basename);

            foreach (var path in RuntimeFileLocator.SearchPossibilitiesForFile(filename, customSearchDirectories))
            {
                if (!File.Exists(path))
                {
                    log.NotifyAttempt(basename, path, false, false, 0);
                }
                else
                {
                    var success = LoadLibrary(path, out handle, out var errorCode);
                    log.NotifyAttempt(basename, path, true, false, errorCode);
                    if (success)
                    {
                        return(true);
                    }
                }
            }
            handle = IntPtr.Zero;
            return(false);
        }