iframe空文档中写入内容

往一个空的iframe中写入内容,再其document ready之前有可能遇到拿回 的body指针为空,因此以下面的函数往其document中写入html

HRESULT WriteToHtmlDocument(CComPtr<IHTMLDocument2> spDoc2, CComBSTR &bstrHtml)
{
    HRESULT hr = S_OK;

    //BSTR bstr = SysAllocString(OLESTR("Written by IHTMLDocument2::write()."));
    BSTR bstr = bstrHtml.Copy();
    // Creates a new one-dimensional array
    SAFEARRAY *psaStrings = SafeArrayCreateVector(VT_VARIANT, 0, 1);
    if (psaStrings == NULL) {
        goto cleanup;
    }
    VARIANT *param;
    hr = SafeArrayAccessData(psaStrings, (LPVOID*)&param);
    param->vt = VT_BSTR;
    param->bstrVal = bstr;
    hr = SafeArrayUnaccessData(psaStrings);
    hr = spDoc2->write(psaStrings);

cleanup:
    // SafeArrayDestroy calls SysFreeString for each BSTR
    if (psaStrings != NULL) 
        SafeArrayDestroy(psaStrings);

    return hr;
}
原文地址:https://www.cnblogs.com/dlbrant/p/3141740.html