Azure/C++/C#/Java/Asp.net/JavaScript/Html - MFC/COM/ActiveX/eclipse/Web

dlbrant

working soft

博客园 首页 新随笔 管理

往一个空的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;
}

 

posted on 2013-06-18 10:05  dlbrant  阅读(1437)  评论(0编辑  收藏  举报

Software