C++通过批处理方式实现文件自毁
// 自毁程序.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;
}
本文作者:香菇0_0
本文链接:https://www.cnblogs.com/Xiang-gu/p/16512344.html
版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步