组件返回失败代码:0x80004002(NS_NOINTERFACE)[nsIFileURL.file]

时间:2016-07-26 04:54:38

标签: firefox-addon jsctypes

我想使用js-ctype制作基本示例Firefox插件。首先,我用一个简单的C代码创建了一个 .dll 文件:

#include "stdio.h"
extern "C"
{
    __declspec(dllexport) int add(int a, int b) { return a + b; }
}

库文件很好。我在另一个项目中测试过它。 我用js-ctypes代码加载它:

var {Cu , Cc, Ci} = require("chrome");
Cu.import("resource://gre/modules/ctypes.jsm", null);
Cu.import("resource://gre/modules/Services.jsm");
var self = require("sdk/self");
var prompts = Cc["@mozilla.org/embedcomp/prompt-service;1"].getService(Ci.nsIPromptService);
var dataUrl = self.data.url("Js-CtypeLib.dll");
dataUrl = Services.io.newURI(dataUrl,null,null).QueryInterface(Ci.nsIFileURL).file.path;
var lib, add;

try{
    console.log("Load library");
    lib = ctypes.open("Js-CtypeLib.dll");
    try{
        declareFunc();
    }
    catch(e){
        console.log("Error declare function");
    }
}
catch(e){
    console.log("Error load Library!");
}

function declareFunc(){
    add = lib.declare("add", ctypes.default_abi, ctypes.int, ctypes.int, ctypes.int);
}

function test(){
    var rs = add(4,2);
    prompts.alert(null, "Result: ", rs);
    lib.close();
}

exports.test = test;

然后,我通过 index.js

调用 support.js 文件
var buttons = require('sdk/ui/button/action');
var tabs = require("sdk/tabs");
var support = require("./support.js");
var button = buttons.ActionButton({
    id: "mozilla-link",
    label: "Visit Mozilla",
    icon: {
        "16": "./images/icon-16.png",
        "32": "./images/icon-32.png",
        "64": "./images/icon-64.png"
    },
    onClick: handleClick}
);

function handleClick(state) {
    support.test();
}

最后,在cmd中,我运行它并得到:

Component returned failure code: 0x80004002 (NS_NOINTERFACE) [nsIFileURL.file]

完整错误文字:
this error

1 个答案:

答案 0 :(得分:0)

您需要将您的插件标记为"已解压缩"在package.json中。然后,您必须在file://的调用中使用ctypes.open(file_uri_here) uri。

要在插件中获取文件的文件,您可以这样做:

Components.utils.import("resource://gre/modules/Services.jsm");
var cr = Components.classes['@mozilla.org/chrome/chrome-registry;1'].getService(Components.interfaces.nsIChromeRegistry);

var chromeURI_myLib = Services.io.newURI('chrome://youraddon/content/mySubFolder/myCFunctionsForUnix.so', 'UTF-8', null);
var localFile_myLib = cr.convertChromeURL(chromeURI_myLib);
var jarPath_myLib = localFile_myLib.spec; // "jar:file:///C:/Users/Vayeate/AppData/Roaming/Mozilla/Firefox/Profiles/aecgxse.Unnamed%20Profile%201/extensions/youraddon@jetpack.xpi!/mySubFolder/myCFunctionsForUnix.so"
var filePath_myLib = localFilemyLib.path; 

ctypes.open(filePath_myLib);