Ejemplo n.º 1
0
        private static IntPtr GetFunctionPointer(IntPtr nativeLibraryHandle, string functionName)
        {
#if NET45
            switch (LibraryLoader.GetPlatformId())
            {
            case PlatformID.MacOSX:
                return(MacNativeMethods.dlsym(nativeLibraryHandle, functionName));

            case PlatformID.Unix:
                return(LinuxNativeMethods.dlsym(nativeLibraryHandle, functionName));

            case PlatformID.Win32NT:
            case PlatformID.Win32S:
            case PlatformID.Win32Windows:
                return(WindowsNativeMethods.GetProcAddress(nativeLibraryHandle, functionName));

            default:
                throw new PlatformNotSupportedException();
            }
#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
        }
Ejemplo n.º 2
0
        public static bool UnloadLibrary(IntPtr handle)
        {
            if (handle == IntPtr.Zero)
            {
                return(false);
            }

            switch (ffmpeg.GetPlatform())
            {
            case FFMpegPlatform.macOS:
                //lib = MacNativeMethods.dlopen("lib" + libraryName, MacNativeMethods.RTLD_NOW);
                return(MacNativeMethods.dlclose(handle) == 0);

            case FFMpegPlatform.windows:
                return(WindowsNativeMethods.FreeLibrary(handle));

            case FFMpegPlatform.unix:
                return(LinuxNativeMethods.dlclose(handle) == 0);

            //case FFMpegPlatform.iOS:
            //return false;
            //lib = iOSNativeMethods.dlopen("lib" + libraryName, MacNativeMethods.RTLD_NOW);
            //break;
            default:
                throw new PlatformNotSupportedException();
            }

            throw new PlatformNotSupportedException();
        }
Ejemplo n.º 3
0
        /// <summary>
        ///     Attempts to load a native library.
        /// </summary>
        /// <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)
        {
            switch (GetPlatformId())
            {
            case PlatformID.MacOSX:
                return(MacNativeMethods.dlopen(libraryName, MacNativeMethods.RTLD_NOW));

            case PlatformID.Unix:
                return(LinuxNativeMethods.dlopen(libraryName, LinuxNativeMethods.RTLD_NOW));

            case PlatformID.Win32NT:
            case PlatformID.Win32S:
            case PlatformID.Win32Windows:
                return(WindowsNativeMethods.LoadLibrary(libraryName));

            default:
                throw new PlatformNotSupportedException();
            }
        }
Ejemplo n.º 4
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>
        /// <param name="version">Version 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 NET45
            return(WindowsNativeMethods.LoadLibrary(libraryName));
#else
            IntPtr lib = IntPtr.Zero;

            switch (ffmpeg.GetPlatform())
            {
            case FFMpegPlatform.macOS:
                lib = MacNativeMethods.dlopen(libraryName, MacNativeMethods.RTLD_NOW);
                break;

            case FFMpegPlatform.windows:
                lib = WindowsNativeMethods.LoadLibrary(libraryName);
                break;

            case FFMpegPlatform.unix:
                //TODO: Should I add lib* on Unix too?
                lib = LinuxNativeMethods.dlopen(libraryName, LinuxNativeMethods.RTLD_NOW);
                break;

            case FFMpegPlatform.iOS:
                lib = iOSNativeMethods.dlopen("lib" + libraryName, MacNativeMethods.RTLD_NOW);
                break;

            default:
                throw new PlatformNotSupportedException();
            }

            /*
             *          if (lib == IntPtr.Zero) {
             *  if(ffmpeg.GetPlatform() == FFMpegPlatform.windows){
             *      var winErr = Marshal.GetLastWin32Error();
             *      throw new Exception("Unable to load library " + libraryName + " error code " + winErr);
             *  }
             *                  throw new Exception("Unable to load library " + libraryName);
             *          }
             */

            return(lib);
#endif
        }
        /// <summary>
        ///     Attempts to load a native library.
        /// </summary>
        /// <param name="path">Path of the library.</param>
        /// <param name="libraryName">Name of the library.</param>
        /// <param name="version">Version 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 NET4
            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
        }
Ejemplo n.º 6
0
        private static IntPtr GetFunctionPointer(IntPtr nativeLibraryHandle, string functionName)
        {
#if NET45 || NET40
            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
        }
Ejemplo n.º 7
0
        private static IntPtr GetFunctionPointer(IntPtr nativeLibraryHandle, string functionName)
        {
#if NET45
            return(WindowsNativeMethods.GetProcAddress(nativeLibraryHandle, functionName));
#else
            switch (ffmpeg.GetPlatform())
            {
            case FFMpegPlatform.macOS:
                return(MacNativeMethods.dlsym(nativeLibraryHandle, functionName));

            case FFMpegPlatform.iOS:
                return(iOSNativeMethods.dlsym(nativeLibraryHandle, functionName));

            case FFMpegPlatform.windows:
                return(WindowsNativeMethods.GetProcAddress(nativeLibraryHandle, functionName));

            case FFMpegPlatform.unix:
                return(LinuxNativeMethods.dlsym(nativeLibraryHandle, functionName));
            }

            throw new PlatformNotSupportedException();
#endif
        }