本文配套源碼下載
前言:
在本人用完計算機之後,往往不希望別人發現你做過什麼,和浏覽過什麼網址。(特別是和多人共用一台機子時)。當然,你可以手工清理那些記錄。 但那是多麼繁雜的一件事啊。 我想沒人願意那樣做。於是,我想到用一個軟件來一次性完成上述工作。
清除歷史記錄的文章很多,但是一般都不夠全面。本人經過整理和收集一些資料,加以融合。得到了一個較全面的歷史記錄清除方法。
下圖便是本文例子程序的運行效果圖,我們可以從中看到該例子具體能實現的清除內容。
下面我們通過代碼看看具體是如何實現的。
首先: 在 stdafx.h中要加入: #include "atlbase.h"
設置->Link 要引入庫 : Wininet.lib shlwapi.lib
//在對話框的頭文件中加入:
#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臨時文件
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 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、清收藏夾中的內容
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);
}
好了,一個簡單的清除歷史程序就寫完了, 具體細節可以分析作者提供的源代碼.
作者信息: 陳亮,[email protected]