// 自毁程序.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <memory>
#include "Windows.h"
#include <string>
char *GetExeName()
{
//获取应用程序目录
char szapipath[MAX_PATH];
memset(szapipath, 0, MAX_PATH);
GetModuleFileNameA(NULL, szapipath, MAX_PATH);
//获取应用程序名称
char *szExe = (char *)calloc(MAX_PATH, sizeof(char));
char *pbuf = NULL;
char* szLine = strtok_s(szapipath, "\\", &pbuf);
while (NULL != szLine)
{
strcpy(szExe, szLine);
szLine = strtok_s(NULL, "\\", &pbuf);
}
//删除.exe
strncpy(szapipath, szExe, strlen(szExe) - 4);
return szExe;
}
int main()
{
//获取应用程序目录
char szapipath[MAX_PATH];//(D:\Documents\Downloads\TEST.exe)
memset(szapipath, 0, MAX_PATH);
GetModuleFileNameA(NULL, szapipath, MAX_PATH);
FILE *pFile = fopen("自毁.bat", "w");
if (!pFile)
return -1;
std::string szKill = "taskkill /f /im \"";
auto szExe = GetExeName();
szKill.append(szExe);
szKill.append("\"\n");
szKill.append("del /f \"");
szKill.append(szapipath);
szKill.append("\"\n");
szKill.append("del /f 自毁.bat\n");
fputs( szKill.data(), pFile);
free(szExe);
fclose(pFile);
system("自毁.bat");
return 0;
}