How to show UTF-8 in IWebBrowser2

164 Views Asked by At

I have a IWebBrowser2 control implemented (with some chages) as in this thread and, I suppose, that is originated from a project in codeproject.com here. I'm trying to load the following embedded HTML code:

static const std::wstring html =
    LR"(
        <!DOCTYPE html>
        <head>
            <meta http-equiv="content-type" content="text/html; charset=UTF-8"; content="IE=7">
        </head>
        <body>
            <p>Hello, Привет</p>
        </body>
        </html>
)";

Here is the code for loading HTML:

// Invoking method
HRESULT STDMETHODCALLTYPE WebBrowser::Invoke(
    DISPID dispIdMember,
    REFIID riid,
    LCID lcid,
    WORD wFlags,
    DISPPARAMS* pDispParams,
    VARIANT* pVarResult,
    EXCEPINFO* pExcepInfo,
    UINT* puArgErr)
{
    if (dispIdMember == DISPID_DOCUMENTCOMPLETE) {
        return OnDocumentCompleted(pDispParams);
    }
    
    return S_OK;
}
// Loads HTML document
HRESULT WebBrowser::OnDocumentCompleted(DISPPARAMS* args)
{
    if (html_.empty()) {
        return S_OK;
    }

    IDispatch* iDispatch = 0;
    IHTMLDocument2* htmlDocument = 0;
    HRESULT result = webBrowser_->get_Document(&iDispatch);
    if (SUCCEEDED(result) && iDispatch) {
        result = iDispatch->QueryInterface(IID_IHTMLDocument2, (void**)&htmlDocument);
    }
    else {
        LogRD("WebBrowser error in OnDocumentCompleted", "Cann't get HTML document");
    }

    IStream* iStream = 0;
    IPersistStreamInit* htmlDocumentPersistStream = 0;
    if (SUCCEEDED(result) && htmlDocument) {
        result = htmlDocument->QueryInterface(IID_IPersistStreamInit, (void**)&htmlDocumentPersistStream);
    }
    else {
        LogRD("WebBrowser error in OnDocumentCompleted", "Cann't query IID_IPersistStreamInit interface");
    }

    if (SUCCEEDED(result) && htmlDocumentPersistStream) {
        iStream = SHCreateMemStream((BYTE*)html_.c_str(), html_.size() * sizeof(wchar_t));
        result = htmlDocumentPersistStream->Load(iStream);
        if (FAILED(result)) {
            LogRD("WebBrowser error in OnDocumentCompleted", "Failed to load iStream");
        }
    }

    if (iStream) {
        iStream->Release();
    }
    if (htmlDocumentPersistStream) {
        htmlDocumentPersistStream->Release();
    }
    if (htmlDocument) {
        htmlDocument->Release();
    }
    if (iDispatch) {
        iDispatch->Release();
    }

    html_.clear();

    return S_OK;
}

The result is: screenshot

I've found that Internet Explorer has some requirements for displaying UTF-8 in MSDN. I believe some requirements are satisfied, for example my HTML contains the charset=UTF-8 meta-tag, and my HTML is less than 4Kb in size. However, I don't know how to complete remaining requirements. What else do I need to show UTF-8 symbols properly?

0

There are 0 best solutions below