文件自删除多用于病毒,在病毒执行完之后要自我销毁避免被人发现。
下面的代码是文件自删除的方法之一,通过Cmd程序来完成的。代码如下:
#include<iostream>
#include<Windows.h>#include<ShlObj.h>
using namespace std;
BOOL SeSuicideByCmdProcess();
int main()
{
SeSuicideByCmdProcess();
return 0;
}
BOOL SeSuicideByCmdProcess()
{
SHELLEXECUTEINFO ShellExecuteInfo = { 0 };
WCHAR ProcessFullPath[MAX_PATH] = { 0 };
WCHAR CmdProcesFullPath[MAX_PATH] = { 0 };
WCHAR ParameterData[MAX_PATH] = { 0 };
// 获得文件完整路径. 获得Cmd进程完整路径.
if ((GetModuleFileName(0, ProcessFullPath, MAX_PATH) != 0) &&
(GetEnvironmentVariable(L"COMSPEC", CmdProcesFullPath, MAX_PATH) != 0))
{
// 设置命令参数.
wcscpy(ParameterData, L"/c del ");
wcscat(ParameterData, ProcessFullPath);
wcscat(ParameterData, L" > nul");
// 设置结构成员.
ShellExecuteInfo.cbSize = sizeof(SHELLEXECUTEINFO);
ShellExecuteInfo.lpVerb = L"Open";
ShellExecuteInfo.lpFile = CmdProcesFullPath;
ShellExecuteInfo.lpParameters = ParameterData;
ShellExecuteInfo.nShow = SW_HIDE;
ShellExecuteInfo.fMask = SEE_MASK_NOCLOSEPROCESS;
// 执行shell命令.
if (ShellExecuteEx(&ShellExecuteInfo))
{
// 设置命令行进程的执行级别为空闲执行,使本程序有足够的时间从内存中退出.
SetPriorityClass(ShellExecuteInfo.hProcess, IDLE_PRIORITY_CLASS);
SetPriorityClass(GetCurrentProcess(), REALTIME_PRIORITY_CLASS);
SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_TIME_CRITICAL);
SHChangeNotify(SHCNE_DELETE, SHCNF_PATH, ProcessFullPath, 0);
return TRUE;
}
}
return FALSE;
}