C++:世界上唯一能让程序员在配置环境上|

香菇0_0

园龄:2年7个月粉丝:10关注:1

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 中国大陆许可协议进行许可。

posted @   香菇0_0  阅读(158)  评论(0编辑  收藏  举报
点击右上角即可分享
微信分享提示
评论
收藏
关注
推荐
深色
回顶
收起
  1. 1 404 not found REOL
404 not found - REOL
00:00 / 00:00
An audio error has occurred.