清楚ie缓存 shellapi(附使用demo)

#pragma once

#include <wininet.h>
#include <UrlHist.h>

#pragma comment(lib,"Wininet.lib")
#pragma comment(lib,"shlwapi.lib")
#define SWEEP_BUFFER_SIZE			10000

enum DEL_CACHE_TYPE //要删除的类型。
{
	File,//表示internet临时文件
	Cookie //表示Cookie
};


BOOL ChangeIEVersion(std_string exeName,DWORD dVersion);//修改IE内核版本
//【清除网页缓存】
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);



#include "StdAfx.h"
#include "libwz_web.h"

BOOL ChangeIEVersion(std_string exeName,DWORD dVersion)
{
	//写入注册表
	HKEY hKey; 
	//HKEY_CURRENT_USER\Software\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_BROWSER_EMULATION
	LSTATUS   bres;
	bres   = RegOpenKeyExA(HKEY_CURRENT_USER,
		"Software\\Microsoft\\Internet Explorer\\Main\\FeatureControl\\FEATURE_BROWSER_EMULATION", 0, KEY_SET_VALUE, &hKey);
	if(bres  !=ERROR_SUCCESS){
		return FALSE;
	}
	bres   =RegSetValueExA(hKey,exeName.c_str(),0, REG_DWORD,(const BYTE*)&dVersion,sizeof(dVersion)) ;
	if(bres  !=ERROR_SUCCESS){
		return FALSE;
	}

	//关闭注册表:
	RegCloseKey(hKey);
	return  TRUE;
}

BOOL DeleteUrlCache(DEL_CACHE_TYPE type)
{
	BOOL bRet = FALSE;
	HANDLE hEntry;
	LPINTERNET_CACHE_ENTRY_INFO lpCacheEntry = NULL;  
	DWORD dwEntrySize;

	//delete the files
	dwEntrySize = 0;
	hEntry = FindFirstUrlCacheEntry(NULL, NULL, &dwEntrySize);
	lpCacheEntry = (LPINTERNET_CACHE_ENTRY_INFO) new char[dwEntrySize];
	hEntry = FindFirstUrlCacheEntry(NULL, lpCacheEntry, &dwEntrySize);
	if (!hEntry)
	{
		goto cleanup;
	}

	do
	{
		if (type == File &&
			!(lpCacheEntry->CacheEntryType & COOKIE_CACHE_ENTRY))
		{
			DeleteUrlCacheEntry(lpCacheEntry->lpszSourceUrlName);
		}
		else if (type == Cookie &&
			(lpCacheEntry->CacheEntryType & COOKIE_CACHE_ENTRY))
		{
			DeleteUrlCacheEntry(lpCacheEntry->lpszSourceUrlName);
		}

		dwEntrySize = 0;
		FindNextUrlCacheEntry(hEntry, NULL, &dwEntrySize);
		delete [] lpCacheEntry; 
		lpCacheEntry = (LPINTERNET_CACHE_ENTRY_INFO) new char[dwEntrySize];
	}
	while (FindNextUrlCacheEntry(hEntry, lpCacheEntry, &dwEntrySize));

	bRet = TRUE;
cleanup:
	if (lpCacheEntry)
	{
		delete [] lpCacheEntry; 
	}
	return bRet;
}

