IE快取清除
原文轉載自:http://blog.csdn.net/whatday/article/details/7566925
首先: 在 stdafx.h中要加入: #include "atlbase.h"
設定->Link 要引入庫 : Wininet.lib shlwapi.lib
//在對話方塊的標頭檔案中加入:
#include "wininet.h"#include "UrlHist.h"#define SWEEP_BUFFER_SIZE 10000public:enum DEL_CACHE_TYPE //要刪除的型別。{File,//表示internet臨時檔案Cookie //表示Cookie};
//以下為一些輔助函式 ( 輔助函式的具體實現參見原始碼中.限於篇幅不再給出 )
BOOL DeleteUrlCache(DEL_CACHE_TYPE type);BOOL EmptyDirectory(LPCTSTR szPath, BOOL bDeleteDesktopIni = FALSE, BOOL bWipeIndexDat = FALSE);//清空一個目錄BOOL IsWindowsNT();//判斷系統BOOL IsWindows2k();BOOL GetUserSid(PSID* ppSid);void GetSidString(PSID pSid, LPTSTR szBuffer);BOOL GetOldSD(HKEY hKey, LPCTSTR pszSubKey, BYTE** pSD);BOOL CreateNewSD(PSID pSid, SECURITY_DESCRIPTOR* pSD, PACL* ppDacl);BOOL RegSetPrivilege(HKEY hKey, LPCTSTR pszSubKey, SECURITY_DESCRIPTOR* pSD, BOOL bRecursive);BOOL WipeFile(LPCTSTR szDir, LPCTSTR szFile);
//現在我們可以分別實現每項清除功能。
1、清除IE臨時檔案
TCHAR szPath[MAX_PATH];
DeleteUrlCache(File);
if (SHGetSpecialFolderPath(NULL, szPath, CSIDL_INTERNET_CACHE, FALSE))
{ //得到臨時目錄,並清空它.
EmptyDirectory(szPath);
}
清除IE臨時檔案的另外一個途徑是直接呼叫Wininet函式,請看下面用於清除IE臨時檔案的函式
BOOL DelTempFiles() { BOOL bResult = FALSE; BOOL bDone = FALSE; LPINTERNET_CACHE_ENTRY_INFO lpCacheEntry = NULL; DWORD dwTrySize, dwEntrySize = 4096; // start buffer size HANDLE hCacheDir = NULL; DWORD dwError = ERROR_INSUFFICIENT_BUFFER; do { switch (dwError) { // need a bigger buffer case ERROR_INSUFFICIENT_BUFFER: delete [] lpCacheEntry; lpCacheEntry = (LPINTERNET_CACHE_ENTRY_INFO) new char[dwEntrySize]; lpCacheEntry->dwStructSize = dwEntrySize; dwTrySize = dwEntrySize; BOOL bSuccess; if (hCacheDir == NULL) bSuccess = (hCacheDir = FindFirstUrlCacheEntry(NULL, lpCacheEntry, &dwTrySize)) != NULL; else bSuccess = FindNextUrlCacheEntry(hCacheDir, lpCacheEntry, &dwTrySize); if (bSuccess) dwError = ERROR_SUCCESS; else { dwError = GetLastError(); dwEntrySize = dwTrySize; // use new size returned } break; // we are done case ERROR_NO_MORE_ITEMS: bDone = TRUE; bResult = TRUE; break; // we have got an entry case ERROR_SUCCESS: // don''''''''''''''''t delete cookie entry if (!(lpCacheEntry->CacheEntryType & COOKIE_CACHE_ENTRY)) DeleteUrlCacheEntry(lpCacheEntry->lpszSourceUrlName); // get ready for next entry dwTrySize = dwEntrySize; if (FindNextUrlCacheEntry(hCacheDir, lpCacheEntry, &dwTrySize)) dwError = ERROR_SUCCESS; else { dwError = GetLastError(); dwEntrySize = dwTrySize; // use new size returned } break; // unknown error default: bDone = TRUE; break; } if (bDone) { delete [] lpCacheEntry; if (hCacheDir) FindCloseUrlCache(hCacheDir); } } while (!bDone); return bResult;}
2、Cookie的清除
TCHAR szPath[MAX_PATH];DeleteUrlCache(Cookie);if (SHGetSpecialFolderPath(NULL, szPath, CSIDL_COOKIES, FALSE)){//得到目錄,並清空 EmptyDirectory(szPath);}
3、瀏覽器位址列歷史地址的清除
SHDeleteKey(HKEY_CURRENT_USER, _T("Software\\Microsoft\\Internet Explorer\\TypedURLs"));
4、清除表單自動完成歷史記錄
CString sKey;DWORD dwRet;if (IsWindows2k() || IsWindowsNT())//先判斷系統{CString sBaseKey;SECURITY_DESCRIPTOR NewSD;BYTE* pOldSD;PACL pDacl = NULL;PSID pSid = NULL;TCHAR szSid[256];if (GetUserSid(&pSid)){//get the hiden key nameGetSidString(pSid, szSid);sKey = _T("Software\\Microsoft\\Protected Storage System Provider\\");sKey += szSid;//get old SDsBaseKey = sKey;GetOldSD(HKEY_CURRENT_USER, sBaseKey, &pOldSD);//set new SD and then clearif (CreateNewSD(pSid, &NewSD, &pDacl)){RegSetPrivilege(HKEY_CURRENT_USER, sKey, &NewSD, FALSE);sKey += _T("\\Data");RegSetPrivilege(HKEY_CURRENT_USER, sKey, &NewSD, FALSE);sKey += _T("\\e161255a-37c3-11d2-bcaa-00c04fd929db");RegSetPrivilege(HKEY_CURRENT_USER, sKey, &NewSD, TRUE);dwRet = SHDeleteKey(HKEY_CURRENT_USER, sKey);}if (pDacl != NULL)HeapFree(GetProcessHeap(), 0, pDacl);//restore old SDif (pOldSD){RegSetPrivilege(HKEY_CURRENT_USER, sBaseKey, (SECURITY_DESCRIPTOR*)pOldSD, FALSE);delete pOldSD;}}if (pSid)HeapFree(GetProcessHeap(), 0, pSid);}//win9xDWORD dwSize = MAX_PATH;TCHAR szUserName[MAX_PATH];GetUserName(szUserName, &dwSize);sKey = _T("Software\\Microsoft\\Protected Storage System Provider\\");sKey += szUserName;sKey += _T("\\Data\\e161255a-37c3-11d2-bcaa-00c04fd929db");dwRet = SHDeleteKey(HKEY_LOCAL_MACHINE, sKey);
5、清除自動密碼歷史記錄
SHDeleteKey(HKEY_CURRENT_USER, _T("Software\\Microsoft\\Internet Explorer\\IntelliForms"));
6、清收藏夾中的內容
TCHAR szPath[MAX_PATH];
if (SHGetSpecialFolderPath(NULL, szPath, CSIDL_FAVORITES, FALSE))
{ //得到目錄,並清空
EmptyDirectory(szPath);
}
7、清RAS自動撥號歷史記錄
SHDeleteKey(HKEY_CURRENT_USER, _T("Software\\Microsoft\\RAS Autodial\\Addresses"));
8、清系統臨時資料夾
TCHAR szPath[MAX_PATH];if (GetTempPath(MAX_PATH, szPath))//得到系統臨時目錄{ EmptyDirectory(szPath, TRUE);}
9、清空回收站
SHEmptyRecycleBin(NULL, NULL,
SHERB_NOCONFIRMATION | SHERB_NOPROGRESSUI | SHERB_NOSOUND);
10、清除"執行"中的自動匹配歷史記錄
SHDeleteKey(HKEY_CURRENT_USER, _T("Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\RunMRU"));
11、清"文件"中的歷史記錄
TCHAR szPath[MAX_PATH];if (SHGetSpecialFolderPath(NULL, szPath, CSIDL_RECENT, FALSE)){ EmptyDirectory(szPath);} SHDeleteKey(HKEY_CURRENT_USER,_T("Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\RecentDocs"));
12、清除上次登陸使用者記錄
SHDeleteValue(HKEY_CURRENT_USER, _T("Software\\Microsoft\\Windows NT\\CurrentVersion\\Winlogon"), _T("DefaultUserName"));SHDeleteValue(HKEY_CURRENT_USER, _T("Software\\Microsoft\\Windows NT\\CurrentVersion\\Winlogon"), _T("AltDefaultUserName"));SHDeleteValue(HKEY_LOCAL_MACHINE, _T("Software\\Microsoft\\Windows\\CurrentVersion\\Winlogon"), _T("DefaultUserName"));
13、清除"查詢檔案"自動匹配歷史記錄
SHDeleteKey(HKEY_CURRENT_USER, _T("Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Doc Find Spec MRU"));SHDeleteKey(HKEY_CURRENT_USER, _T("Software\\Microsoft\\Internet Explorer\\Explorer Bars\\{C4EE31F3-4768-11D2-BE5C-00A0C9A83DA1}\\ContainingTextMRU"));SHDeleteKey(HKEY_CURRENT_USER, _T("Software\\Microsoft\\Internet Explorer\\Explorer Bars\\{C4EE31F3-4768-11D2-BE5C-00A0C9A83DA1}\\FilesNamedMRU"));
14、清除"查詢計算機"自動匹配歷史記錄
SHDeleteKey(HKEY_CURRENT_USER, _T("Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\FindComputerMRU"));SHDeleteKey(HKEY_CURRENT_USER, _T("Software\\Microsoft\\Internet Explorer\\Explorer Bars\\{C4EE31F3-4768-11D2-BE5C-00A0C9A83DA1}\\ComputerNameMRU"));
15、清除網路聯接歷史記錄
TCHAR szPath[MAX_PATH];if (SHGetSpecialFolderPath(NULL, szPath, CSIDL_NETHOOD, FALSE)){ //得到目錄,並清空 EmptyDirectory(szPath);}
16、清遠端登入歷史記錄
CString sKey;for (int i=1; i<=8; i++){ sKey.Format(_T("Machine%d"), i); SHDeleteValue(HKEY_CURRENT_USER, _T("Software\\Microsoft\\Telnet"), sKey); sKey.Format(_T("Service%d"), i); SHDeleteValue(HKEY_CURRENT_USER, _T("Software\\Microsoft\\Telnet"), sKey); sKey.Format(_T("TermType%d"), i); SHDeleteValue(HKEY_CURRENT_USER, _T("Software\\Microsoft\\Telnet"), sKey);}SHDeleteValue(HKEY_CURRENT_USER, _T("Software\\Microsoft\\Telnet"), _T("LastMachine"));SHDeleteValue(HKEY_CURRENT_USER, _T("Software\\Microsoft\\Telnet"), _T("LastService"));SHDeleteValue(HKEY_CURRENT_USER, _T("Software\\Microsoft\\Telnet"), _T("LastTermType"));
17、清瀏覽網址歷史記錄
下面這個函式可以用於清除網址歷史記錄
#include "SHLGUID.H"HRESULT ClearHistory() {HRESULT hr;CoInitialize(NULL);{ IUrlHistoryStg2* pUrlHistoryStg2 = NULL; hr= CoCreateInstance(CLSID_CUrlHistory,NULL,1, IID_IUrlHistoryStg2,(void**)&pUrlHistoryStg2); if (SUCCEEDED(hr)) { hr = pUrlHistoryStg2->ClearHistory(); pUrlHistoryStg2->Release(); }}CoUninitialize(); return hr;}
// 如果上面程式碼不能清
// 則有下面的,不完美,但能工作.
TCHAR szPath[MAX_PATH];GetWindowsDirectory(szPath, MAX_PATH);_tcscat(szPath, _T("\\History"));EmptyDirectory(szPath, FALSE, TRUE); if (SHGetSpecialFolderPath(NULL, szPath, CSIDL_HISTORY, FALSE)){ EmptyDirectory(szPath, FALSE, TRUE);}