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