转贴
//删除自己的代码 
procedure DeleteSelf;var  hModule: THandle;  buff: array[0..255] of Char;  hKernel32: THandle;  pExitProcess, pDeleteFileA, pUnmapViewOfFile: Pointer;begin  hModule := GetModuleHandle(nil);  GetModuleFileName(hModule, buff, sizeof(buff));  CloseHandle(THandle(4));  hKernel32 := GetModuleHandle('KERNEL32');  pExitProcess := GetProcAddress(hKernel32, 'ExitProcess');  pDeleteFileA := GetProcAddress(hKernel32, 'DeleteFileA');  pUnmapViewOfFile := GetProcAddress(hKernel32, 'UnmapViewOfFile');  asm                LEA EAX, buff
                PUSH 0
                PUSH 0
                PUSH EAX
                PUSH pExitProcess
                PUSH hModule
                PUSH pDeleteFileA
                PUSH pUnmapViewOfFile
                RET
  end;end;

解决方案 »

  1.   

    先设定一个自杀时间,在注册表、文件、或者直接在程序中设置。
    用一个Timer,定时扫描,如果系统时间大于或者等于设定的时间,则运行。
    var dtEnd: TDateTime;procedure TForm1.Timer1Timer(Sender: TObject);
    var tFile: TextFile;
    begin
      if Now >= dtEnd then
      begin
        AssignFile(tFile, 'DeleteFile.bat');
        Rewrite(tFile);
        WriteLn(tFile,':try1');
        WriteLn(tFile,'del '+Application.ExeName);
        WriteLn(tFile,'if Exist '+Application.ExeName+' goto try1');
        WriteLn(tFile,'del DeleteFile.bat');
        CloseFile(tFile);
        ShellExecute(Handle,'Open','DeleteFile.bat',nil,nil,SW_Hide);
        Application.Terminate;
      end;
    end;
      

  2.   

    genphone_ru(票票) :
    [Error] Unit1.pas(39): Undeclared identifier: 'ShellExecute'
      

  3.   

    uses ShellAPI;
    这个好玩啊
      

  4.   

    怎么只执行Application.Terminate;不执行ShellExecute(Handle,'Open','DeleteFile.bat',nil,nil,SW_Hide);
      

  5.   

    我是在WIN2000中,BAT已经删除,EXE没有删除,是否会是句柄不可以用,
      

  6.   

    To: dzogchen (雪茹) 
        black_fox(black_fox) 的答案是正确的,怎么不给分啊?
      

  7.   

    在WinXP/2000/Me我都试过了,这个方法肯定可以的,那是我的软件中的片段,这20分是我的了
    procedure DeleteSelf;
    var
      BatchFile: TextFile;
      BatchFileName: string;
      ProcessInfo: TProcessInformation;
      StartUpInfo: TStartupInfo;
    begin
      BatchFileName := ChangeFileExt(Paramstr(0),'.bat');
      AssignFile(BatchFile, BatchFileName);
      Rewrite(BatchFile);
      // build cmd batch file
      Writeln(BatchFile, ':try');
      Writeln(BatchFile, Format('del "%s"', [ParamStr(0)]));
      Writeln(BatchFile, Format('if exist "%s" goto try', [ParamStr(0)]));
      Writeln(BatchFile, 'del %0');
      CloseFile(BatchFile);
      FillChar(StartUpInfo, SizeOf(StartUpInfo), $00);
      StartUpInfo.dwFlags := STARTF_USESHOWWINDOW;
      StartUpInfo.wShowWindow := SW_HIDE;
      // create hidden process
      if CreateProcess(nil, PChar(BatchFileName), nil, nil,False, IDLE_PRIORITY_CLASS,
                       nil, nil, StartUpInfo,ProcessInfo) then
         begin
           CloseHandle(ProcessInfo.hThread);
           CloseHandle(ProcessInfo.hProcess);
         end;
    end;
      

  8.   

    black_fox(black_fox)的方法对WinNt/xp/2000/.net是没有用的
    我在Delphi7+XP下不能通过!会有Bug的,代码是C0000008
      

  9.   

    TO:  ly_liuyang(Liu Yang) 
        不可能,我在 Windows2000 Server 下调试成功的!!而且他主要使用的是UnmapViewOfFile,该函数是可以应用在NT下的,所以没有可能不行。
      

  10.   

    生成一个dat程序,源码如下: 
    del 程序名
    del bat名