日韩黑丝制服一区视频播放|日韩欧美人妻丝袜视频在线观看|九九影院一级蜜桃|亚洲中文在线导航|青草草视频在线观看|婷婷五月色伊人网站|日本一区二区在线|国产AV一二三四区毛片|正在播放久草视频|亚洲色图精品一区

分享

將網(wǎng)頁(yè)保存為mht

 tianht 2015-06-19
#include "stdafx.h"
#include "windows.h"

//adostream中保存了,網(wǎng)頁(yè)轉(zhuǎn)為mht后的內(nèi)容
#import "c:\\program files\\common files\\system\\ado\\msado15.dll" no_namespace raw_interfaces_only

//將網(wǎng)頁(yè)轉(zhuǎn)為mht的主要組件
#import "C:\\Windows\\system32\\cdosys.dll" no_namespace raw_interfaces_only

//將unicode轉(zhuǎn)為utf-8[否則生成的文件,中文亂碼]
char* ToUTF8( WCHAR* str );

//主要函數(shù)
int SaveWholePage(wchar_t* szPageURL,wchar_t* szFileName)
{
CoInitialize(NULL);
BSTR bstr=szPageURL;
wchar_t* szUserName=L"domain\\username";//domain\\username
BSTR bstrUserName=szUserName;
wchar_t* szPass=L"domain\\username";//domain\\username
BSTR bstrPass=szPass;
IMessage *pMsg=NULL;
IConfiguration* pConfig = NULL;

HRESULT hr=CoCreateInstance( __uuidof(Message), NULL, CLSCTX_INPROC_SERVER, __uuidof(IMessage), (void**)&pMsg);
hr=CoCreateInstance(__uuidof(Configuration),NULL,CLSCTX_INPROC_SERVER,__uuidof(IConfiguration),(void**)&pConfig);
pMsg->put_Configuration (pConfig);
//將網(wǎng)頁(yè)轉(zhuǎn)為mht
hr = pMsg->CreateMHTMLBody(bstr,cdoSuppressNone,bstrUserName,bstrPass );//cdoSuppressAll
if (FAILED(hr))
{
pMsg->CreateMHTMLBody(bstr,cdoSuppressNone,bstrUserName,bstrPass );
}

//獲取到adostream中
_Stream* pStm = NULL;
pMsg->GetStream(&pStm);
pStm->put_Position(0);
pStm->put_Charset(L"UTF-8");//設(shè)置編碼
long nsize;
pStm->get_Size(&nsize);
BSTR strruss;
pStm->ReadText(nsize,&strruss);//讀取
//當(dāng)我用pstm->savetofile函數(shù)時(shí),會(huì)有不知名的錯(cuò)誤,導(dǎo)致有時(shí)無(wú)法生成,所以選擇了自己創(chuàng)建文件
HANDLE hFile = CreateFileW(szFileName,GENERIC_WRITE,0,NULL,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);
if (hFile == INVALID_HANDLE_VALUE)
{
wprintf(L"open file error\n");
return 0;
}

DWORD csize;
char* pstr = ToUTF8(strruss);//這里調(diào)用了轉(zhuǎn)碼[防止中文亂碼]
bool bRet = WriteFile(hFile,pstr,strlen(pstr),&csize,NULL);
delete pstr;
if (bRet)
{
wprintf(L"write file succeed\n");
}
CloseHandle(hFile);

pMsg->Release();
pStm->Release();
CoUninitialize();
return 1;
}

//Unicode to UTF-8
char* ToUTF8(WCHAR* str)
{
char*     pElementText;
int    iTextLen;
// wide char to multi char
iTextLen = WideCharToMultiByte( CP_UTF8,
0,
str,
-1,
NULL,
0,
NULL,
NULL);
pElementText = new char[iTextLen + 1];
memset( ( void* )pElementText, 0, sizeof( char ) * ( iTextLen + 1 ) );
WideCharToMultiByte( CP_UTF8,
0,
str,
-1,
pElementText,
iTextLen,
NULL,
NULL);
return pElementText;
}

int _tmain(int argc, _TCHAR* argv[])
{
SaveWholePage(L"http://blog./post/qstring-wchar_t-tchar-and-other-types-of-conversion-characters-or-strings/",L"E:\\test.mht");
return 0;
}


    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買(mǎi)等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶 評(píng)論公約

    類(lèi)似文章 更多