示例#1
0
        /// <summary>
        ///     Attempts to load a native library.
        /// </summary>
        /// <param name="path">Path of the library.</param>
        /// <param name="libraryName">Name of the library.</param>
        /// <returns>
        ///     A handle to the library when found; otherwise, <see cref="IntPtr.Zero" />.
        /// </returns>
        /// <remarks>
        ///     This function may return a null handle. If it does, individual functions loaded from it will throw a
        ///     DllNotFoundException,
        ///     but not until an attempt is made to actually use the function (rather than load it). This matches how PInvokes
        ///     behave.
        /// </remarks>
        public static IntPtr LoadNativeLibrary(string libraryName)
        {
#if NET46
            return(WindowsNativeMethods.LoadLibrary(libraryName));
#else
            if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
            {
                return(WindowsNativeMethods.LoadLibrary(libraryName));
            }
            if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
            {
                return(LinuxNativeMethods.dlopen(libraryName, LinuxNativeMethods.RTLD_NOW));
            }
            if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
            {
                return(MacNativeMethods.dlopen(libraryName, MacNativeMethods.RTLD_NOW));
            }
            throw new PlatformNotSupportedException();
#endif
        }
        private static IntPtr GetFunctionPointer(IntPtr nativeLibraryHandle, string functionName)
        {
#if NET46
            return(WindowsNativeMethods.GetProcAddress(nativeLibraryHandle, functionName));
#else
            if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
            {
                return(LinuxNativeMethods.dlsym(nativeLibraryHandle, functionName));
            }
            if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
            {
                return(MacNativeMethods.dlsym(nativeLibraryHandle, functionName));
            }
            if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
            {
                return(WindowsNativeMethods.GetProcAddress(nativeLibraryHandle, functionName));
            }
            throw new PlatformNotSupportedException();
#endif
        }