The problem of "MFC" Chtmlview::getsource Chinese garbled characters

Source: Internet
Author: User

In the MFC SDI, the use of Chtmlview::getsource to obtain the Web page source code, saved to the local, found in the Chinese part garbled, some Chinese normal. Try to transcode yourself first, and find nothing. There is no right solution on the web.

Self-tracking Chtmlview::getsource function, in the ViewHtml.cpp file 1083 lines, there is the following code:

Bretval = TRUE; try{    = CString (pstr, StatStg.cbSize.LowPart);} Catch_all (e) {    = FALSE;    Delete_exception (e);}

Key in: refstring = CString (pstr, StatStg.cbSize.LowPart);

In fact, in the case of Unicode, CString is constructed using LPCSTR or char* to construct the CString constructor. But inside this CString, the following function is called:

Static void __cdecl Converttobasetype (        _out_writes_ (ndestlength) lpwstr pszdest,        int  ndestlength ,        _in_ LPCSTR pszsrc,        int nsrclength =-1throw() {       Nlen is in wchar_ts    0, pszsrc, Nsrclength, Pszdest, ndestlength);}

There is an internal character conversion for LPCSTR or char*.

Part of the Chinese language is garbled, it is this place produced.

Do not expect to use CStringA Str (strhtml) to convert back, after the content, there is still a part of the Chinese garbled.

My solution is to copy the Chtmlview::getsource code, return the contents of the CStringA directly, and use the CStringA content to save the file. The result is normal.

My code is as follows:

BOOL Cmfcbrowserview::getmysource (cstringa&strref) {BOOL Bretval=FALSE; CComPtr<IDispatch>Spdisp; m_pBrowserApp->get_document (&spdisp); if(Spdisp = =NULL) {        returnBretval;    } hglobal hmemory; Hmemory= GlobalAlloc (gmem_moveable,0); if(Hmemory = =NULL) {        returnBretval; } ccomqiptr<IPersistStreamInit> Sppersiststream =Spdisp; if(Sppersiststream = =NULL)        {GlobalFree (hmemory); returnBretval; } CComPtr<IStream>Spstream; HRESULT hres= CreateStreamOnHGlobal (Hmemory, TRUE, &Spstream); if(FAILED (hres)) {GlobalFree (hmemory); returnBretval; } Sppersiststream-Save (Spstream, FALSE);    STATSTG STATSTG; Spstream->stat (&STATSTG, Statflag_noname); LPCSTR pstr= static_cast<lpcstr>(GlobalLock (hmemory)); if(Pstr = =NULL)        {GlobalFree (hmemory); returnBretval; }    //Stream is expected to being ANSI (CP-ACP). CString Constructor//Would convert implicitly, and truncate to correct length.Bretval=TRUE;    CStringA STRV (PSTR, StatStg.cbSize.LowPart); Strref=STRV;    GlobalUnlock (hmemory);    GlobalFree (hmemory); returnBretval;}

Main problem: The conversion of a string when CString is constructed internally. If you need a CString string, you might want to convert it to a wide character, and then assign it to CString. This is just speculation, not tested.

The problem of "MFC" Chtmlview::getsource Chinese garbled characters

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.