可执行程序自己删除自己。挺有意思的,Google && csdn 了一下,还是有不少方法的。
有一个已经经过试验,挺管用的。代码如下:
- #include <afx.h>
- #include <stdio.h>
- #include <shellapi.h>
- int DeleteMyExe()
- {
- TCHAR tcsExename[MAX_PATH];
- TCHAR tcsParam[MAX_PATH * 2];
- TCHAR tcsCmd[MAX_PATH];
- HANDLE hProcess = NULL;
- // get exe filename and command shell program
- if( 0 == GetModuleFileName(NULL, tcsExename, MAX_PATH)
- || 0 == GetEnvironmentVariable(_T("COMSPEC"), tcsCmd, MAX_PATH))
- FAILRET;
- // get short filename for command shell program
- if( 0 == GetShortPathName(tcsExename, tcsExename, MAX_PATH))
- FAILRET;
- // create a command process, set its priority, then start it.
- STARTUPINFO si;
- PROCESS_INFORMATION pi;
- ZeroMemory( &si, sizeof(si) );
- si.cb = sizeof(si);
- si.dwFlags = STARTF_USESHOWWINDOW;
- si.wShowWindow = SW_HIDE;
- ZeroMemory( π, sizeof(pi) );
- _stprintf(tcsParam, _T("%s /c del %s"), tcsCmd, tcsExename);
- if(!CreateProcess(NULL,
- tcsParam,
- NULL,
- NULL,
- FALSE,
- CREATE_SUSPENDED,
- NULL,
- NULL,
- &si,
- π))
- {
- return GetLastError();
- }
- // heigthen priority of the current process
- SetPriorityClass(GetCurrentProcess(), HIGH_PRIORITY_CLASS);
- // set file attribute to normal
- SetFileAttributes(tcsExename, FILE_ATTRIBUTE_NORMAL);
- // depress priority of command process, then start it
- SetPriorityClass(pi.hProcess, IDLE_PRIORITY_CLASS);
- ResumeThread(pi.hThread);
- return 0;
- }
参考:
http://topic.csdn.net/t/20060409/11/4673437.html