自毁程序
Posted endenvor
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了自毁程序相关的知识,希望对你有一定的参考价值。
1 void SelfDestroy() 2 { 3 // temporary .bat file 4 static const WCHAR tempbatname[] = L"_uninsep.bat"; 5 CString csCmd; //bat contents 6 std::string szCmd; 7 8 static WCHAR templ[] = 9 L":Repeat " 10 L"del "%s" " 11 L"if exist "%s" goto Repeat " 12 L"rmdir "%s" " 13 L"del "%s""; 14 15 WCHAR modulename[_MAX_PATH]; // absolute path of calling .exe file 16 WCHAR temppath[_MAX_PATH]; // absolute path of temporary .bat file 17 WCHAR folder[_MAX_PATH]; 18 // absolute path of temporary .bat file 19 GetTempPath(_MAX_PATH, temppath); 20 wcscat_s(temppath, tempbatname); 21 // absolute path of calling .exe file 22 GetModuleFileName(NULL, modulename, MAX_PATH); 23 wcscpy_s(folder, modulename); 24 25 PWCHAR pb = wcsrchr(folder, ‘\‘); 26 if (pb != NULL) 27 *pb = 0; 28 29 HANDLE hf; 30 31 hf = CreateFile(temppath, GENERIC_WRITE, 0, NULL, 32 CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); 33 //Long,如执行成功,则返回文件句柄。INVALID_HANDLE_VALUE表示出错,会设置GetLastError。即使函数成功,但若文件存在,且指定了CREATE_ALWAYS 或 OPEN_ALWAYS,GetLastError也会设为ERROR_ALREADY_EXISTS 34 if (hf != INVALID_HANDLE_VALUE) 35 { 36 csCmd.Format(templ, modulename, modulename, folder, temppath); 37 CStringToString(csCmd, szCmd); 38 DWORD len; 39 PWCHAR bat; 40 //缓存大小最大1024 41 WriteFile(hf, szCmd.c_str(), szCmd.size(), &len, NULL); 42 CloseHandle(hf); 43 ShellExecuteW(NULL, L"open", temppath, NULL, NULL, SW_HIDE); 44 } 45 }
以上是关于自毁程序的主要内容,如果未能解决你的问题,请参考以下文章