Răspuns C++ BHO added link not working

  • 18 апреля 2012 г. 9:15
     
     

    Hi,

    I wrote a BHO (C++/ATL following the helloworld example at http://msdn.microsoft.com/en-us/library/bb250489%28VS.85%29.aspx), but am having problems when I add to the document (IE9).

    Basically I search through the text then 'linkify' certain parts.

    This all works fine - I can find the node I am interested in and so I then create a new IHTMLElement like:

        pDocument->createElement(L"A", &a); //returns OK

    then I set some parameters, such as

        a->setAttribute(L"href",hrefTarget,0); //also returns OK

    and finally insert it at the position I want it, etc...

        CComQIPtr<IHTMLDOMNode> adom = a;

        if(adom){parentNode->insertBefore(adom,nodeBefore,NULL); }//returns OK

    The resulting html is well formed, including the link I added (so, ends up in doc as <a href="good ref" ...>expected text</a> )

    The problem is that it doesn't act as a link - I just see "expected text" with no functionality.....

    Any ideas? Am I doing this correctly?

    (I did it already in C# using spicIE, and that worked - but I'd rather have it in C++ for speed)

    Thanks

Все ответы

  • 19 апреля 2012 г. 14:35
     
     

    also, after the call to createElement(L"a", &a), I tried:

        CComQIPtr<IHTMLAnchorElement> anchor = a; //but this remains null

    But I would have expected this is be good if createElement worked?

    So I am guessing whatever is created, it isn't recognised as a valid anchor....

    (I also tried other types just in case... img, script...... none worked in returning anything other than IHTMLElement)

    Anyone done this?

  • 20 апреля 2012 г. 7:35
     
     
    What the hrefTarget value?

    NEU_ShieldEdge

  • 20 апреля 2012 г. 7:39
     
     

    I have it set as:

        VARIANT hrefTarget;
        hrefTarget.vt = VT_BSTR;
        hrefTarget.bstrVal = SysAllocString(L"javascript:void(0);");

  • 25 апреля 2012 г. 14:23
     
     Отвечено

    Well, if anyone is interested I found what the problem was....

    Where I put

        pDocument->createElement(L"A", &a);

    it should have been:

        CComBSTR aTag = "A";

        pDocument->createElement(aTag,&a);

    • Помечено в качестве ответа JasonTomlinson 25 апреля 2012 г. 14:23
    •  
  • 3 июля 2012 г. 7:48
     
     
    This is well known problem with COM marshaling. C++ compiler can't distinguish between BSTR strings and WCHAR* strings. But WCHAR* string can't be marshaled between COM calls. Because COM expects true BSTR string allocated with the SysAllocString() API and located in the system heap. Meanwhile WCHAR* is allocated statically in a stack or dynamically in a process heap. As the result, WCHAR* strings will NOT work for any COM call that makes marshaling. However, there is a chance WCHAR* strings will work for STA in-proc COM objects because marshaling is switched off by default for this scenario until you manually marshal objects (through GIT or streams).

    Best regards, Sergey