Search code examples
c++ihtmldocument2ihtmldocument

Change div innerHTML through IHTMLDocument2 and C++


I'm trying to change the content of a div using IHTMLDocument2 interface this way:

    IHTMLElementCollection* collection = NULL;
    IDispatch* mydiv;

    doc2->get_all(&collection);
    long count;
    collection->get_length(&count);     //just to check I get something

    CComVariant varstr = L"mydivname";
    CComVariant varint = 0;
    collection->item(varstr, varint, &mydiv);    //this works I get the div
    IHTMLElement* htmldiv;
    mydiv->QueryInterface(IID_IHTMLElement, (void**)&htmldiv);

    CComBSTR html;
    htmldiv->get_innerHTML(&html);      //works too, I get the current content

    HRESULT hr=htmldiv->put_innerText(L"hello");      //this does not work but returns S_OK

    collection->Release();

So the content of my div is just cleared and not replaced with "hello", I don't understand why, can it be a security issue ?

Thanks


Solution

  • According to the MSDN documentation, the string passed to put_innerText is of type BSTR.

    So, I would suggest trying some code like this:

    CComBSTR text(OLESTR("hello"));
    hr = htmldiv->put_innerText(text);