Beispiel #1
0
        /// <summary>
        /// Loads specified DLL.
        /// </summary>
        /// <returns>Null when successful; error message otherwise.</returns>
        public static string Load(string dllPath)
        {
            if (Os.IsWindows)
            {
                return(NativeMethodsWindows.LoadLibrary(dllPath) == IntPtr.Zero
                    ? FormatWin32Error(Marshal.GetLastWin32Error()) ?? "Unknown error"
                    : null);
            }

            if (Os.IsLinux)
            {
                if (Os.IsMono)
                {
                    return(NativeMethodsMono.dlopen(dllPath, RtldGlobal | RtldLazy) == IntPtr.Zero
                        ? GetErrorText(NativeMethodsMono.dlerror())
                        : null);
                }

                if (Os.IsNetCore)
                {
                    return(NativeMethodsCore.dlopen(dllPath, RtldGlobal | RtldLazy) == IntPtr.Zero
                        ? GetErrorText(NativeMethodsCore.dlerror())
                        : null);
                }

                return(NativeMethodsLinux.dlopen(dllPath, RtldGlobal | RtldLazy) == IntPtr.Zero
                    ? GetErrorText(NativeMethodsLinux.dlerror())
                    : null);
            }

            throw new InvalidOperationException("Unsupported OS: " + Environment.OSVersion);
        }
Beispiel #2
0
        /// <summary>
        /// Loads specified DLL.
        /// </summary>
        /// <returns>Library handle and error message.</returns>
        public static KeyValuePair <IntPtr, string> Load(string dllPath)
        {
            if (Os.IsWindows)
            {
                var ptr = NativeMethodsWindows.LoadLibrary(dllPath);
                return(new KeyValuePair <IntPtr, string>(ptr, ptr == IntPtr.Zero
                    ? FormatWin32Error(Marshal.GetLastWin32Error()) ?? "Unknown error"
                    : null));
            }

            if (Os.IsMacOs)
            {
                var ptr = NativeMethodsMacOs.dlopen(dllPath, RtldGlobal | RtldLazy);
                return(new KeyValuePair <IntPtr, string>(ptr, ptr == IntPtr.Zero
                    ? GetErrorText(NativeMethodsMacOs.dlerror())
                    : null));
            }

            if (Os.IsLinux)
            {
                if (Os.IsMono)
                {
                    var ptr = NativeMethodsMono.dlopen(dllPath, RtldGlobal | RtldLazy);
                    return(new KeyValuePair <IntPtr, string>(ptr, ptr == IntPtr.Zero
                        ? GetErrorText(NativeMethodsMono.dlerror())
                        : null));
                }

                // Depending on the Linux distro, dlopen is either present in libdl or in libcoreclr.
                try
                {
                    var ptr = NativeMethodsLinuxLibcoreclr.dlopen(dllPath, RtldGlobal | RtldLazy);
                    return(new KeyValuePair <IntPtr, string>(ptr, ptr == IntPtr.Zero
                        ? GetErrorText(NativeMethodsLinuxLibcoreclr.dlerror())
                        : null));
                }
                catch (EntryPointNotFoundException)
                {
                    var ptr = NativeMethodsLinuxLibdl.dlopen(dllPath, RtldGlobal | RtldLazy);
                    return(new KeyValuePair <IntPtr, string>(ptr, ptr == IntPtr.Zero
                        ? GetErrorText(NativeMethodsLinuxLibdl.dlerror())
                        : null));
                }
            }

            throw new InvalidOperationException("Unsupported OS: " + Environment.OSVersion);
        }
Beispiel #3
0
        /// <summary>
        /// Loads specified DLL.
        /// </summary>
        /// <returns>Library handle and error message.</returns>
        public static KeyValuePair <IntPtr, string> Load(string dllPath)
        {
            if (Os.IsWindows)
            {
                var ptr = NativeMethodsWindows.LoadLibrary(dllPath);
                return(new KeyValuePair <IntPtr, string>(ptr, ptr == IntPtr.Zero
                    ? FormatWin32Error(Marshal.GetLastWin32Error()) ?? "Unknown error"
                    : null));
            }

            if (Os.IsMacOs)
            {
                var ptr = NativeMethodsMacOs.dlopen(dllPath, RtldGlobal | RtldLazy);
                return(new KeyValuePair <IntPtr, string>(ptr, ptr == IntPtr.Zero
                    ? GetErrorText(NativeMethodsMacOs.dlerror())
                    : null));
            }

            if (Os.IsLinux)
            {
                if (Os.IsMono)
                {
                    var ptr = NativeMethodsMono.dlopen(dllPath, RtldGlobal | RtldLazy);
                    return(new KeyValuePair <IntPtr, string>(ptr, ptr == IntPtr.Zero
                        ? GetErrorText(NativeMethodsMono.dlerror())
                        : null));
                }

                if (Os.IsNetCore)
                {
                    var ptr = NativeMethodsCore.dlopen(dllPath, RtldGlobal | RtldLazy);
                    return(new KeyValuePair <IntPtr, string>(ptr, ptr == IntPtr.Zero
                        ? GetErrorText(NativeMethodsCore.dlerror())
                        : null));
                }

                var lptr = NativeMethodsLinux.dlopen(dllPath, RtldGlobal | RtldLazy);
                return(new KeyValuePair <IntPtr, string>(lptr, lptr == IntPtr.Zero
                    ? GetErrorText(NativeMethodsLinux.dlerror())
                    : null));
            }

            throw new InvalidOperationException("Unsupported OS: " + Environment.OSVersion);
        }