BOOL EmptyDirectory(LPCTSTR szPath, BOOL bDeleteDesktopIni, 
	BOOL bWipeIndexDat)
{
	WIN32_FIND_DATA wfd;
	HANDLE hFind;
	CString sFullPath;
	CString sFindFilter;
	DWORD dwAttributes = 0;

	sFindFilter = szPath;
	sFindFilter += _T("\\*.*");
	if ((hFind = FindFirstFile(sFindFilter, &wfd)) == INVALID_HANDLE_VALUE)
	{
		return FALSE;
	}

	do
	{
		if (_tcscmp(wfd.cFileName, _T(".")) == 0 || 
			_tcscmp(wfd.cFileName, _T("..")) == 0 ||
			(bDeleteDesktopIni == FALSE && _tcsicmp(wfd.cFileName, _T("desktop.ini")) == 0))
		{
			continue;
		}

		sFullPath = szPath;
		sFullPath += _T('\\');
		sFullPath += wfd.cFileName;

		//去掉只读属性
		dwAttributes = GetFileAttributes(sFullPath);
		if (dwAttributes & FILE_ATTRIBUTE_READONLY)
		{
			dwAttributes &= ~FILE_ATTRIBUTE_READONLY;
			SetFileAttributes(sFullPath, dwAttributes);
		}

		if (wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
		{
			EmptyDirectory(sFullPath, bDeleteDesktopIni, bWipeIndexDat);
			RemoveDirectory(sFullPath);
		}
		else
		{
			if (bWipeIndexDat && _tcsicmp(wfd.cFileName, _T("index.dat")) == 0)
			{
				WipeFile(szPath, wfd.cFileName);
			}
			DeleteFile(sFullPath);
		}
	}
	while (FindNextFile(hFind, &wfd));
	FindClose(hFind);

	return TRUE;
}

//判断系统类型
BOOL IsWindowsNT()
{
	BOOL bRet = FALSE;
	BOOL bOsVersionInfoEx;
	OSVERSIONINFOEX osvi;

	// Try calling GetVersionEx using the OSVERSIONINFOEX structure,
	// If that fails, try using the OSVERSIONINFO structure.
	ZeroMemory(&osvi, sizeof(OSVERSIONINFOEX));
	osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);

	if( !(bOsVersionInfoEx = GetVersionEx ((OSVERSIONINFO *) &osvi)) )
	{
		// If OSVERSIONINFOEX doesn't work, try OSVERSIONINFO.
		osvi.dwOSVersionInfoSize = sizeof (OSVERSIONINFO);
		if (! GetVersionEx ( (OSVERSIONINFO *) &osvi) ) 
			return bRet;
	}

	if (osvi.dwPlatformId == VER_PLATFORM_WIN32_NT && osvi.dwMajorVersion <= 4)
	{
		bRet = TRUE;
	}

	return bRet;
}

BOOL IsWindows2k()
{
	BOOL bRet = FALSE;
	BOOL bOsVersionInfoEx;
	OSVERSIONINFOEX osvi;

	// Try calling GetVersionEx using the OSVERSIONINFOEX structure,
	// If that fails, try using the OSVERSIONINFO structure.
	ZeroMemory(&osvi, sizeof(OSVERSIONINFOEX));
	osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);

	if( !(bOsVersionInfoEx = GetVersionEx ((OSVERSIONINFO *) &osvi)) )
	{
		// If OSVERSIONINFOEX doesn't work, try OSVERSIONINFO.
		osvi.dwOSVersionInfoSize = sizeof (OSVERSIONINFO);
		if (! GetVersionEx ( (OSVERSIONINFO *) &osvi) ) 
			return bRet;
	}

	if (osvi.dwPlatformId == VER_PLATFORM_WIN32_NT && osvi.dwMajorVersion >= 5)
	{
		bRet = TRUE;
	}

	return bRet;
}

