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.
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.