- /***********************************************************************************
- * 程序描述: 本程序为Windows7环境下,程序自删除的一种实现方式
- * 运行环境: Win7
- * 开发环境: VS2012
- * 调用接口函数库类型: Windows API,标准C库函数
- * 程序原理: Windows加载程序后,进程无法删除进程映像,利用批处理延时加载,进程退出后,
- 利用批处理删除映像文件
- ***********************************************************************************/
- #include "stdafx.h"
- #include <stdio.h>
- #include <string.h>
- #include <windows.h>
- void DelItself()
- {
- HANDLE hFile = CreateFileA("1.bat",
- GENERIC_WRITE,0,
- NULL,CREATE_ALWAYS,
- FILE_ATTRIBUTE_NORMAL,
- NULL);
- if(hFile == INVALID_HANDLE_VALUE)
- {
- return ;
- }
- char *szBat = "del DelItself.exe\\r\\n del 1.bat";
- DWORD dwNum = 0;
- //向1.bat批处理文件中写入删除进程映像的命令
- WriteFile(hFile,szBat,lstrlenA(szBat)+1,&dwNum,NULL);
- CloseHandle(hFile);
- ShellExecuteA(NULL,"open","1.bat","","",SW_HIDE);
- }
- int _tmain(int argc, _TCHAR* argv[])
- {
- char szInputStr[MAXBYTE];
- //输入delete,触发程序自删除
- scanf("%s",szInputStr);
- if(0 == strncmp(szInputStr, "delete", MAXBYTE))
- {
- printf("程序正在自删除...........\\n");
- DelItself();
- }
- return 0;
- }
- //该片段来自于http://www.codesnippet.cn/detail/1304201512221.html
来源: http://www.codesnippet.cn/detail/1304201512221.html