I'd like to ask a simple question about interfaces in XPCOM. I'm
writing C++ XPCOM component for Firefox (linking against Gecko SDK
1.9.2 in MS Visual Studio).
I am trying to cast from one interface to another one. Let's say from
nsIDOMNode to nsIDOMHTMLElement.
I retrieve initial nsIDOMNode from nsIDOMHTMLDocument via GetFirstChild
(). Then I'd like to traverse DOM tree and cast each HTML element from
nsIDOMNode to appropriate interface (nsIDOMHTMLElement,
nsIDOMHTMLTableElement, ...).
I check node name via GetNodeName() before calling QueryInterface().
Even if the nsIDOMNode is <HTML> node, the QI fails (NS_NOINTERFACE)
when trying to *cast* it to nsIDOMHTMLElement.
You can see my code snippet here: http://pastebin.mozilla.org/691367
(QI fails on marked line).
There is no problem when I call QI to cast interface to one of its
parent interfaces. Problem is casting to child interfaces. However,
documentation says that QI is symmetric operation, QI(A) = B <=> QI(B)
= A.
I'm quite confused of such a behaviour - could anyone suggest what am
I doing wrong (or what I misunerstood), please?
Thank you. Libor
> I check node name via GetNodeName() before calling QueryInterface().
> Even if the nsIDOMNode is<HTML> node, the QI fails (NS_NOINTERFACE)
> when trying to *cast* it to nsIDOMHTMLElement.
>
> You can see my code snippet here: http://pastebin.mozilla.org/691367
> (QI fails on marked line).
If you can run in a debugger, check the actual type of the C++ object. You
can do this on release builds in Windows using the Mozilla symbol server.
Then look and see whether that object actually supports nsIDOMHTMLElement or
not. It's quite possible that the first node in the document is a text node
or a doctype or some other node type, which isn't an element and therefore
won't implement nsIDOMHTMLElement.
--BDS
This should be working fine. And you're using interfaces that are
frozen, so even compiling against one Gecko version and linking against
another shouldn't be a problem.
So something really weird is going on; you'll need to pull out a
debugger and debug....
-Boris
I've been debugging before posting this issue. Also I remember that
these interfaces are frozen -- that's good info.
Now I tried to debug using Mozilla symbol server and I this is what I
see in Visual Studio Debugger: http://i.imgur.com/ntXNH.png I think
that it isn't the benefit of the symbol server, is it?
Another weirdness is that my code runs fine on my home desktop (Gecko
1.8, FF 3.5.6). I'll try again tomorrow at office (1.9/3.5.6), but I
think that the problem is somewhere in my PC. What could it be then?
I'll post some news if I will have any.
I appreciate your help.