Пример #1
0
        public static T LoadFunction <T>(string name, DllLoadUtils utils, IntPtr handle) where T : class
        {
            IntPtr address = utils.GetProcAddress(handle, name);

            System.Delegate fn_ptr = Marshal.GetDelegateForFunctionPointer(address, typeof(T));
            return(fn_ptr as T);
        }
Пример #2
0
        /*
         * internal delegate void MyCrossplatformBar(int a, int b);
         *
         *
         * DllLoadUtils dllLoadUtils = IsLinux() ? (DllLoadUtils) new DllLoadUtilsLinux()
         *                                                                           : new DllLoadUtilsWindows();
         * string libraryName;
         *
         * if (IsLinux()) {
         * libraryName = IntPtr.Size == 8 ? "mylib64.so" : "mylib32.so";
         * } else {
         * libraryName = IntPtr.Size == 8 ? "mylib64.dll" : "mylib32.dll";
         * }
         *
         * var dllHandle = dllLoadUtils.LoadLibrary(libraryName);
         * var functionHandle = dllLoadUtils.GetProcAddress(dllHandle, "MyCrossplatformBar");
         *
         * var method = (MyCrossplatformBar) Marshal.GetDelegateForFunctionPointer(
         *         functionHandle, typeof (MyCrossplatformBar));
         * method(10, 15);
         * }
         *
         */

        public static bool LoadLibrary(string name)
        {
            DllLoadUtils dllLoadUtils = null;

            switch (RunningPlatform())
            {
            case PlatformID.MacOSX: dllLoadUtils = (DllLoadUtils) new DllLoadUtilsMac(); break;

            case PlatformID.Unix: dllLoadUtils = (DllLoadUtils) new DllLoadUtilsUnix(); break;

            default: dllLoadUtils = (DllLoadUtils) new DllLoadUtilsWindows(); break;
            }

            IntPtr dllHandle;

            dllHandle = dllLoadUtils.LoadLibrary(name);

            if (dllHandle == IntPtr.Zero)
            {
                Console.WriteLine("Unable to load dll {0}", name);
                return(false);
            }

            ArikaRaw.ResolveSymbols(dllLoadUtils, dllHandle);

            return(true);
        }
Пример #3
0
        static PublisherDelegates()
        {
            dllLoadUtils = DllLoadUtilsFactory.GetDllLoadUtils();
            IntPtr nativelibrary          = dllLoadUtils.LoadLibrary("rcldotnet_publisher");
            IntPtr native_rcl_publish_ptr = dllLoadUtils.GetProcAddress(nativelibrary, "native_rcl_publish");

            PublisherDelegates.native_rcl_publish = (NativeRCLPublishType)Marshal.GetDelegateForFunctionPointer(
                native_rcl_publish_ptr, typeof(NativeRCLPublishType));
        }
Пример #4
0
        static RclCppDotnetDelegates()
        {
            dllLoadUtils = DllLoadUtilsFactory.GetDllLoadUtils();
            IntPtr pDll = dllLoadUtils.LoadLibrary("rclcppdotnet");

            IntPtr native_rclcpp_init_ptr = dllLoadUtils.GetProcAddress(pDll, "native_rclcpp_init");

            RclCppDotnetDelegates.native_rclcpp_init = (NativeRclcppInitType)Marshal.GetDelegateForFunctionPointer(
                native_rclcpp_init_ptr, typeof(NativeRclcppInitType));

            IntPtr native_rclcpp_shutdown_ptr = dllLoadUtils.GetProcAddress(pDll, "native_rclcpp_shutdown");

            RclCppDotnetDelegates.native_rclcpp_shutdown = (NativeRclcppShutdownType)Marshal.GetDelegateForFunctionPointer(
                native_rclcpp_shutdown_ptr, typeof(NativeRclcppShutdownType));
        }
Пример #5
0
        private void init(string libName)
        {
            string curPath = Environment.CurrentDirectory;

            Environment.CurrentDirectory = AppDomain.CurrentDomain.BaseDirectory;

            dllLoadUtils = IsLinux() ? (DllLoadUtils) new DllLoadUtilsLinux() : new DllLoadUtilsWindows();
            if (IsLinux())
            {
                libName = Environment.CurrentDirectory + "/" + (IntPtr.Size == 8 ? libName + "64.so" : libName + "32.so");
            }
            else
            {
                libName = Environment.CurrentDirectory + "\\" + (IntPtr.Size == 8 ? libName + "64.dll" : libName + "32.dll");
            }
            dllHandle = dllLoadUtils.LoadLibrary(libName);
        }
