예제 #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);
        }
예제 #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));
                }

                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);
        }