如何从 SDK 插件数据文件夹加载 DLL

how to load dll from SDK addon data folder?

本文关键字:文件夹 加载 DLL 数据 插件 SDK      更新时间:2023-09-26

我们使用 web IDE 来创建插件。我的测试.dll位于数据文件夹中。如何通过 js-ctypes 加载它?

像"c:''test.dll"这样的绝对路径没有问题,但我不能使用此路径来分发它。

var lib = ctypes.open("c:''test.dll"); 
// works but how i get path to addon inner data directory?

我在这里给你阻力最小的方法......还有其他方法,例如从已安装的XPI中手动解压缩DLL,但这变得过于宽泛,容易出错且复杂。

  1. 您需要在package.json中定义"unpack": true,以便在安装时解压缩XPI。
  2. 您需要使用self.data.url()和各种其他工具来找出DLL文件的实际路径。在成为文件 URI 之前,URI 可能会在"资源:"和/或"chrome:" URI 中多次包装。所以这也需要解开。

    const {Cc, Cu, Ci} = require("chrome");
    Cu.import("resource://gre/modules/Services.jsm");
    const ResProtocolHandler = Services.io.getProtocolHandler("resource").
                               QueryInterface(Ci.nsIResProtocolHandler);
    const ChromeRegistry = Cc["@mozilla.org/chrome/chrome-registry;1"].
                           getService(Ci.nsIChromeRegistry);
    function resolveToFile(uri) {
      switch (uri.scheme) {
        case "chrome":
          return resolveToFile(ChromeRegistry.convertChromeURL(uri));
        case "resource":
          return resolveToFile(Services.io.newURI(ResProtocolHandler.resolveURI(uri), null, null));
        case "file":
          return uri.QueryInterface(Ci.nsIFileURL).file;
        default:
          throw new Error("Cannot resolve");
      }
    }
    const {data} = require("self");
    let dll = data.url("test.dll");
    dll = resolveToFile(Services.io.newURI(dll, null, null));
    console.log(dll.path); // dll.path is the full, platform-dependent path for the file.