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;
}

posted @ 2022-07-23 16:39  香菇0_0  阅读(192)  评论(0)    收藏  举报