Пример #6
0
        static TransformListenerDelegates()
        {
            dllLoadUtils = DllLoadUtilsFactory.GetDllLoadUtils();
            IntPtr nativeLibTFL = dllLoadUtils.LoadLibrary("transform_listener");

            IntPtr native_construct_buffer_ptr = dllLoadUtils.GetProcAddress(nativeLibTFL, "native_construct_buffer");

            TransformListenerDelegates.native_construct_buffer = (NativeConstructBufferType)Marshal.GetDelegateForFunctionPointer(
                native_construct_buffer_ptr, typeof(NativeConstructBufferType));

            IntPtr native_delete_buffer_ptr = dllLoadUtils.GetProcAddress(nativeLibTFL, "native_delete_buffer");

            TransformListenerDelegates.native_delete_buffer = (NativeDeleteBufferType)Marshal.GetDelegateForFunctionPointer(
                native_delete_buffer_ptr, typeof(NativeDeleteBufferType));

            IntPtr native_construct_listener_ptr = dllLoadUtils.GetProcAddress(nativeLibTFL, "native_construct_listener");

            TransformListenerDelegates.native_construct_listener = (NativeConstructListenerType)Marshal.GetDelegateForFunctionPointer(
                native_construct_listener_ptr, typeof(NativeConstructListenerType));

            IntPtr native_delete_listener_ptr = dllLoadUtils.GetProcAddress(nativeLibTFL, "native_delete_listener");

            TransformListenerDelegates.native_delete_listener = (NativeDeleteListenerType)Marshal.GetDelegateForFunctionPointer(
                native_delete_listener_ptr, typeof(NativeDeleteListenerType));

            IntPtr native_construct_time_ptr = dllLoadUtils.GetProcAddress(nativeLibTFL, "native_construct_time");

            TransformListenerDelegates.native_construct_time = (NativeConstructTimeType)Marshal.GetDelegateForFunctionPointer(
                native_construct_time_ptr, typeof(NativeConstructTimeType));

            IntPtr native_lookup_transform_ptr = dllLoadUtils.GetProcAddress(nativeLibTFL, "native_lookup_transform");

            TransformListenerDelegates.native_lookup_transform = (NativeLookupTransformType)Marshal.GetDelegateForFunctionPointer(
                native_lookup_transform_ptr, typeof(NativeLookupTransformType));

            IntPtr native_retrieve_translation_ptr = dllLoadUtils.GetProcAddress(nativeLibTFL, "native_retrieve_translation");

            TransformListenerDelegates.native_retrieve_translation = (NativeRetrieveTranslationType)Marshal.GetDelegateForFunctionPointer(
                native_retrieve_translation_ptr, typeof(NativeRetrieveTranslationType));

            IntPtr native_retrieve_rotation_ptr = dllLoadUtils.GetProcAddress(nativeLibTFL, "native_retrieve_rotation");

            TransformListenerDelegates.native_retrieve_rotation = (NativeRetrieveRotationType)Marshal.GetDelegateForFunctionPointer(
                native_retrieve_rotation_ptr, typeof(NativeRetrieveRotationType));
        }
Пример #7
0
        static NodeDelegates()
        {
            dllLoadUtils = DllLoadUtilsFactory.GetDllLoadUtils();
            IntPtr nativelibrary = dllLoadUtils.LoadLibrary("rcldotnet_node");

            IntPtr native_rcl_create_publisher_handle_ptr = dllLoadUtils.GetProcAddress(
                nativelibrary, "native_rcl_create_publisher_handle");

            NodeDelegates.native_rcl_create_publisher_handle =
                (NativeRCLCreatePublisherHandleType)Marshal.GetDelegateForFunctionPointer(
                    native_rcl_create_publisher_handle_ptr, typeof(NativeRCLCreatePublisherHandleType));

            IntPtr native_rcl_create_subscription_handle_ptr = dllLoadUtils.GetProcAddress(
                nativelibrary, "native_rcl_create_subscription_handle");

            NodeDelegates.native_rcl_create_subscription_handle =
                (NativeRCLCreateSubscriptionHandleType)Marshal.GetDelegateForFunctionPointer(
                    native_rcl_create_subscription_handle_ptr, typeof(NativeRCLCreateSubscriptionHandleType));
        }
