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 }
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(); }
/// <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(); } }
/// <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 }
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 }
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 }
/// <summary> /// Attempts to load a native library using platform nammig convention. /// </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 LoadNativeLibraryUsingPlatformNamingConvention(string path, string libraryName, int version) { string fullName = ""; IntPtr libPtr = IntPtr.Zero; switch (ffmpeg.GetPlatform()) { case FFMpegPlatform.macOS: fullName = Path.Combine(path, $"libff{libraryName}.{version}.dylib"); libPtr = LoadNativeLibrary(fullName); if (libPtr != IntPtr.Zero) { return(libPtr); } break; case FFMpegPlatform.windows: fullName = Path.Combine(path, $"{libraryName}-{version}.dll"); libPtr = LoadNativeLibrary(fullName); if (libPtr != IntPtr.Zero) { return(libPtr); } fullName = Path.Combine(path, $"{libraryName}-{version}"); libPtr = LoadNativeLibrary(fullName); if (libPtr != IntPtr.Zero) { return(libPtr); } break; case FFMpegPlatform.unix: fullName = Path.Combine(path, $"{libraryName}.so{version}"); return(LoadNativeLibrary(fullName)); case FFMpegPlatform.iOS: fullName = Path.Combine(path, $"{libraryName}.{version}.dylib"); libPtr = LoadNativeLibrary(fullName); if (libPtr != IntPtr.Zero) { return(libPtr); } fullName = Path.Combine(path, $"{libraryName}.{version}.bundle"); return(LoadNativeLibrary(fullName)); } if (libPtr == IntPtr.Zero) { if (ffmpeg.GetPlatform() == FFMpegPlatform.windows) { throw new Exception("Unable to load " + fullName + " load error " + Marshal.GetLastWin32Error()); } throw new Exception(string.Format("Unable to load {0}, loading error {1}", fullName, Marshal.PtrToStringAuto(MacNativeMethods.dlerror()))); } throw new PlatformNotSupportedException(); }