public static T LoadFunction <T>(IntPtr library, string function)
        {
            var ret = IntPtr.Zero;

            if (CurrentPlatform.OS == OS.Windows)
            {
                ret = Windows.GetProcAddress(library, function);
            }
            else if (CurrentPlatform.OS == OS.MacOSX)
            {
                ret = OSX.dlsym(library, function);
            }
            else
            {
                ret = Linux.dlsym(library, function);
            }

            if (ret == IntPtr.Zero)
            {
                return(default(T));
            }

            // TODO: Use the function bellow once Protobuild gets axed
            // requires .NET Framework 4.5.1 and its useful for corert
            // return Marshal.GetDelegateForFunctionPointer<T>(ret);

            return((T)(object)Marshal.GetDelegateForFunctionPointer(ret, typeof(T)));
        }
Esempio n. 2
0
        public static T LoadFunction <T> (IntPtr library, string function, bool throwIfNotFound = true)
        {
            var ret = IntPtr.Zero;

            if (CurrentPlatform.OS == OS.Windows)
            {
                ret = Windows.GetProcAddress(library, function);
            }
            else if (CurrentPlatform.OS == OS.MacOSX)
            {
                ret = OSX.dlsym(library, function);
            }
            else
            {
                ret = Linux.dlsym(library, function);
            }

            if (ret == IntPtr.Zero)
            {
                if (throwIfNotFound)
                {
                    throw new EntryPointNotFoundException(function);
                }

                return(default(T));
            }

#if NETSTANDARD
            return(Marshal.GetDelegateForFunctionPointer <T> (ret));
#else
            return(( T )( object )Marshal.GetDelegateForFunctionPointer(ret, typeof(T)));
#endif
        }
Esempio n. 3
0
        public static bool TryLoadFunction <T>(
            IntPtr library,
            string functionName,
            [NotNullWhen(true)] out T?func)
            where T : Delegate
        {
            IntPtr ret;

            if (PlatformInfo.CurrentOS == PlatformInfo.OS.Windows)
            {
                ret = Windows.GetProcAddress(library, functionName);
            }
            else if (PlatformInfo.CurrentOS == PlatformInfo.OS.MacOSX)
            {
                ret = OSX.dlsym(library, functionName);
            }
            else
            {
                ret = Linux.dlsym(library, functionName);
            }

            if (ret == IntPtr.Zero)
            {
                func = default;
                return(false);
            }

            func = Marshal.GetDelegateForFunctionPointer <T>(ret);
            return(true);
        }
Esempio n. 4
0
        public static T LoadFunction <T>(IntPtr library, string function, bool throwIfNotFound = false)
        {
            IntPtr result;

            if (Environment.OSVersion.IsWindows())
            {
                result = Windows.GetProcAddress(library, function);
            }
            else if (Environment.OSVersion.IsMacOSX())
            {
                result = OSX.dlsym(library, function);
            }
            else
            {
                result = Linux.dlsym(library, function);
            }

            if (result == IntPtr.Zero)
            {
                if (throwIfNotFound)
                {
                    throw new EntryPointNotFoundException(function);
                }

                return(default(T));
            }

            return(Marshal.GetDelegateForFunctionPointer <T>(result));
        }
            public static IntPtr GetProcAddress(IntPtr library, string function)
            {
                var num = !IsWindows
                    ? !IsOSX?Linux.dlsym(library, function) : OSX.dlsym(library, function)
                              : Windows.GetProcAddress(library, function);

                return(num);
            }
Esempio n. 6
0
        public static T LoadFunction <T>(IntPtr library, string function)
        {
            var ret = RuntimeInformation.IsOSPlatform(OSPlatform.Windows)
                ? Windows.GetProcAddress(library, function)
                : Linux.dlsym(library, function);

            return(Marshal.GetDelegateForFunctionPointer <T>(ret));
        }
Esempio n. 7
0
        /// <summary>
        /// Loads symbol in a platform specific way.
        /// </summary>
        /// <param name="symbolName"></param>
        /// <returns></returns>
        private IntPtr LoadSymbol(string symbolName)
        {
            IntPtr pResult = IntPtr.Zero;

            if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) /* PlatformApis.IsWindows */
            {
                int    ErrCode  = 0;
                string errorMsg = "";

                // See http://stackoverflow.com/questions/10473310 for background on this.
                if (Environment.Is64BitProcess) /* PlatformApis.Is64Bit */
                {
                    pResult = Windows.GetProcAddress(this.handle, symbolName);
                    if (pResult == IntPtr.Zero)
                    {
                        ErrCode  = Marshal.GetLastWin32Error();
                        errorMsg = Windows.GetLastErrMsg((uint)ErrCode);
                        Console.WriteLine("Error while loading function: " + symbolName + "\n\t" + errorMsg);
                    }
                    return(pResult);
                }
                else
                {
                    // Yes, we could potentially predict the size... but it's a lot simpler to just try
                    // all the candidates. Most functions have a suffix of @0, @4 or @8 so we won't be trying
                    // many options - and if it takes a little bit longer to fail if we've really got the wrong
                    // library, that's not a big problem. This is only called once per function in the native library.
                    symbolName = "_" + symbolName + "@";
                    for (int stackSize = 0; stackSize < 128; stackSize += 4)
                    {
                        pResult = Windows.GetProcAddress(this.handle, symbolName + stackSize);
                        if (pResult != IntPtr.Zero)
                        {
                            return(pResult);
                        }
                    }
                    // Fail.
                    return(IntPtr.Zero);
                }
            }
            if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
            {
                if (InteropRuntimeConfig.IsRunningOnMono) /* PlatformApis.IsMono */
                {
                    return(Mono.dlsym(this.handle, symbolName));
                }
                if (RuntimeInformation.FrameworkDescription.StartsWith(".NET Core")) /* PlatformApis.IsNetCore */
                {
                    return(CoreCLR.dlsym(this.handle, symbolName));
                }
                return(Linux.dlsym(this.handle, symbolName));
            }
            if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))  /* PlatformApis.IsMacOSX */
            {
                return(MacOSX.dlsym(this.handle, symbolName));
            }
            throw new InvalidOperationException("Unsupported platform.");
        }