BOOL GetUserSid(PSID* ppSid)
{
	HANDLE hToken;
	BOOL bRes;
	DWORD cbBuffer, cbRequired;
	PTOKEN_USER pUserInfo;

	// The User's SID can be obtained from the process token访问令牌
	bRes = OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &hToken);
	if (FALSE == bRes)
	{
		return FALSE;
	}

	// Set buffer size to 0 for first call to determine
	// the size of buffer we need.
	cbBuffer = 0;
	bRes = GetTokenInformation(hToken, TokenUser, NULL, cbBuffer, &cbRequired);
	if (GetLastError() != ERROR_INSUFFICIENT_BUFFER)
	{
		return FALSE;
	}

	// Allocate a buffer for our token user data
	cbBuffer = cbRequired;
	pUserInfo = (PTOKEN_USER) HeapAlloc(GetProcessHeap(), 0, cbBuffer);
	if (NULL == pUserInfo)
	{
		return FALSE;
	}

	// Make the "real" call
	bRes = GetTokenInformation(hToken, TokenUser, pUserInfo, cbBuffer, &cbRequired);
	if (FALSE == bRes) 
	{
		return FALSE;
	}

	// Make another copy of the SID for the return value
	cbBuffer = GetLengthSid(pUserInfo->User.Sid);

	*ppSid = (PSID) HeapAlloc(GetProcessHeap(), 0, cbBuffer);
	if (NULL == *ppSid)
	{
		return FALSE;
	}

	bRes = CopySid(cbBuffer, *ppSid, pUserInfo->User.Sid);
	if (FALSE == bRes)
	{
		HeapFree(GetProcessHeap(), 0, *ppSid);
		return FALSE;
	}

	bRes = HeapFree(GetProcessHeap(), 0, pUserInfo);

	return TRUE;
}

void GetSidString(PSID pSid, LPTSTR szBuffer)
{
	//convert SID to string
	SID_IDENTIFIER_AUTHORITY *psia = ::GetSidIdentifierAuthority( pSid );
	DWORD dwTopAuthority = psia->Value[5];
	_stprintf(szBuffer, _T("S-1-%lu"), dwTopAuthority);

	TCHAR szTemp[32];
	int iSubAuthorityCount = *(GetSidSubAuthorityCount(pSid));
	for (int i = 0; i<iSubAuthorityCount; i++) 
	{
		DWORD dwSubAuthority = *(GetSidSubAuthority(pSid, i));
		_stprintf(szTemp, _T("%lu"), dwSubAuthority);
		_tcscat(szBuffer, _T("-"));
		_tcscat(szBuffer, szTemp);
	}
}

BOOL GetOldSD(HKEY hKey, LPCTSTR pszSubKey, BYTE** pSD)
{
	BOOL bRet = FALSE;
	HKEY hNewKey = NULL;
	DWORD dwSize = 0;
	LONG lRetCode;
	*pSD = NULL;

	lRetCode = RegOpenKeyEx(hKey, pszSubKey, 0, READ_CONTROL, &hNewKey);
	if(lRetCode != ERROR_SUCCESS)
		goto cleanup;

	lRetCode = RegGetKeySecurity(hNewKey, 
		(SECURITY_INFORMATION)DACL_SECURITY_INFORMATION, *pSD, &dwSize);
	if (lRetCode == ERROR_INSUFFICIENT_BUFFER)
	{
		*pSD = new BYTE[dwSize];
		lRetCode = RegGetKeySecurity(hNewKey, 
			(SECURITY_INFORMATION)DACL_SECURITY_INFORMATION, *pSD, &dwSize);
		if(lRetCode != ERROR_SUCCESS)
		{
			delete *pSD;
			*pSD = NULL;
			goto cleanup;
		}
	}
	else if (lRetCode != ERROR_SUCCESS)
		goto cleanup;

	bRet = TRUE; // indicate success

cleanup:
	if (hNewKey)
	{
		RegCloseKey(hNewKey);
	}
	return bRet;
}

