可执行文件自我删除

可执行程序自己删除自己。挺有意思的,Google && csdn 了一下,还是有不少方法的。
有一个已经经过试验,挺管用的。代码如下:




  1. #include <afx.h>
  2. #include <stdio.h>
  3. #include <shellapi.h>
  4. int DeleteMyExe()
  5. {
  6.     TCHAR tcsExename[MAX_PATH];
  7.     TCHAR tcsParam[MAX_PATH * 2];
  8.     TCHAR tcsCmd[MAX_PATH];
  9.     HANDLE hProcess = NULL;
  10.     
  11.     // get exe filename and command shell program
  12.     if( 0 == GetModuleFileName(NULL, tcsExename, MAX_PATH) 
  13.         ||  0 == GetEnvironmentVariable(_T("COMSPEC"), tcsCmd, MAX_PATH))
  14.         FAILRET;
  15.     
  16.     // get short filename for command shell program
  17.     if( 0 == GetShortPathName(tcsExename, tcsExename, MAX_PATH))
  18.         FAILRET;
  19.     
  20.     // create a command process, set its priority, then start it.
  21.     STARTUPINFO si;
  22.     PROCESS_INFORMATION pi;
  23.     
  24.     ZeroMemory( &si, sizeof(si) );
  25.     si.cb          = sizeof(si);
  26.     si.dwFlags     = STARTF_USESHOWWINDOW;
  27.     si.wShowWindow = SW_HIDE;
  28.     ZeroMemory( π, sizeof(pi) );
  29.     
  30.     _stprintf(tcsParam, _T("%s /c del %s"), tcsCmd, tcsExename);
  31.     if(!CreateProcess(NULL,
  32.         tcsParam,
  33.         NULL,
  34.         NULL,
  35.         FALSE,
  36.         CREATE_SUSPENDED,
  37.         NULL,
  38.         NULL,
  39.         &si,
  40.         π))
  41.     {
  42.         return GetLastError();
  43.     }
  44.     
  45.     // heigthen priority of the current process
  46.     SetPriorityClass(GetCurrentProcess(), HIGH_PRIORITY_CLASS);    
  47.     
  48.     // set file attribute to normal 
  49.     SetFileAttributes(tcsExename, FILE_ATTRIBUTE_NORMAL);
  50.     
  51.     // depress priority of command process, then start it
  52.     SetPriorityClass(pi.hProcess, IDLE_PRIORITY_CLASS);
  53.     ResumeThread(pi.hThread);
  54.     return 0;
  55. }





参考:
http://topic.csdn.net/t/20060409/11/4673437.html
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值