c++實現程式自刪除
阿新 • • 發佈:2019-01-01
程式自刪除的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;
}