Skip to content

API Loader zh

3cxc edited this page Sep 13, 2026 · 1 revision

NativePluginKit.Loader

插件加载与生命周期管理。


PluginLoader

public unsafe class PluginLoader
{
    public PluginLoader(Action<string> logAction,
                        HostApiTable apiTable,
                        Version apiVersion);

    public HostApiTable Api { get; }
    public Action<string> LogWriter { get; }
    public Version CurrentApiVersion { get; }

    public bool LoadPlugin(string dllPath);
    public void LoadPluginsFromDirectory(string directoryPath);
    public void UnloadAll();
}

LoadPlugin(string dllPath)

加载单个插件 DLL / SO / DYLIB。

执行步骤:

  1. LoadLibrary(Windows)或 dlopen(类 Unix)
  2. 读取 GetPluginInfo → PluginInfo
  3. 若插件要求不兼容的 API 版本则拒绝加载
  4. 读取 OnInit 并传入 HostApiTable 指针调用
  5. 记录句柄,供 UnloadAll 使用

模块句柄会保留至宿主退出。

LoadPluginsFromDirectory(string directoryPath)

按编译期平台枚举 *.dll / *.so / *.dylib, 逐个调用 LoadPlugin, 目录不存在时静默返回。

UnloadAll()

遍历所有已加载插件:

  1. 若存在 OnStop 导出则调用。
  2. FreeLibrary / dlclose。

调用后 PluginLoader 已清空,可重复使用。

Clone this wiki locally