我们使用 Web IDE 来创建插件。我的 test.dll 位于数据文件夹中。如何通过 js-ctypes 加载它?
使用像“c:\test.dll”这样的绝对路径没有问题,但我不能使用这个路径来分发它。
var lib = ctypes.open("c:\\test.dll");
// works but how i get path to addon inner data directory?
我们使用 Web IDE 来创建插件。我的 test.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,但这太宽泛、容易出错且复杂。
"unpack": true
在 package.json 中定义,以便在安装时解压 XPI。您需要使用self.data.url()
和其他各种工具来找出 DLL 文件的实际路径。在成为文件 URI 之前,该 URI 可能会在“resource:”和/或“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.