I've tried writing the method in two slightly different ways. All my code is based on code I found at http://www.mozilla.org/scriptable/faq.html#i9. In fact, I pretty much copied and pasted the code without changing it.
Here are my two method declarations in my .idl file.
IMyComponent.idl
Code: Select all
string GetStringA();
void GetStringB(out string s);
The xpidl utility produces the following method declarations in the resulting header file.
IMyComponent.h
Code: Select all
/* string GetStringA (); */
NS_IMETHOD GetStringA(char **_retval) = 0;
/* void GetStringB (out string s); */
NS_IMETHOD GetStringB(char **s) = 0;
string s);
Note that the underlying C++ methods are exactly the same except for the name of the parameter.
And here's the implementation of the two methods.
MyComponent.cpp
Code: Select all
/* string GetStringA (); */
NS_IMETHODIMP MyComponent::GetStringA(char **_retval)
{
const char* message = "Hello from GetStringA!";
if(!_retval)
return NS_ERROR_NULL_POINTER;
*_retval = (char*) nsMemory::Clone(message, sizeof(char)*(strlen(message)+1));
return *_retval ? NS_OK : NS_ERROR_OUT_OF_MEMORY;
}
/* void GetStringB (out string s); */
NS_IMETHODIMP MyComponent::GetStringB(char **s)
{
const char* message = "Hello from GetStringB!";
if(!s)
return NS_ERROR_NULL_POINTER;
*s = (char*) nsMemory::Clone(message, sizeof(char)*(strlen(message)+1));
return *s ? NS_OK : NS_ERROR_OUT_OF_MEMORY;
}
This code builds without error. Since I found it on a Mozilla website, I assume it's correct and does what I intend it to do.
The problem is actually loading and using the component in JavaScript. I've tried all sorts of variations, but I haven't been able to get anything to work. To the best of my obviously faulty understanding, this code should work -- although it actually doesn't.
MyComponentTest.js
Code: Select all
var sA;
sA = obj.GetStringA();
alert(sA);
var sB = {};
obj.GetStringB(sB);
alert(sB.value);
I don't see anything when the code runs -- neither alert box comes up.
I've been reading some documentation on the nsEmbedString class. Should I be using nsEmbedString objects here? If so, how?
Can anybody see what I'm doing wrong and let me know how to fix it? I'd be very grateful for any help.
Thanks,
Jay