예제 #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.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);
        }
예제 #2
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);
        }