程序中用了IE的內核,想取出網站中的驗證碼圖片,單獨顯示出來,調研瞭如下幾路方法canvas
一、枚舉全部緩存文件,進行處理,找到想要的,核心代碼緩存
if (0)//這段代碼能夠枚舉全部緩存資源,而後對應作處理 { LPINTERNET_CACHE_ENTRY_INFO lpCacheEntry = NULL; HANDLE hCacheDir = NULL; DWORD dwTrySize, dwEntrySize = 4096; DWORD dwError = ERROR_INSUFFICIENT_BUFFER; BOOL bSuccess, bDone = FALSE; //是否已經處理完,初始設爲false while(!bDone){ switch(dwError){ case ERROR_INSUFFICIENT_BUFFER: if(lpCacheEntry) { delete [] lpCacheEntry; } lpCacheEntry = (LPINTERNET_CACHE_ENTRY_INFO) new char[dwEntrySize]; lpCacheEntry->dwStructSize = dwEntrySize; dwTrySize = dwEntrySize; if(!hCacheDir) {//爲空,說明第一次執行,須要找第一個 hCacheDir = FindFirstUrlCacheEntry(NULL,lpCacheEntry,&dwTrySize); bSuccess = (hCacheDir != NULL); } else { bSuccess = FindNextUrlCacheEntry(hCacheDir,lpCacheEntry,&dwTrySize); } if(!bSuccess){ dwError = GetLastError(); dwEntrySize = dwTrySize; //使用獲得的新長度 } else { dwError = ERROR_SUCCESS; } break; //we are done case ERROR_NO_MORE_ITEMS: bDone = TRUE; break; //we have got an entry case ERROR_SUCCESS: if(bSuccess && (NORMAL_CACHE_ENTRY & lpCacheEntry->CacheEntryType)) { // CString sUrl1 = lpCacheEntry->lpszSourceUrlName; if (sUrl1.Compare(s) == 0) { //處理 } // CTime lastModified(lpCacheEntry->LastModifiedTime); // CTime lastAccessed(lpCacheEntry->LastAccessTime); //a changed cookie - delete it! // if(lastModified >= m_time || lastAccessed >= m_time){ //const CString name = lpCacheEntry->lpszSourceUrlName; //const CString result = lastModified.Format("%I:%M:%S%p %d %b %Y"); //MessageBox (NULL, name + " last modified " + result, "Deleting cookie", MB_OK); //DeleteUrlCacheEntry(lpCacheEntry->lpszSourceUrlName); } //get ready for next entry dwTrySize = dwEntrySize; if(!FindNextUrlCacheEntry(hCacheDir,lpCacheEntry,&dwTrySize)) { dwError = GetLastError(); dwEntrySize = dwTrySize; //使用獲得的新長度 } else { dwError = ERROR_SUCCESS; } break; //unknown error default: CString strError; strError.Format(_T("There is an error (%d) when trying deleting temporary internet files.\r\n"), dwError); TRACE(strError); bDone = TRUE; break; if(bDone) break; Sleep(10); if(lpCacheEntry){ delete [] lpCacheEntry; lpCacheEntry = NULL;} if(hCacheDir){ FindCloseUrlCache(hCacheDir); hCacheDir = NULL;} break; } } }
二、直接取某個URL對應的緩存資源cookie
if (1)//這段代碼能夠直接從緩存找到對應URL的圖片資源的路徑 { CString s = dispImg.Get(L"src"); if (1) { LPINTERNET_CACHE_ENTRY_INFO lpCacheEntry = NULL; DWORD dwEntrySize = 4096; lpCacheEntry = (LPINTERNET_CACHE_ENTRY_INFO) new char[dwEntrySize]; lpCacheEntry->dwStructSize = dwEntrySize; BOOL b = GetUrlCacheEntryInfo(s, lpCacheEntry, &dwEntrySize); CString sPath = lpCacheEntry->lpszLocalFileName; } }
三、在網頁新建一canvas,將圖片繪上,而後toDataURL,這樣能夠獲得png格式的,通過base64編碼後的字符串,該方法只能在ie9以上使用網站
if (0)//這段代碼只能在IE9以上運行,能夠將圖片繪到canvas上,再取出png格式經base64編碼後的數據 //而後解碼,存到文件中,就獲得一個png圖片 //HKEY_CURRENT_USER\Software\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_BROWSER_EMULATION //設置對應程序的dword值爲10001爲強制ie10,9999爲強制ie9,8888爲強制ie8 { CDispatch dispCanvas = disp.InvokePtr(L"createElement", L"canvas"); dispCanvas.Put(L"width", dispImg.Get(L"naturalWidth")); dispCanvas.Put(L"height", dispImg.Get(L"naturalHeight")); CDispatch dispCtx = dispCanvas.InvokePtr(L"getContext", L"2d"); dispCtx.Invoke(L"drawImage", dispImg.p, 0, 0); CString sData = dispCanvas.Invoke(L"toDataURL", L"image/png"); sData = sData.Right(sData.GetLength() - 22); int nBuffer = sData.GetLength() + 1; byte* buffer = new byte[nBuffer]; memset(buffer, 0, nBuffer); Base64Decode(sData, sData.GetLength(), buffer, &nBuffer); CFile f("1.png", CFile::modeCreate|CFile::modeWrite); f.Write(buffer, nBuffer); delete[] buffer; }