Esempio n. 8
0
            public static IntPtr GetProcAddress(IntPtr library, string function)
            {
                var num = !IsWindows ? (!IsOSX ? Linux.dlsym(library, function) : OSX.dlsym(library, function)) : Windows.GetProcAddress(library, function);

                if (num == IntPtr.Zero)
                {
                    Logger.Warn("Função não encontrada: " + function);
                }
                return(num);
            }
Esempio n. 9
0
 /// <summary>
 /// Loads symbol in a platform specific way.
 /// </summary>
 /// <param name="symbolName"></param>
 /// <returns></returns>
 public IntPtr LoadSymbol(string symbolName)
 {
     if (PlatformApis.IsLinux)
     {
         return(Linux.dlsym(this.handle, symbolName));
     }
     if (PlatformApis.IsMacOSX)
     {
         return(MacOSX.dlsym(this.handle, symbolName));
     }
     throw new InvalidOperationException("Unsupported platform.");
 }
Esempio n. 10
0
        /// <summary>
        /// Loads symbol in a platform specific way.
        /// </summary>
        /// <param name="symbolName"></param>
        /// <returns></returns>
        private IntPtr LoadSymbol(string symbolName)
        {
            if (PlatformApis.IsWindows)
            {
                // See http://stackoverflow.com/questions/10473310 for background on this.

                //if (PlatformApis.Is64Bit)
                //{
                //    return Windows.GetProcAddress(this.handle, symbolName);
                //}
                //else
                //{
                //    // Yes, we could potentially predict the size... but it's a lot simpler to just try
                //    // all the candidates. Most functions have a suffix of @0, @4 or @8 so we won't be trying
                //    // many options - and if it takes a little bit longer to fail if we've really got the wrong
                //    // library, that's not a big problem. This is only called once per function in the native library.
                //    symbolName = "_" + symbolName + "@";
                //    for (int stackSize = 0; stackSize < 128; stackSize += 4)
                //    {
                //        IntPtr candidate = Windows.GetProcAddress(this.handle, symbolName + stackSize);
                //        if (candidate != IntPtr.Zero)
                //        {
                //            return candidate;
                //        }
                //    }
                //    // Fail.
                //    return IntPtr.Zero;
                //}

                // VLFD.dll 比较特殊
                // 虽然是32位的,但符号和64位相同
                return(Windows.GetProcAddress(this.handle, symbolName));
            }
            if (PlatformApis.IsLinux)
            {
                if (PlatformApis.IsMono)
                {
                    return(Mono.dlsym(this.handle, symbolName));
                }
                if (PlatformApis.IsNetCore)
                {
                    return(CoreCLR.dlsym(this.handle, symbolName));
                }
                return(Linux.dlsym(this.handle, symbolName));
            }
            if (PlatformApis.IsMacOSX)
            {
                return(MacOSX.dlsym(this.handle, symbolName));
            }
            throw new InvalidOperationException("Unsupported platform.");
        }
Esempio n. 11
0
        /// <summary>
        ///     Loads symbol in a platform specific way.
        /// </summary>
        /// <param name="symbolName"></param>
        /// <returns></returns>
        public IntPtr LoadSymbol(string symbolName)
        {
            if (IsWindows)
            {
                // See http://stackoverflow.com/questions/10473310 for background on this.
                if (Is64Bit)
                {
                    return(Windows.GetProcAddress(NativeLibraryHandle, symbolName));
                }
                // Yes, we could potentially predict the size... but it's a lot simpler to just try
                // all the candidates. Most functions have a suffix of @0, @4 or @8 so we won't be trying
                // many options - and if it takes a little bit longer to fail if we've really got the wrong
                // library, that's not a big problem. This is only called once per function in the native library.
                symbolName = "_" + symbolName + "@";
                for (var stackSize = 0; stackSize < 128; stackSize += 4)
                {
                    var candidate = Windows.GetProcAddress(NativeLibraryHandle, symbolName + stackSize);
                    if (candidate != IntPtr.Zero)
                    {
                        return(candidate);
                    }
                }

                // Fail.
                return(IntPtr.Zero);
            }

            if (IsLinux)
            {
                if (IsMono)
                {
                    return(Mono.dlsym(NativeLibraryHandle, symbolName));
                }
                if (IsNetCore)
                {
                    return(CoreCLR.dlsym(NativeLibraryHandle, symbolName));
                }
                return(Linux.dlsym(NativeLibraryHandle, symbolName));
            }

            if (IsMacOSPlatform)
            {
                return(MacOSX.dlsym(NativeLibraryHandle, symbolName));
            }
            throw new InvalidOperationException("Unsupported platform.");
        }
