【MFC】CHtmlView::GetSource中文亂碼的問題

來源:互聯網
上載者:User

標籤:cto   結果   tle   unlock   擷取   網頁   測試   length   set   

在MFC的SDI中,使用CHtmlView::GetSource來擷取網頁源碼,儲存到本地,發現中文中的一部分亂碼,有些中文正常。自己先試著轉碼等各種嘗試,發現一無所獲。網上也沒有正確的解決方案。

自己跟蹤CHtmlView::GetSource函數內,在ViewHtml.cpp檔案的1083行處,有如下代碼:

bRetVal = TRUE;TRY{    refString = CString(pstr, statStg.cbSize.LowPart);}CATCH_ALL(e){    bRetVal = FALSE;    DELETE_EXCEPTION(e);}

關鍵處在:refString = CString(pstr, statStg.cbSize.LowPart);

其實就是在UNICODE情況下,CString在構造時,使用了LPCSTR或者char*來構造CString時的建構函式。但是在構造這個CString內部,會調用到如下函數:

static void __cdecl ConvertToBaseType(        _Out_writes_(nDestLength) LPWSTR pszDest,        _In_ int nDestLength,        _In_ LPCSTR pszSrc,        _In_ int nSrcLength = -1) throw(){    // nLen is in wchar_ts    ::MultiByteToWideChar( _AtlGetConversionACP(), 0, pszSrc, nSrcLength, pszDest, nDestLength );}

內部有對LPCSTR或char*進行字元轉換。

中文的一部分亂碼,正是由這個地方產生的。

別奢望用CStringA str(strHtml)來轉換回來,轉了之後的內容,依然有一部分的中文亂碼。

 

我的解決辦法是,將CHtmlView::GetSource的代碼Copy出來,直接返回CStringA的內容,並用CStringA的內容,來儲存檔案。結果就正常了。

My Code如下:

BOOL CMFCBrowserView::GetMySource(CStringA& strRef){    BOOL bRetVal = FALSE;    CComPtr<IDispatch> spDisp;     m_pBrowserApp->get_Document(&spDisp);    if ( spDisp == NULL )    {        return bRetVal;    }    HGLOBAL hMemory;    hMemory = GlobalAlloc(GMEM_MOVEABLE, 0);    if ( hMemory == NULL )    {        return bRetVal;    }    CComQIPtr<IPersistStreamInit> spPersistStream = spDisp;    if ( spPersistStream == NULL )    {        GlobalFree(hMemory);        return bRetVal;    }    CComPtr<IStream> spStream;    HRESULT hRes = CreateStreamOnHGlobal(hMemory, TRUE, &spStream);    if ( FAILED(hRes) )    {        GlobalFree(hMemory);        return bRetVal;           }    spPersistStream->Save(spStream, FALSE);    STATSTG statStg;    spStream->Stat(&statStg, STATFLAG_NONAME);    LPCSTR pstr = static_cast<LPCSTR>(GlobalLock(hMemory));    if ( pstr == NULL )    {        GlobalFree(hMemory);        return bRetVal;    }    // Stream is expected to be ANSI (CP-ACP). CString constructor    // will convert implicitly, and truncate to correct length.    bRetVal = TRUE;    CStringA strV(pstr, statStg.cbSize.LowPart);    strRef = strV;    GlobalUnlock(hMemory);    GlobalFree(hMemory);    return bRetVal;}

主要問題:就出在CString內部構造時,對字串的轉換。如果需要CString的字串,可能需要自己來轉換成寬字元,然後賦值給CString。這個只是猜測,沒有進行測試。

【MFC】CHtmlView::GetSource中文亂碼的問題

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.