BOOL CreateNewSD(PSID pSid, SECURITY_DESCRIPTOR* pSD, PACL* ppDacl)
{
	BOOL bRet = FALSE;
	PSID pSystemSid = NULL;
	SID_IDENTIFIER_AUTHORITY sia = SECURITY_NT_AUTHORITY;
	ACCESS_ALLOWED_ACE* pACE = NULL;
	DWORD dwAclSize;
	DWORD dwAceSize;

	// prepare a Sid representing local system account
	if(!AllocateAndInitializeSid(&sia, 1, SECURITY_LOCAL_SYSTEM_RID,
		0, 0, 0, 0, 0, 0, 0, &pSystemSid))
	{
		goto cleanup;
	}

	// compute size of new acl
	dwAclSize = sizeof(ACL) + 2 * (sizeof(ACCESS_ALLOWED_ACE) - sizeof(DWORD)) + 
		GetLengthSid(pSid) + GetLengthSid(pSystemSid);

	// allocate storage for Acl
	*ppDacl = (PACL)HeapAlloc(GetProcessHeap(), 0, dwAclSize);
	if(*ppDacl == NULL)
		goto cleanup;

	if(!InitializeAcl(*ppDacl, dwAclSize, ACL_REVISION))
		goto cleanup;

	//    if(!AddAccessAllowedAce(pDacl, ACL_REVISION, KEY_WRITE, pSid))
	//		goto cleanup;

	// add current user
	dwAceSize = sizeof(ACCESS_ALLOWED_ACE) - sizeof(DWORD) + GetLengthSid(pSid); 
	pACE = (ACCESS_ALLOWED_ACE *)HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, dwAceSize);

	pACE->Mask = KEY_READ | KEY_WRITE | KEY_ALL_ACCESS;
	pACE->Header.AceType = ACCESS_ALLOWED_ACE_TYPE;
	pACE->Header.AceFlags = CONTAINER_INHERIT_ACE | OBJECT_INHERIT_ACE;
	pACE->Header.AceSize = dwAceSize;

	memcpy(&pACE->SidStart, pSid, GetLengthSid(pSid));
	if (!AddAce(*ppDacl, ACL_REVISION, MAXDWORD, pACE, dwAceSize))
		goto cleanup;

	// add local system account
	HeapFree(GetProcessHeap(), 0, pACE);
	pACE = NULL;
	dwAceSize = sizeof(ACCESS_ALLOWED_ACE) - sizeof(DWORD) + GetLengthSid(pSystemSid);
	pACE = (ACCESS_ALLOWED_ACE *)HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, dwAceSize);

	pACE->Mask = KEY_READ | KEY_WRITE | KEY_ALL_ACCESS;
	pACE->Header.AceType = ACCESS_ALLOWED_ACE_TYPE;
	pACE->Header.AceFlags = CONTAINER_INHERIT_ACE | OBJECT_INHERIT_ACE;
	pACE->Header.AceSize = dwAceSize;

	memcpy(&pACE->SidStart, pSystemSid, GetLengthSid(pSystemSid));
	if (!AddAce(*ppDacl, ACL_REVISION, MAXDWORD, pACE, dwAceSize))
		goto cleanup;

	if(!InitializeSecurityDescriptor(pSD, SECURITY_DESCRIPTOR_REVISION))
		goto cleanup;

	if(!SetSecurityDescriptorDacl(pSD, TRUE, *ppDacl, FALSE))
		goto cleanup;

	bRet = TRUE; // indicate success

cleanup:
	if(pACE != NULL)
		HeapFree(GetProcessHeap(), 0, pACE);
	if(pSystemSid != NULL)
		FreeSid(pSystemSid);

	return bRet;
}

