2

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

Entretoize
  • 2,124
  • 3
  • 23
  • 44

1 Answers1

0

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);
Mr.C64
  • 41,637
  • 14
  • 86
  • 162
  • No problem. Glad to be of help. The "tricky" part here is that we don't get compile-time errors when we pass an ordinary wchar_t string instead of a BSTR string (which makes sense from the compiler's perspective, considering the typedef definition of a BSTR :) – Mr.C64 Sep 19 '17 at 18:03