Пример #8
0
        private void init(string libName)
        {
            string curPath = Environment.CurrentDirectory;

            Environment.CurrentDirectory = AppDomain.CurrentDomain.BaseDirectory;

            dllLoadUtils = IsLinux() ? (DllLoadUtils) new DllLoadUtilsLinux() : new DllLoadUtilsWindows();
            if (IsLinux())
            {
                libName = Environment.CurrentDirectory + "/" + libName + ".so";
            }
            else
            {
                libName = Environment.CurrentDirectory + "\\" + libName + ".dll";
            }

            if (!File.Exists(libName))
            {
                throw new Exception("not find file:" + libName);
            }
            dllHandle = dllLoadUtils.LoadLibrary(libName);
        }
Пример #9
0
        static RCLdotnetDelegates()
        {
            dllLoadUtils = DllLoadUtilsFactory.GetDllLoadUtils();
            string library_name = "rcldotnet";
            IntPtr pDll         = dllLoadUtils.LoadLibrary(library_name);

            IntPtr native_rcl_init_ptr =
                dllLoadUtils.GetProcAddress(pDll, "native_rcl_init");

            RCLdotnetDelegates.native_rcl_init =
                (NativeRCLInitType)Marshal.GetDelegateForFunctionPointer(
                    native_rcl_init_ptr, typeof(NativeRCLInitType));

            IntPtr native_rcl_get_rmw_identifier_ptr =
                dllLoadUtils.GetProcAddress(pDll, "native_rcl_get_rmw_identifier");

            RCLdotnetDelegates.native_rcl_get_rmw_identifier =
                (NativeRCLGetRMWIdentifierType)Marshal.GetDelegateForFunctionPointer(
                    native_rcl_get_rmw_identifier_ptr, typeof(NativeRCLGetRMWIdentifierType));

            IntPtr native_rcl_ok_ptr =
                dllLoadUtils.GetProcAddress(pDll, "native_rcl_ok");

            RCLdotnetDelegates.native_rcl_ok =
                (NativeRCLOkType)Marshal.GetDelegateForFunctionPointer(
                    native_rcl_ok_ptr, typeof(NativeRCLOkType));

            IntPtr native_rcl_create_node_handle_ptr =
                dllLoadUtils.GetProcAddress(pDll, "native_rcl_create_node_handle");

            RCLdotnetDelegates.native_rcl_create_node_handle =
                (NativeRCLCreateNodeHandleType)Marshal.GetDelegateForFunctionPointer(
                    native_rcl_create_node_handle_ptr, typeof(NativeRCLCreateNodeHandleType));

            IntPtr native_rcl_get_zero_initialized_wait_set_ptr =
                dllLoadUtils.GetProcAddress(pDll, "native_rcl_get_zero_initialized_wait_set");

            RCLdotnetDelegates.native_rcl_get_zero_initialized_wait_set =
                (NativeRCLGetZeroInitializedWaitSetType)Marshal.GetDelegateForFunctionPointer(
                    native_rcl_get_zero_initialized_wait_set_ptr, typeof(NativeRCLGetZeroInitializedWaitSetType));

            IntPtr native_rcl_wait_set_init_ptr =
                dllLoadUtils.GetProcAddress(pDll, "native_rcl_wait_set_init");

            RCLdotnetDelegates.native_rcl_wait_set_init =
                (NativeRCLWaitSetInitType)Marshal.GetDelegateForFunctionPointer(
                    native_rcl_wait_set_init_ptr, typeof(NativeRCLWaitSetInitType));

            IntPtr native_rcl_wait_set_clear_ptr =
                dllLoadUtils.GetProcAddress(pDll, "native_rcl_wait_set_clear");

            RCLdotnetDelegates.native_rcl_wait_set_clear =
                (NativeRCLWaitSetClearType)Marshal.GetDelegateForFunctionPointer(
                    native_rcl_wait_set_clear_ptr, typeof(NativeRCLWaitSetClearType));

            IntPtr native_rcl_wait_set_add_subscription_ptr =
                dllLoadUtils.GetProcAddress(pDll, "native_rcl_wait_set_add_subscription");

            RCLdotnetDelegates.native_rcl_wait_set_add_subscription =
                (NativeRCLWaitSetAddSubscriptionType)Marshal.GetDelegateForFunctionPointer(
                    native_rcl_wait_set_add_subscription_ptr, typeof(NativeRCLWaitSetAddSubscriptionType));

            IntPtr native_rcl_destroy_wait_set_ptr =
                dllLoadUtils.GetProcAddress(pDll, "native_rcl_destroy_wait_set");

            RCLdotnetDelegates.native_rcl_destroy_wait_set =
                (NativeRCLDestroyWaitSetType)Marshal.GetDelegateForFunctionPointer(
                    native_rcl_destroy_wait_set_ptr, typeof(NativeRCLDestroyWaitSetType));

            IntPtr native_rcl_wait_ptr =
                dllLoadUtils.GetProcAddress(pDll, "native_rcl_wait");

            RCLdotnetDelegates.native_rcl_wait =
                (NativeRCLWaitType)Marshal.GetDelegateForFunctionPointer(
                    native_rcl_wait_ptr, typeof(NativeRCLWaitType)
                    );

            IntPtr native_rcl_take_ptr =
                dllLoadUtils.GetProcAddress(pDll, "native_rcl_take");

            RCLdotnetDelegates.native_rcl_take =
                (NativeRCLTakeType)Marshal.GetDelegateForFunctionPointer(
                    native_rcl_take_ptr, typeof(NativeRCLTakeType));
        }
