用CHtmlView接口获取HTML源文件

用CHtmlView接口获取HTML源文件:

支持在UNICODE工程下获取HTML代码,无乱码。。。。发了两年多了才知道,害得我找了一晚上,没想到自己的博客里就有。。。

BOOL CWeb::GetSource(CString& strString) 
{ 
    IHTMLDocument2* pHtmlDoc2 = (IHTMLDocument2*)GetHtmlDocument(); 
    // check if HtmlDocument initialized 
    if( pHtmlDoc2 != NULL) 
    { 
        IHTMLDocument3* pHTMLDoc3 = NULL; 
        HRESULT hr = pHtmlDoc2->QueryInterface(IID_IHTMLDocument3, (LPVOID*)&pHTMLDoc3); 
        ASSERT(SUCCEEDED(hr)); 
        if(pHTMLDoc3) 
        { 
            IHTMLElement* pDocElem=NULL; 
            hr = pHTMLDoc3->get_documentElement(&pDocElem); 
            ASSERT(SUCCEEDED(hr)); 
            if(pDocElem) 
            { 
                BSTR bstrHTML; 
                pDocElem->get_outerHTML(&bstrHTML); 
                pDocElem->Release(); 
                USES_CONVERSION; 
                //MessageBox(OLE2T(bstrHTML), _T("源文件")); 
                strString = OLE2T(bstrHTML); 
                SysFreeString(bstrHTML); 
            } 
            pHTMLDoc3->Release(); 
        } 
        pHtmlDoc2->Release(); 
        pHtmlDoc2=NULL; 
    }
    return TRUE; 
}

关于gethostname函数失败的问题

调用gethostname函数失败。

解决办法:调用gethostname之前, 要先调用WSAStartup才可以, 否则gethostname会失败!

下面是正确的代码

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <Winsock2.h>
#include <windows.h>
#pragma comment(lib, "Ws2_32")
int main()
{
	WSADATA wsData;
	::WSAStartup(MAKEWORD(2,2), &wsData);
	char szIP[32] = {0};
	char szHostName[32] = {0};
	int iResult = ::gethostname(szHostName, sizeof(szHostName));
	if (iResult != 0)
	{
		printf("error/n");
		return -1;
	}
	printf("%s/n", szHostName);
	hostent *pHost = ::gethostbyname(szHostName);
	::WSACleanup();
	return 0;
}