月度归档: 2010 年 4 月

  • 用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; 
    }
  • VC中, 关于UNICODE的L和_T()

    1.  UNICODE项目下,字符串用L是可以的,但项目再转成 多字节编码时, L就不能编译通过

    2.  如果用_T() 或 _TEXT()则可以在UINCODE和多字节编码项目下 都 可以无修改编译通过

  • 关于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;
    }