Ive overridden the context menu and want to gain the URL, when a user
right clicks on a link (anchor).
However, although my code compiles fine, it crashes when
right-clicking on a link. I have narrowed the problem to my
queryinterface line. Has anyone got an idea of where i went wrong?
Thanks.
HRESULT FAR EXPORT CCustomControlSite::XDocHostUIHandler::ShowContextMenu(
DWORD dwID,
POINT* /*pptPosition*/,
IUnknown* /*pCommandTarget*/,
IDispatch *pdispReserved)
{
METHOD_PROLOGUE(CCustomControlSite, DocHostUIHandler)
if (dwID == 6) //an anchor (works fine)
{
IHTMLAnchorElement *pElem = NULL;
BSTR bstrURL;
pdispReserved->QueryInterface(IID_IHTMLAnchorElement,(void**)&pElem);
pElem->get_href(&bstrURL);
}
else
AfxMessageBox("Default Menu");
return S_OK;
}
It is strange that the function is not marked with __stdcall calling
convention. Usually, an interface method is declared with STDMETHODIMP
(which expands to __stdcall). I'm not familiar with MFC - does it
achieve the same effect in some other way?
> POINT* /*pptPosition*/,
> IUnknown* /*pCommandTarget*/,
> IDispatch *pdispReserved)
> {
> METHOD_PROLOGUE(CCustomControlSite, DocHostUIHandler)
>
> if (dwID == 6) //an anchor (works fine)
6 is CONTEXT_MENU_UNKNOWN. An anchor would be CONTEXT_MENU_ANCHOR, which
is 5.
> {
> IHTMLAnchorElement *pElem = NULL;
> BSTR bstrURL;
>
> pdispReserved->QueryInterface(IID_IHTMLAnchorElement,(void**)&pElem);
Is pdispReserved NULL, by any chance?
--
With best wishes,
Igor Tandetnik
"For every complex problem, there is a solution that is simple, neat,
and wrong." H.L. Mencken