BOOL RegSetPrivilege(HKEY hKey, LPCTSTR pszSubKey, 
	SECURITY_DESCRIPTOR* pSD, BOOL bRecursive)
{
	BOOL bRet = FALSE;
	HKEY hSubKey = NULL;
	LONG lRetCode;
	LPTSTR pszKeyName = NULL;;
	DWORD dwSubKeyCnt;
	DWORD dwMaxSubKey;
	DWORD dwValueCnt;
	DWORD dwMaxValueName;
	DWORD dwMaxValueData;
	DWORD i;

	if (!pszSubKey)
		goto cleanup;

	// open the key for WRITE_DAC access
	lRetCode = RegOpenKeyEx(hKey, pszSubKey, 0, WRITE_DAC, &hSubKey);
	if(lRetCode != ERROR_SUCCESS)
		goto cleanup;

	// apply the security descriptor to the registry key
	lRetCode = RegSetKeySecurity(hSubKey, 
		(SECURITY_INFORMATION)DACL_SECURITY_INFORMATION, pSD);
	if( lRetCode != ERROR_SUCCESS )
		goto cleanup;

	if (bRecursive)
	{
		// reopen the key for KEY_READ access
		RegCloseKey(hSubKey);
		hSubKey = NULL;
		lRetCode = RegOpenKeyEx(hKey, pszSubKey, 0, KEY_READ, &hSubKey);
		if(lRetCode != ERROR_SUCCESS)
			goto cleanup;

		// first get an info about this subkey ...
		lRetCode = RegQueryInfoKey(hSubKey, 0, 0, 0, &dwSubKeyCnt, &dwMaxSubKey,
			0, &dwValueCnt, &dwMaxValueName, &dwMaxValueData, 0, 0);
		if( lRetCode != ERROR_SUCCESS )
			goto cleanup;

		// enumerate the subkeys and call RegTreeWalk() recursivly
		pszKeyName = new TCHAR [MAX_PATH + 1];
		for (i=0 ; i<dwSubKeyCnt; i++)
		{
			lRetCode = RegEnumKey(hSubKey, i, pszKeyName, MAX_PATH + 1);
			if(lRetCode == ERROR_SUCCESS)
			{
				RegSetPrivilege(hSubKey, pszKeyName, pSD, TRUE);
			}
			else if(lRetCode == ERROR_NO_MORE_ITEMS)
			{
				break;
			}
		}
		delete [] pszKeyName ;
	}

	bRet = TRUE; // indicate success

cleanup:
	if (hSubKey)
	{
		RegCloseKey(hSubKey);
	}
	return bRet;
}

BOOL WipeFile(LPCTSTR szDir, LPCTSTR szFile)
{
	CString sPath;
	HANDLE	hFile;
	DWORD	dwSize;
	DWORD	dwWrite;
	char	sZero[SWEEP_BUFFER_SIZE];
	memset(sZero, 0, SWEEP_BUFFER_SIZE);

	sPath = szDir;
	sPath += _T('\\');
	sPath += szFile;

	hFile = CreateFile(sPath, GENERIC_WRITE, 
		FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 
		FILE_ATTRIBUTE_NORMAL, NULL);
	if (hFile == INVALID_HANDLE_VALUE)
	{
		return FALSE;
	}

	dwSize = GetFileSize(hFile, NULL);

	//skip file header (actually, I don't know the file format of index.dat)
	dwSize -= 64;
	SetFilePointer(hFile, 64, NULL, FILE_BEGIN);

	while (dwSize > 0)
	{
		if (dwSize > SWEEP_BUFFER_SIZE)
		{
			WriteFile(hFile, sZero, SWEEP_BUFFER_SIZE, &dwWrite, NULL);
			dwSize -= SWEEP_BUFFER_SIZE;
		}
		else
		{
			WriteFile(hFile, sZero, dwSize, &dwWrite, NULL);
			break;
		}
	}

	CloseHandle(hFile);
	return TRUE;
}

//使用demo

	//更改网页ie内核版本
	BOOL    bRes  =FALSE;
	bRes   = ChangeIEVersion("MagicClient.exe",9000);
	if(bRes){
		logs->WriteFormat("【清除ie缓存;修改IE版本成功!/】");
	}