Esempio n. 12
0
        public static IntPtr GetProcAddress(IntPtr library, string function)
        {
            var ret = IntPtr.Zero;

            if (IsWindows)
            {
                ret = Windows.GetProcAddress(library, function);
            }
            else if (IsOSX)
            {
                ret = OSX.dlsym(library, function);
            }
            else
            {
                ret = Linux.dlsym(library, function);
            }

            return(ret);
        }
Esempio n. 13
0
        public static T LoadFunction <T>(IntPtr library, string function)
        {
            IntPtr ret;

            if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
            {
                ret = Windows.GetProcAddress(library, function);
            }
            else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
            {
                ret = OSX.dlsym(library, function);
            }
            else
            {
                ret = Linux.dlsym(library, function);
            }

            if (ret == IntPtr.Zero)
            {
                throw new EntryPointNotFoundException(function);
            }

            return(Marshal.GetDelegateForFunctionPointer <T>(ret));
        }
Esempio n. 14
0
    public static IntPtr GetProcAddress(IntPtr library, string function)
    {
        var ret = IntPtr.Zero;

        if (IsWindows)
        {
            ret = Windows.GetProcAddress(library, function);
        }
        else if (IsOSX)
        {
            ret = OSX.dlsym(library, function);
        }
        else
        {
            ret = Linux.dlsym(library, function);
        }

        if (ret == IntPtr.Zero)
        {
            Console.WriteLine("[WARNING] Function not found: " + function);
        }

        return(ret);
    }
Esempio n. 15
0
        public static T LoadFunction <T>(IntPtr library, string function)
        {
            var ret = IntPtr.Zero;

            if (IsWindows)
            {
                ret = Windows.GetProcAddress(library, function);
            }
            else if (IsOSX)
            {
                ret = OSX.dlsym(library, function);
            }
            else
            {
                ret = Linux.dlsym(library, function);
            }

            if (ret == IntPtr.Zero)
            {
                return(default(T));
            }

            return(Marshal.GetDelegateForFunctionPointer <T>(ret));
        }
Esempio n. 16
0
        private static IntPtr LoadSymbol(IntPtr handle, string symbolName, out string errorMsg)
        {
            errorMsg = null;
            if (Platform.IsWindows)
            {
                // See http://stackoverflow.com/questions/10473310 for background on this.
                if (Platform.Is64Bit)
                {
                    return(Windows.GetProcAddress(handle, symbolName));
                }
                else
                {
                    IntPtr candidate = Windows.GetProcAddress(handle, symbolName);
                    if (candidate != IntPtr.Zero)
                    {
                        return(candidate);
                    }

                    // Yes, we could potentially predict the size... but it's a lot simpler to just try
                    // all the candidates. Most functions have a suffix of @0, @4 or @8 so we won't be trying
                    // many options - and if it takes a little bit longer to fail if we've really got the wrong
                    // library, that's not a big problem. This is only called once per function in the native library.
                    symbolName = "_" + symbolName + "@";
                    for (int stackSize = 0; stackSize < 128; stackSize += 4)
                    {
                        candidate = Windows.GetProcAddress(handle, symbolName + stackSize);
                        if (candidate != IntPtr.Zero)
                        {
                            return(candidate);
                        }
                    }
                    // Fail.
                    return(IntPtr.Zero);
                }
            }
            if (Platform.IsLinux)
            {
                if (Platform.IsMono)
                {
                    var monoAddr = Mono.dlsym(handle, symbolName);
                    errorMsg = Marshal.PtrToStringAnsi(Mono.dlerror());
                    return(monoAddr);
                }
                if (Platform.IsNetCore)
                {
                    var coreAddr = CoreClr.dlsym(handle, symbolName);
                    errorMsg = Marshal.PtrToStringAnsi(CoreClr.dlerror());
                    return(coreAddr);
                }
                var addr = Linux.dlsym(handle, symbolName);
                errorMsg = Marshal.PtrToStringAnsi(Linux.dlerror());
                return(addr);
            }
            if (Platform.IsMacOSX)
            {
                var addr = MacOsx.dlsym(handle, symbolName);
                errorMsg = Marshal.PtrToStringAnsi(MacOsx.dlerror());
                return(addr);
            }
            throw new InvalidOperationException("Unsupported platform.");
        }