Пример #10
0
        /// <summary>
        ///   References the native hunspell DLL.
        /// </summary>
        /// <exception cref="System.DllNotFoundException"></exception>
        /// <exception cref="System.NotSupportedException"></exception>
        internal static void ReferenceNativeHunspellDll()
        {
            lock (nativeDllReferenceCountLock)
            {
                if (nativeDllReferenceCount == 0)
                {
                    bool success = false;

                    if (dllHandle != IntPtr.Zero)
                    {
                        throw new InvalidOperationException("Native Dll handle is not Zero");
                    }

                    try
                    {
                        string dllName;
                        string dllPath;

                        if (Environment.OSVersion.Platform == PlatformID.Win32NT)
                        {
                            dllLoadUtils = new DllLoadUtilsWindows();
                            if (IntPtr.Size == 4)
                            {
                                dllName = Resources.HunspellX86DllName;
                            }
                            else
                            {
                                dllName = Resources.HunspellX64DllName;
                            }
                        }
                        else
                        {
                            dllLoadUtils = new DllLoadUtilsUnix();
                            dllName      = "libhunspell.dylib";
                        }

                        dllPath   = Path.Combine(NativeDLLPath, dllName);
                        dllHandle = dllLoadUtils.LoadLibrary(Path.Combine(NativeDLLPath, dllPath));
                        if (dllHandle == IntPtr.Zero)
                        {
                            if (IntPtr.Size == 4)
                            {
                                throw new DllNotFoundException(string.Format(Resources.HunspellX86DllNotFoundMessage, dllPath));
                            }
                            else
                            {
                                throw new DllNotFoundException(string.Format(Resources.HunspellX64DllNotFoundMessage, dllPath));
                            }
                        }

                        HunspellInit = (HunspellInitDelegate)GetDelegate("HunspellInit", typeof(HunspellInitDelegate));
                        HunspellFree = (HunspellFreeDelegate)GetDelegate("HunspellFree", typeof(HunspellFreeDelegate));

                        HunspellAdd          = (HunspellAddDelegate)GetDelegate("HunspellAdd", typeof(HunspellAddDelegate));
                        HunspellAddWithAffix = (HunspellAddWithAffixDelegate)GetDelegate("HunspellAddWithAffix", typeof(HunspellAddWithAffixDelegate));

                        HunspellRemove = (HunspellRemoveDelegate)GetDelegate("HunspellRemove", typeof(HunspellRemoveDelegate));

                        HunspellSpell   = (HunspellSpellDelegate)GetDelegate("HunspellSpell", typeof(HunspellSpellDelegate));
                        HunspellSuggest = (HunspellSuggestDelegate)GetDelegate("HunspellSuggest", typeof(HunspellSuggestDelegate));

                        HunspellAnalyze  = (HunspellAnalyzeDelegate)GetDelegate("HunspellAnalyze", typeof(HunspellAnalyzeDelegate));
                        HunspellStem     = (HunspellStemDelegate)GetDelegate("HunspellStem", typeof(HunspellStemDelegate));
                        HunspellGenerate = (HunspellGenerateDelegate)GetDelegate("HunspellGenerate", typeof(HunspellGenerateDelegate));

                        HyphenInit      = (HyphenInitDelegate)GetDelegate("HyphenInit", typeof(HyphenInitDelegate));
                        HyphenFree      = (HyphenFreeDelegate)GetDelegate("HyphenFree", typeof(HyphenFreeDelegate));
                        HyphenHyphenate = (HyphenHyphenateDelegate)GetDelegate("HyphenHyphenate", typeof(HyphenHyphenateDelegate));

                        success = true;
                    }
                    finally
                    {
                        if (dllHandle != IntPtr.Zero && !success)
                        {
                            dllLoadUtils.FreeLibrary(dllHandle);
                            dllHandle = IntPtr.Zero;
                        }
                    }
                }

                ++nativeDllReferenceCount;
            }
        }