void ClearWebCache()
{	
	TCHAR szPath[MAX_PATH];

	//清internet临时文件
	DeleteUrlCache(File);
	if (SHGetSpecialFolderPath(NULL, szPath, CSIDL_INTERNET_CACHE, FALSE))
	{  //得到临时目录,并清空它.
		EmptyDirectory(szPath);
	}

	//Cookie的清除
	DeleteUrlCache(Cookie);
	if (SHGetSpecialFolderPath(NULL, szPath, CSIDL_COOKIES, FALSE))
	{//得到目录,并清空
		EmptyDirectory(szPath);
	}

	//清收藏夹中的内容
	if (SHGetSpecialFolderPath(NULL, szPath, CSIDL_FAVORITES, FALSE))
	{ //得到目录,并清空 
		EmptyDirectory(szPath);
	}

	//清除网络联接历史记录
	if (SHGetSpecialFolderPath(NULL, szPath, CSIDL_NETHOOD, FALSE))
	{ //得到目录,并清空
		EmptyDirectory(szPath);
	}

	// //清"文档"中的历史记录
	if (SHGetSpecialFolderPath(NULL, szPath, CSIDL_RECENT, FALSE))
	{
		EmptyDirectory(szPath);
	}

	// //清系统临时文件夹
	if (GetTempPath(MAX_PATH, szPath))//得到系统临时目录
	{
		EmptyDirectory(szPath, TRUE);
	}

	SHDeleteKey(HKEY_CURRENT_USER, 
		_T("Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\RecentDocs"));

	//浏览器地址栏历史地址的清除
	SHDeleteKey(HKEY_CURRENT_USER, 
		_T("Software\\Microsoft\\Internet Explorer\\TypedURLs"));

	// 清除自动密码历史记录
	SHDeleteKey(HKEY_CURRENT_USER, 
		_T("Software\\Microsoft\\Internet Explorer\\IntelliForms"));

	// 清RAS自动拨号历史记录
	SHDeleteKey(HKEY_CURRENT_USER, 
		_T("Software\\Microsoft\\RAS Autodial\\Addresses"));

	// 清空回收站
	SHEmptyRecycleBin(NULL, NULL, 
		SHERB_NOCONFIRMATION | SHERB_NOPROGRESSUI | SHERB_NOSOUND);

	// 清除"运行"中的自动匹配历史记录
	SHDeleteKey(HKEY_CURRENT_USER, 
		_T("Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\RunMRU"));

	// 清除上次登陆用户记录

	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"));

	//清除"查找文件"自动匹配历史记录

	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"));

	// 清除"查找计算机"自动匹配历史记录
	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"));

	// 清远程登录历史记录
	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"));
	/
	// 清除表单自动完成历史记录
	CString  sKeyTwo;
	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);

			sKeyTwo = _T("Software\\Microsoft\\Protected Storage System Provider\\");
			sKeyTwo += szSid;

			//get old SD
			sBaseKey = sKeyTwo;
			GetOldSD(HKEY_CURRENT_USER, sBaseKey, &pOldSD);

			//set new SD and then clear
			if (CreateNewSD(pSid, &NewSD, &pDacl))
			{
				RegSetPrivilege(HKEY_CURRENT_USER, sKeyTwo, &NewSD, FALSE);

				sKeyTwo += _T("\\Data");
				RegSetPrivilege(HKEY_CURRENT_USER, sKeyTwo, &NewSD, FALSE);

				sKeyTwo += _T("\\e161255a-37c3-11d2-bcaa-00c04fd929db");
				RegSetPrivilege(HKEY_CURRENT_USER, sKeyTwo, &NewSD, TRUE);

				dwRet = SHDeleteKey(HKEY_CURRENT_USER, sKeyTwo);
			}

			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);

	sKeyTwo = _T("Software\\Microsoft\\Protected Storage System Provider\\");
	sKeyTwo += szUserName;
	sKeyTwo += _T("\\Data\\e161255a-37c3-11d2-bcaa-00c04fd929db");
	dwRet = SHDeleteKey(HKEY_LOCAL_MACHINE, sKeyTwo);

	
	// 清浏览网址历史记录
	HRESULT hr;
	IUrlHistoryStg2* pUrlHistoryStg2 = NULL;
	hr = CoCreateInstance(CLSID_CUrlHistory, NULL, 
		CLSCTX_INPROC_SERVER, IID_IUrlHistoryStg2, 
		(void**)&pUrlHistoryStg2);
	if (SUCCEEDED(hr))
	{
		hr = pUrlHistoryStg2->ClearHistory(); 
		pUrlHistoryStg2->Release();
	}

	// 如果上面代码不能清
	// 则有下面的,不完美,但能工作.
	GetWindowsDirectory(szPath, MAX_PATH);
	_tcscat(szPath, _T("\\History"));
	EmptyDirectory(szPath, FALSE, TRUE);

	if (SHGetSpecialFolderPath(NULL, szPath, CSIDL_HISTORY, FALSE))
	{
		EmptyDirectory(szPath, FALSE, TRUE);
	}
	///
}


