none
C# 如何实现像C++一样的安全数组,求解 RRS feed

  • 问题

  • 我不懂C++,我在用C#开发的时候用了mshtml.IHTMLDocument2.write 这个方法来修改原始数据。

    现在的问题是我C#里面的write这个方法不会用,输出的数据总是不对,我试过用string\byte[]\Stream,出来的结果都是不对。

    var doc = explorer.Document as IHTMLDocument2;

    doc.write(System.Text.Encoding.Default.GetBytes("网页的统计结果..."));

    这样输出的结果始终是乱码,

    MSDN 原文 http://msdn.microsoft.com/zh-cn/library/ms536782

        IHTMLDocument2 *document; // Declared earlier in the code
        BSTR bstr = SysAllocString(OLESTR("Written by IHTMLDocument2::write()."));
        // Creates a new one-dimensional array
        SAFEARRAY *psaStrings = SafeArrayCreateVector(VT_VARIANT, 0, 1);
        if (psaStrings == NULL) {
            goto cleanup;
        }
        VARIANT *param;
        HRESULT hr = SafeArrayAccessData(psaStrings, (LPVOID*)&param);
        param->vt = VT_BSTR;
        param->bstrVal = bstr;
        hr = SafeArrayUnaccessData(psaStrings);
        hr = document->write(psaStrings);
    cleanup:
        // SafeArrayDestroy calls SysFreeString for each BSTR
        if (psaStrings != NULL) {
            SafeArrayDestroy(psaStrings);
        }
    


    2015年1月5日 7:41

答案

  • 你好:

    请参考以下代码,直接将html字符串封装为object[]数组写入就可以了:

    string link = "http://...";
                WebRequest request = HttpWebRequest.Create(link);
                WebResponse response = request.GetResponse();
                StreamReader sd = new StreamReader(response.GetResponseStream());
                HTMLDocumentClass ms = new HTMLDocumentClass();
                object[] obj = { sd.ReadToEnd() };
                mshtml.IHTMLDocument2 objMyDoc = (mshtml.IHTMLDocument2)ms;
                objMyDoc.write(obj);
    


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    2015年1月6日 7:20
    版主