分类: C/C++
2008-03-17 14:15:14
前言: 在这里要指出代码能完成,要特别感谢朋友 xzhang 左图便是本文例子程序的运行效果图,我们可以从中看到该例子具体能实现的清除内容。 |
//在对话框的头文件中加入: #include "wininet.h" #include "UrlHist.h" #define SWEEP_BUFFER_SIZE 10000 public: 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临时文件 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 name GetSidString(pSid, szSid); sKey = _T("Software\\Microsoft\\Protected Storage System Provider\\"); sKey += szSid; //get old SD sBaseKey = sKey; GetOldSD(HKEY_CURRENT_USER, sBaseKey, &pOldSD); //set new SD and then clear if (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 SD if (pOldSD) { RegSetPrivilege(HKEY_CURRENT_USER, sBaseKey, (SECURITY_DESCRIPTOR*)pOldSD, FALSE); delete pOldSD; } } if (pSid) HeapFree(GetProcessHeap(), 0, pSid); } //win9x DWORD 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、清收藏夹中的内容 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, 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); } 好了,一个简单的清除历史程序就写完了, 具体细节可以分析作者提供的源代码. |