用vc清除浏览痕迹
(唐灿、喻志成 2001年05月25日 11:10)
上机浏览,总要留下一些痕迹。手工一一清除,实在烦人。那么,就让我们用编程来一劳永逸地清除浏览痕迹吧。
1.清除指定目录下的文件
众所周知,大多数的“垃圾”都放在指定的文件夹下,你只需删除这些文件即可。
使用windows api中的deletefile函数即可作到这一点。我们可以将它作成一个较完善的函数,用于删除指定文件夹下的选定类型文件。函数扩充代码如下:
void delmypointfile(lpstr name,lpstr currentpath) { //删除指定路径下的指定文件,支持通配符 //name:被删除的文件;currentpath:找到的文件路径 win32_find_data filedata; handle hsearch; char szhome[max_path]; //char szfile[max_path]; dword rightwrong; //hdc mydiadc; dword namelength; //当前的程序路径 rightwrong=getcurrentdirectory(max_path,szhome); rightwrong=setcurrentdirectory(currentpath); //保存程序执行路径,然后,把当前路径设定为需要查找的路径 hsearch = findfirstfile(name, &&filedata); if (hsearch!= invalid_handle_value) { namelength=lstrlen(filedata.cfilename); deletefile(filedata.cfilename); while(findnextfile(hsearch,&&filedata)) { //找下一个文件,找到一个删除一个 namelength=lstrlen(filedata.cfilename); deletefile(filedata.cfilename); } findclose(hsearch); //关闭查找句柄 } rightwrong=setcurrentdirectory(szhome); }
|
有了这个函数,你就可以用以下代码清除文档选单、系统临时目录和ie临时目录。
char windowrecentpath[]="\\recent"; char windowtemp[]="\\temp"; char windowietemp[]="\\temporary internet files"; char windowcookie[]="\\cookies"; char szwindowspath[max_path]; char szdelpath[max_path]; ... getwindowsdirectory(szwindowspath,max_path); lstrcpy(szdelpath,szwindowspath); lstrcat(szdelpath,windowrecentpath); //删除window最近使用的文件列表 delmypointfile("*.*",szdelpath); lstrcpy(szdelpath,szwindowspath); lstrcat(szdelpath,windowtemp); //删除window临时文件 delmypointfile("*.*",szdelpath); lstrcpy(szdelpath,szwindowspath); lstrcat(szdelpath,windowietemp); //删除window ie临时文件 delmypointfile("*.*",szdelpath);
|
2.“历史记录”的清除
ie的“历史记录”一直让人头疼不已,即使你把“历史记录”设定为零天同样能保存当天的内容;尝试用文件删除,却因为文件处于加载模式而此路不通;翻遍msdn中也找不到可以利用的api。幸好ie自身的清除功能可以作到这一点。ie是一个典型的com构件,我们可以通过调用它的组件模块来直接清除“历史记录”。
hresult clearhistory() { //建立iurlhistorystg2组件指针 iurlhistorystg2 * purlhistorystg2=null; //初始化com库 coinitialize(null); //建立客户对象 hresult hr=cocreateinstance(clsid_curlhistory,null,clsctx_inproc,iid_iurlhistorystg2,(void)&&purlhistorystg2); if(succeeded(hr)) { //接口调用 hr=purlhistorystg2->clearhistory(); purlhistorystg2->release(); } //关闭com库的联接 couninitialize(); return hr; }
|
3.清除下拉cache列表
ie的下拉cache列表随时保存你浏览过的网址,而以上的代码却不能清除它。windows api中提供了findnexturlcacheentry、deleteurlcacheentry和findfirsturlcacheentry三个函数用于清除,使用上略有点麻烦。而在window 9x中有一个更加偷懒的办法,即直接删除注册表。代码如下:
void delregcache() { //删除注册表中保存的ie cache中的记录 lpcstr rootkey="hkey_current_user"; lpcstr subkey="software\\microsoft\\internet explorer\\typedurls"; regdeletekey(hkey_current_user,subkey); } cookie的删除 cookie保存在系统目录的cookies子目录中,如果没有打开ie,则可直接删除。如果打开ie,由于它会保存一部分在内存中,你可以使用以下代码删除。 internetsetoption(0, internet_option_end_browser_session, 0, 0); lstrcpy(szdelpath,szwindowspath); lstrcat(szdelpath,windowcookie); //删除window ie临时文件 delmypointfile("*.*",szdelpath);
|
以上代码在vc++ 6.0、windows 98下运行通过,可以满足平时的大多数使用需要。然而,以上代码中delmypointfile功能较为单一,只能删除目录下的文件,对于子目录就无能为力了。有兴趣的读者可以自己编写一个递归过程来完善它的功能。