想做一个卸载程序,不知道怎样让程序在执行过程中将自己也删除掉。请各位帮帮忙

解决方案 »

  1.   

    //转载,调用这个函数的程序在退出之后会自动删除Exe文件
    procedure DeleteMe;
    var
        BatchFile         : TextFile;
        BatchFileName     : String;
        ProcessInfo       : TProcessInformation;
        StartUpInfo       : TStartupInfo;
    begin
        BatchFileName := ChangeFileExt(ParamStr(0), '.bat');    AssignFile(BatchFile, BatchFileName);
        Rewrite(BatchFile);    Writeln(BatchFile, ':try');
        Writeln(BatchFile, 'del "' + ParamStr(0) + '"');
        Writeln(BatchFile, 'if exist "' + ParamStr(0) + '"' + ' goto try');
        Writeln(BatchFile, 'del %0');
        CloseFile(BatchFile);    FillChar(StartUpInfo, SizeOf(StartUpInfo), $00);
        StartUpInfo.dwFlags := STARTF_USESHOWWINDOW;
        StartUpInfo.wShowWindow := SW_HIDE;    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;
      

  2.   

    //转贴 来自 delphi猛料
    procedure DeleteSelf;
    var
      module            : HMODULE;
      buf               : array[0..MAX_PATH - 1] of char;
      p                 : ULONG;
      hKrnl32           : HMODULE;
      pExitProcess, pDeleteFile, pFreeLibrary: pointer;
    begin
      module := GetModuleHandle(nil);
      GetModuleFileName(module, buf, sizeof(buf));  CloseHandle(THandle(4));  p := ULONG(module) + 1;  hKrnl32 := GetModuleHandle('kernel32');
      pExitProcess := GetProcAddress(hKrnl32, 'ExitProcess');
      pDeleteFile := GetProcAddress(hKrnl32, 'DeleteFileA');
      pFreeLibrary := GetProcAddress(hKrnl32, 'FreeLibrary');  asm
        lea eax, buf
        push 0
        push 0
        push eax
        push pExitProcess
        push p
        push pDeleteFile
        push pFreeLibrary
        ret
      end;
    end;
    把DeleteSelf代码放在DPR文件的最后一句即可,程序关闭之后,就会自动删除文件!此外,如果要任何时候都删除Exe的话,可以配合ExitProc&TerminateProc全局变量和AddExitProc&AddTerminateProc来做到这一点!!这个过程的优点在于不需要一个临时文件,保密程度和技术方面都比较强的。当然,你也可以利用一个临时的批处理文件来达到目的!
    procedure DeleteMe;
    var
      BatchFile: TextFile;
      BatchFileName: string;
      ProcessInfo: TProcessInformation;
      StartUpInfo: TStartupInfo;
    begin
      BatchFileName := ExtractFilePath(ParamStr(0)) + '_deleteme.bat';  AssignFile(BatchFile, BatchFileName);
      Rewrite(BatchFile);  Writeln(BatchFile, ':try');
      Writeln(BatchFile, 'del "' + ParamStr(0) + '"');
      Writeln(BatchFile,
        'if exist "' + ParamStr(0) + '"' + ' goto try');
      Writeln(BatchFile, 'del %0');
      CloseFile(BatchFile);  FillChar(StartUpInfo, SizeOf(StartUpInfo), $00);
      StartUpInfo.dwFlags := STARTF_USESHOWWINDOW;
      StartUpInfo.wShowWindow := SW_HIDE;  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;