예제 #1
0
        static Sodium()
        {
            _sodiumNative = new EmbeddedNativeLibrary(
                "libsodium",
                new DllInfo(TargetRuntime.Win32, "Saltpack.win_x86.native.libsodium.dll", "Saltpack.win_x86.native.msvcr120.dll"),
                new DllInfo(TargetRuntime.Win64, "Saltpack.win_x64.native.libsodium.dll", "Saltpack.win_x64.native.msvcr120.dll"),
                new DllInfo(TargetRuntime.Mac, "Saltpack.osx_x64.native.libsodium.dylib"),
                new DllInfo(TargetRuntime.Linux, "Saltpack.linux_x64.native.libsodium.so"));

            if (sodium_init() == -1)
            {
                throw new InvalidOperationException("The libsodium library failed to initialize: sodium_init() returned -1.");
            }
        }
예제 #2
0
        /// <summary>
        /// Loads the native library defined by a list of <see cref="DllInfo"/> objects.
        /// </summary>
        /// <param name="libraryName">The name of the library.</param>
        /// <param name="preferEmbeddedOverInstalled">
        /// If true, loading the embedded native library is attempted first and if it fails, then loading
        /// the native library from the operating system's default load paths is attempted. If false,
        /// the installed library is attempted first and the embedded library is attempted second.
        /// </param>
        /// <param name="dllInfos">A collection of <see cref="DllInfo"/> objects.</param>
        /// <returns>True, if the native library was loaded, or false if the library failed to load.</returns>
        public static bool Load(string libraryName, bool preferEmbeddedOverInstalled, params DllInfo[] dllInfos)
        {
            if (dllInfos != null && dllInfos.Any(info => info.TargetRuntime == TargetRuntime.Linux || info.TargetRuntime == TargetRuntime.Mac))
            {
                throw new ArgumentException("Embedding a Mac or Linux native library is not supported with the Load method: one or more DllInfo object had a TargetRuntime with a non-windows value.", "dllInfos");
            }

            if (_runtimeOS != RuntimeOS.Windows)
            {
                return(false);
            }

            var library = new EmbeddedNativeLibrary(libraryName, preferEmbeddedOverInstalled, dllInfos);

            try
            {
                var libraryPointer = library._libraryPointer.Value;
                return(libraryPointer != IntPtr.Zero);
            }
            catch
            {
                return(false);
            }
        }