Esempio n. 1
0
        private static IntPtr GetFunctionPointer(IntPtr nativeLibraryHandle, string functionName)
        {
#if NET45 || NET40
            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
        }
Esempio n. 2
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();
            }
        }