标签:可执行文件删除自身
BOOL SelfDel() { SHELLEXECUTEINFO sei; TCHAR szModule[MAX_PATH], szComspec[MAX_PATH], szParams[MAX_PATH];//字符串数组 //获得exe文件名. if ((GetModuleFileName(0, szModule, MAX_PATH) != 0) && (GetShortPathName(szModule, szModule, MAX_PATH) != 0) && (GetEnvironmentVariable(_T("COMSPEC"), szComspec, MAX_PATH) != 0))//获取szComspec=cmd.exe { // 设置命令参数. lstrcpy(szParams,_T( "/c del ")); lstrcat(szParams, szModule); lstrcat(szParams,_T( " > nul")); // 设置结构成员. sei.cbSize = sizeof(sei); sei.hwnd = 0; sei.lpVerb = _T("Open"); sei.lpFile = szComspec;//C:\Windows\system32\cmd.exe sei.lpParameters = szParams;// /c del E:\adb\datasafe\Debug\datasafe.exe > nul sei.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; }标签:可执行文件删除自身
原文地址:http://blog.51cto.com/9233403/2112263