1

I'm trying to build an extension for Firefox. This extension uses an XPCOM component (a C++ dll). I'm compiling the DLL, compilation is OK.

I also succeeded in building a JS code which instanciates the object from XPCOM:

try {
   greenfox;
   return true;
} catch( e ) {
   alert( e );
   return false;
}

The object returned is this one:

QueryInterface
    QueryInterface()

__proto__
    [xpconnect wrapped native prototype] { QueryInterface=QueryInterface()}

QueryInterface
    QueryInterface()

Everything is fine, except I can't call the function which are supposed to be in my XPCOM component.

Here is my IDL file:

[scriptable, uuid(ec8030f7-c20a-464f-9b0e-13a3a9e97384)]
interface nsISample : nsISupports
{
    attribute string value;
    void writeValue(in string aPrefix);
    void poke(in string aValue);

    void start();
    double stop();
};

When callingstart() function, I get the Javascript error: "is not a function"

greenfox.start();

Do you have any idea? It seems like no function is exposed in my XPCOM.

Wladimir Palant
  • 56,865
  • 12
  • 98
  • 126
frinux
  • 2,052
  • 6
  • 26
  • 47

2 Answers2

1

You seem to be looking at an object exposing only the nsISupports interface. Your interface (nsISample) won't get exposed by default, you have to explicitly request it. You can do it for example by instantiating your component like this:

var greenfox = Components.classes["..."].getService(Components.interfaces.nsISample);
greenfox.start();

Alternatively, you can also call QueryInterface on an object you already have:

greenfox.QueryInterface(Components.interfaces.nsISample);
greenfox.start();

Generally, I wouldn't recommend using a binary XPCOM component for reasons outlined here, maintaining them requires way too much effort. I would rather suggest compiling a regular DLL and using it via js-ctypes. Reference a binary-component to js-ctypes mentions how you would locate a DLL inside your add-on to use it via js-ctypes.

Community
  • 1
  • 1
Wladimir Palant
  • 56,865
  • 12
  • 98
  • 126
  • That's a lead, and I also found this code in the sample given by Mozilla. I already tried it before, but when trying to get instance, I get this message: [Exception... "Could not convert JavaScript argument arg 0 [nsISupports.QueryInterface]" nsresult: "0x80570009 (NS_ERROR_XPC_BAD_CONVERT_JS)" location: "JS frame :: chrome://greenfox/content/greenfox_sampler.js :: :: line 11" data: no] – frinux Feb 05 '12 at 17:55
  • I'm going further: I examinated Components.interfaces and I found no nsISample in it. – frinux Feb 05 '12 at 18:08
  • @frinux: That means that the XPT file with your interface wasn't registered. Either you didn't include it in your extension or the [`interfaces` entry in `chrome.manifest`](https://developer.mozilla.org/en/Chrome_Registration#interfaces) is incorrect. – Wladimir Palant Feb 05 '12 at 18:39
  • I'm following your advice Wladimir, I also thinks XPCom are too complicated and I'm trying to do it through js-ctypes. J'm still stuck in the library definition, you can see my question here : http://stackoverflow.com/questions/9152142/mozilla-use-a-c-dll-with-js-ctypes – frinux Feb 05 '12 at 19:14
0

Do you call QueryInterface with your uuid? It is necessary to call it before using the component instance created. Does your usage match what's in here?

If you don't want to deal with XPCOM you can use js-ctypes.

Wladimir Palant
  • 56,865
  • 12
  • 98
  • 126
artificialidiot
  • 5,309
  • 29
  • 27