コード例 #1
0
        private static NativeLibrary Load(string name, bool failOnError, NativeLibraryLoadOptions options, Assembly callingAssembly)
        {
            if (name == null)
            {
                throw new ArgumentNullException("name");
            }

            if (string.IsNullOrWhiteSpace(name))
            {
                throw new ArgumentException("Name cannot be empty.", "name");
            }

            bool addExtension = !Path.HasExtension(name) && string.IsNullOrWhiteSpace(Path.GetDirectoryName(name));
            bool windows;

            switch (Environment.OSVersion.Platform)
            {
            case PlatformID.MacOSX:
            case PlatformID.Unix:
                windows = false;
                if (addExtension)
                {
                    name += ".so";
                }
                break;

            case PlatformID.Win32NT:
                windows = true;
                if (addExtension)
                {
                    name += ".dll";
                }
                break;

            default:
                throw new ApplicationException(ErrorUnsupportedPlatform);
            }

            string path;

            if (!Path.IsPathRooted(name))
            {
                path = SearchLocations(name, options, windows, callingAssembly);
            }
            else
            {
                path = name;
            }

            var library = new NativeLibrary(path, windows, failOnError);

            if (library._handle == IntPtr.Zero)
            {
                return(null);
            }

            return(library);
        }
コード例 #2
0
        private static string SearchLocations(string name, NativeLibraryLoadOptions options, bool windows, Assembly callingAssembly)
        {
            if ((options & NativeLibraryLoadOptions.SearchAll) == 0)
            {
                return(name);
            }

            bool nameIsPath = Path.IsPathRooted(name) || !string.IsNullOrEmpty(Path.GetDirectoryName(name));

            var locations = new NativeLibraryLoadOptions[] {
                NativeLibraryLoadOptions.SearchAssemblyDirectory,
                NativeLibraryLoadOptions.SearchExecutableDirectory,
                NativeLibraryLoadOptions.SearchCurrentDirectory
            };

            string assemblyDirectory = callingAssembly != null?GetAssemblyDirectory(callingAssembly) : null;

            foreach (NativeLibraryLoadOptions location in locations)
            {
                string path = null;
                switch (location & options)
                {
                case NativeLibraryLoadOptions.SearchAssemblyDirectory:
                    path = SearchFile(assemblyDirectory, name, nameIsPath, windows);
                    break;

                case NativeLibraryLoadOptions.SearchExecutableDirectory:
                    path = SearchFile(GetExecutableDirectory(), name, nameIsPath, windows);
                    break;

                case NativeLibraryLoadOptions.SearchCurrentDirectory:
                    path = SearchFile(Directory.GetCurrentDirectory(), name, nameIsPath, windows);
                    break;
                }
                if (path != null)
                {
                    return(path);
                }
            }

            return(name);
        }
コード例 #3
0
 /// <summary>
 /// Loads a native (unmanaged) dynamically loaded library.
 /// </summary>
 /// <param name="name">Library name. Can be a full path as well.</param>
 /// <param name="options">Load options.</param>
 /// <param name="library">When the method returns, contains an instance of <see cref="NativeLibrary"/> on success, or null on failure.</returns>
 /// <returns>True on succes; false on failure.</returns>
 public static bool TryLoad(string name, NativeLibraryLoadOptions options, out NativeLibrary library)
 {
     library = Load(name, false, options, ((options & NativeLibraryLoadOptions.SearchAssemblyDirectory) != 0) ? Assembly.GetCallingAssembly() : null);
     return(library != null);
 }
コード例 #4
0
 /// <summary>
 /// Loads a native (unmanaged) dynamically loaded library. A return value indicates whether the method succeeded.
 /// </summary>
 /// <param name="name">Library name. Can be a full path as well.</param>
 /// <param name="options">Load options.</param>
 /// <returns>An instance of <see cref="NativeLibrary"/>.</returns>
 public static NativeLibrary Load(string name, NativeLibraryLoadOptions options)
 {
     return(Load(name, true, options, ((options & NativeLibraryLoadOptions.SearchAssemblyDirectory) != 0) ? Assembly.GetCallingAssembly() : null));
 }