VC实现文件自我删除

来源:互联网 发布:pubmed数据库电脑官网 编辑:程序博客网 时间:2024/06/05 23:57

http://blog.csdn.net/itcastcpp/article/details/7179948

#include <afx.h>//需要使用mfc库#include <sys/stat.h>  //加入状态显示头文件.#include <ShellAPI.h>#include <Shlobj.h>BOOL SelfDel1(){SHELLEXECUTEINFO sei;TCHAR szModule [MAX_PATH],szComspec[MAX_PATH],szParams [MAX_PATH];//字符串数组// 获得文件名.if((GetModuleFileName(0,szModule,MAX_PATH)!=0) &&(GetShortPathName(szModule,szModule,MAX_PATH)!=0) &&(GetEnvironmentVariable("COMSPEC",szComspec,MAX_PATH)!=0))//获取szComspec=cmd.exe{// 设置命令参数.lstrcpy(szParams,"/c del ");lstrcat(szParams, szModule);lstrcat(szParams, " > nul");// 设置结构成员.sei.cbSize = sizeof(sei);sei.hwnd = 0;sei.lpVerb = "Open";sei.lpFile = szComspec;//C:\Windows\system32\cmd.exesei.lpParameters = szParams;//  /c del E:\adb\datasafe\Debug\datasafe.exe > nulsei.lpDirectory = 0;sei.nShow = SW_HIDE;sei.fMask = SEE_MASK_NOCLOSEPROCESS;// 执行shell命令.if(ShellExecuteEx(&sei)){// 设置命令行进程的执行级别为空闲执行,使本程序有足够的时间从内存中退出. SetPriorityClass(sei.hProcess,IDLE_PRIORITY_CLASS);SetPriorityClass(GetCurrentProcess(),REALTIME_PRIORITY_CLASS);SetThreadPriority(GetCurrentThread(),THREAD_PRIORITY_TIME_CRITICAL);// 通知Windows资源浏览器,本程序文件已经被删除.SHChangeNotify(SHCNE_DELETE,SHCNF_PATH,szModule,0);return TRUE;}}return FALSE;} BOOL SelfDel2(){CStdioFilefile;CFileException fileEx;TCHAR szDir[MAX_PATH];TCHAR szModule[MAX_PATH];GetModuleFileName(0, szModule, sizeof(szModule));   // 获得应用程序名.GetCurrentDirectory(MAX_PATH, szDir);               // 获得文件的当前目录.CString strFilePath=CString(szDir)+"\\tempDel.bat";   // 临时批处理文件名.if(!file.Open(strFilePath,CFile::modeWrite | CFile::typeText | CFile::modeCreate,&fileEx)){#ifdef _DEBUGafxDump << "The file could not be opened " << strFilePath<<"\n";afxDump << "Cause :"<<fileEx.m_cause << "\n";#endifreturn FALSE;}CString strCmdLine1,strCmdLine2;strCmdLine1.Format("del %s\n",szModule);//del E:\adb\datasafe\Debug\datasafe.exestrCmdLine2.Format("del %%0\n");//del %0file.WriteString(strCmdLine1);                    // 写删除EXE的命令行.file.WriteString(strCmdLine2);                    // 写删除BAT的命令行.file.Close();WinExec(strFilePath,SW_HIDE);                     // 执行自行删除操作.return TRUE;}void main(){//SelfDel1();SelfDel2();}


0 0
原创粉丝点击