通过 IHTMLDocument2 和 C++ 更改 div innerHTML

Change div innerHTML through IHTMLDocument2 and C++

我正在尝试以这种方式使用 IHTMLDocument2 界面更改 div 的内容:

    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();

所以我的div的内容只是被清除了,并没有被"hello"替换,我不明白为什么,会不会是安全问题?

谢谢

根据MSDN documentation,传递给put_innerText的字符串的类型是BSTR

所以,我建议尝试这样的代码:

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