//duilib 网页控件 不读缓存的访问网页(经测试还是上面的方面管用)

		IWebBrowser2*    pWb =   m_ChatWeb->GetWebBrowser2();
		CVariant tempUrl,noCache;
		tempUrl.vt=VT_BSTR;
		tempUrl.bstrVal=T2BSTR(CA2T(url.c_str()));
		noCache.vt = VT_INT;
		noCache.intVal  =BrowserNavConstants::navNoReadFromCache | BrowserNavConstants::navNoHistory;
		if(pWb){
			HRESULT   hr  =pWb->Navigate2(&tempUrl,&noCache,NULL,NULL,NULL);
		}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
使用管理员权限通过C++代码修改Windows本机时间,可以使用Windows API提供的一些函数来获取管理员权限,例如ShellExecute和CreateProcessWithLogonW等函数。以下是一个示例代码: ```c++ #include <Windows.h> #include <Shellapi.h> #include <iostream> int main() { // 构造SYSTEMTIME结构体,保存新的时间值 SYSTEMTIME st = { 0 }; st.wYear = 2021; st.wMonth = 9; st.wDay = 1; st.wHour = 15; st.wMinute = 30; st.wSecond = 0; // 构造命令行参数,指定需要执行的命令和参数 std::wstring command = L"cmd.exe /c date "; command += std::to_wstring(st.wYear) + L"/" + std::to_wstring(st.wMonth) + L"/" + std::to_wstring(st.wDay); std::wstring time = std::to_wstring(st.wHour) + L":" + std::to_wstring(st.wMinute) + L":" + std::to_wstring(st.wSecond); command += " && time " + time; // 使用ShellExecute函数启动命令行窗口,并获取管理员权限 SHELLEXECUTEINFO sei = { 0 }; sei.cbSize = sizeof(sei); sei.fMask = SEE_MASK_NOCLOSEPROCESS; sei.lpVerb = L"runas"; sei.lpFile = L"cmd.exe"; sei.lpParameters = command.c_str(); sei.nShow = SW_HIDE; BOOL success = ShellExecuteEx(&sei); if (!success) { std::cout << "启动命令行窗口失败" << std::endl; return 1; } // 等待命令行窗口执行完毕 WaitForSingleObject(sei.hProcess, INFINITE); // 获取命令行窗口的退出码,判断是否执行成功 DWORD exitCode = 0; GetExitCodeProcess(sei.hProcess, &exitCode); if (exitCode == 0) { std::cout << "本机时间已修改" << std::endl; } else { std::cout << "本机时间修改失败" << std::endl; } // 关闭命令行窗口句柄 CloseHandle(sei.hProcess); return 0; } ``` 这份代码中,我们使用ShellExecuteEx函数启动了一个命令行窗口,并指定了需要执行的命令和参数。在lpVerb参数中,我们传入了"runas",表示需要获取管理员权限。等待命令行窗口执行完毕后,我们通过GetExitCodeProcess函数获取命令行窗口的退出码,判断是否执行成功。需要注意的是,这种方法需要用户授权,才能获取管理员权限,因此可能会对用户造成不便。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值