/// <summary> /// /// </summary> /// <param name="module">Optional reference to native module</param> /// <param name="thisPtr">Pointer to ther unmanaged object</param> /// <param name="delete">Delegate to the destructor/delete method</param> /// <param name="initialize"></param> /// <param name="update">Delegate to the update method</param> public UnmanagedPlugin( IntPtr module, IntPtr thisPtr, UnmanagedActionMethod delete, UnmanagedActionMethod initialize, UnmanagedActionMethod update) { m_module = module; m_thisPtr = thisPtr; m_handleDelete = delete; m_handleInitialize = initialize; m_handleUpdate = update; }
/// <summary> /// Creates a plugin loaded from a platform dependant unmanaged /// shared library binary file. /// </summary> /// <param name="name"></param> /// <returns></returns> public static UnmanagedPlugin CreateNativePlugin(string name) { #if WIN32 string filepath = Path.Combine( Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), name + ".dll" ); #else // UNIX string filepath = Path.Combine( Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "lib" + name + ".so" ); #endif IntPtr module = dlopen(filepath, DlFlag.RTLD_LAZY); if (module == IntPtr.Zero) { throw new Exception("module"); } IntPtr createPluginPtr = dlsym(module, "CreatePlugin"); if (createPluginPtr == IntPtr.Zero) { throw new Exception("create"); } IntPtr deletePtr = dlsym(module, "DestroyPlugin"); if (deletePtr == IntPtr.Zero) { throw new Exception("destroy"); } IntPtr initializePtr = dlsym(module, "InitializePlugin"); if (initializePtr == IntPtr.Zero) { throw new Exception("initialize"); } IntPtr updatePtr = dlsym(module, "UpdatePlugin"); if (updatePtr == IntPtr.Zero) { throw new Exception("update"); } UnmanagedFuncFunctionIntPtr createPlugin = (UnmanagedFuncFunctionIntPtr)Marshal.GetDelegateForFunctionPointer( createPluginPtr, typeof(UnmanagedFuncFunctionIntPtr) ); UnmanagedActionMethod delete = (UnmanagedActionMethod)Marshal.GetDelegateForFunctionPointer( deletePtr, typeof(UnmanagedActionMethod) ); UnmanagedActionMethod initialize = (UnmanagedActionMethod)Marshal.GetDelegateForFunctionPointer( initializePtr, typeof(UnmanagedActionMethod) ); UnmanagedActionMethod update = (UnmanagedActionMethod)Marshal.GetDelegateForFunctionPointer( updatePtr, typeof(UnmanagedActionMethod) ); return(new UnmanagedPlugin(module, createPlugin(), delete, initialize, update)); }