程序自删除的2钟方法
#include "stdafx.h"
#include <stdio.h>
#include <string.h>
#include<windows.h>
#include<ShlObj.h>
VOID DelItself()
{
SHELLEXECUTEINFO stShellDel;
TCHAR szBat[MAX_PATH];
//获取文件路径名
TCHAR szFileName[MAX_PATH],szComspec[MAX_PATH];
if((GetModuleFileName(0,szFileName,MAX_PATH)!=0) &&
(GetShortPathName(szFileName,szFileName,MAX_PATH)!=0) &&
(GetEnvironmentVariable(L"COMSPEC",szComspec,MAX_PATH)!=0))
{
lstrcpy(szBat,L"/c del ");
lstrcat(szBat, szFileName);
lstrcat(szBat, L" > nul");
stShellDel.cbSize = sizeof(stShellDel);
//命令窗口进程句柄,ShellExecuteEx函数执行时设置。
stShellDel.hwnd = 0;
stShellDel.lpVerb = L"Open";
stShellDel.lpFile = szComspec;
stShellDel.lpParameters = szBat;
stShellDel.lpDirectory = NULL;
stShellDel.nShow = SW_HIDE;
//设置为SellExecuteEx函数结束后进程退出。
stShellDel.fMask = SEE_MASK_NOCLOSEPROCESS;
//创建执行命令窗口进程。
if(ShellExecuteEx(&stShellDel))
{
//设置命令行进程的执行级别为空闲执行,这使本程序有足够的时间从内存中退出。
SetPriorityClass(stShellDel.hProcess,IDLE_PRIORITY_CLASS);
//设置本程序进程的执行级别为实时执行,这保证本程序能立即获取CPU执行权,快速退出。
SetPriorityClass(GetCurrentProcess(), REALTIME_PRIORITY_CLASS);
SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_TIME_CRITICAL);
//通知Windows资源管理器,本程序文件已经被删除。
SHChangeNotify(SHCNE_DELETE, SHCNF_PATH, szFileName, 0);
ExitProcess(0);
}
}
}
int _tmain(int argc, _TCHAR* argv[])
{
char szInputStr[MAXBYTE];
//输入delete,触发程序自删除
scanf("%s", szInputStr);
if(0 == (strncmp(szInputStr, "delete", MAXBYTE)))
{
DelItself();
}
return 0;
第一种
/**********************************************************************************
* 程序描述: 本程序为Windows7环境下,程序自删除的一种实现方式
* 运行环境: Win7
* 开发环境: VS2012
* 调用接口函数库类型: Windows API,标准C库函数
* 程序原理: Windows加载程序后,进程无法删除进程映像,利用进程优先级,
父进程结束,子进程删除父进程映像后结束,实现自删除
***********************************************************************************/
#include "stdafx.h"
#include <stdio.h>
#include <string.h>
#include<windows.h>
#include<ShlObj.h>
VOID DelItself()
{
SHELLEXECUTEINFO stShellDel;
TCHAR szBat[MAX_PATH];
//获取文件路径名
TCHAR szFileName[MAX_PATH],szComspec[MAX_PATH];
if((GetModuleFileName(0,szFileName,MAX_PATH)!=0) &&
(GetShortPathName(szFileName,szFileName,MAX_PATH)!=0) &&
(GetEnvironmentVariable(L"COMSPEC",szComspec,MAX_PATH)!=0))
{
lstrcpy(szBat,L"/c del ");
lstrcat(szBat, szFileName);
lstrcat(szBat, L" > nul");
stShellDel.cbSize = sizeof(stShellDel);
//命令窗口进程句柄,ShellExecuteEx函数执行时设置。
stShellDel.hwnd = 0;
stShellDel.lpVerb = L"Open";
stShellDel.lpFile = szComspec;
stShellDel.lpParameters = szBat;
stShellDel.lpDirectory = NULL;
stShellDel.nShow = SW_HIDE;
//设置为SellExecuteEx函数结束后进程退出。
stShellDel.fMask = SEE_MASK_NOCLOSEPROCESS;
//创建执行命令窗口进程。
if(ShellExecuteEx(&stShellDel))
{
//设置命令行进程的执行级别为空闲执行,这使本程序有足够的时间从内存中退出。
SetPriorityClass(stShellDel.hProcess,IDLE_PRIORITY_CLASS);
//设置本程序进程的执行级别为实时执行,这保证本程序能立即获取CPU执行权,快速退出。
SetPriorityClass(GetCurrentProcess(), REALTIME_PRIORITY_CLASS);
SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_TIME_CRITICAL);
//通知Windows资源管理器,本程序文件已经被删除。
SHChangeNotify(SHCNE_DELETE, SHCNF_PATH, szFileName, 0);
ExitProcess(0);
}
}
}
int _tmain(int argc, _TCHAR* argv[])
{
char szInputStr[MAXBYTE];
//输入delete,触发程序自删除
scanf("%s", szInputStr);
if(0 == (strncmp(szInputStr, "delete", MAXBYTE)))
{
DelItself();
}
return 0;
}
第二种
/***********************************************************************************
* 程序描述: 本程序为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;
}