程序自删除的一种实现方式

来源:互联网 发布:java for循环嵌套 编辑:程序博客网 时间:2024/05/29 16:07
/********************************************************************************** * 程序描述: 本程序为Windows7环境下,程序自删除的一种实现方式 * 运行环境: Win7 * 开发环境: VS2012 * 调用接口函数库类型: Windows API,标准C库函数 * 程序原理: Windows加载程序后,进程无法删除进程映像,利用进程优先级,                父进程结束,子进程删除父进程映像后结束,实现自删除 ***********************************************************************************/#include <stdio.h>#include <string.h> #include<windows.h>#include<ShlObj.h>#include <tchar.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[]){     DelItself();    return 0;}

0 0
原创粉丝点击