如何自己删除自己

lovelymelon 2003-06-05 08:55:55
想做一个卸载程序,不知道怎样让程序在执行过程中将自己也删除掉。请各位帮帮忙
...全文
66 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
kuangning 2003-06-05
  • 打赏
  • 举报
回复
//转贴 来自 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;
blueshrimp 2003-06-05
  • 打赏
  • 举报
回复
用批处理吧,建一个临时批处理,退出时,执行这个批处理程序
hammer_shi 2003-06-05
  • 打赏
  • 举报
回复
有可能么?你能拿火柴将自己烧干净?
foilsman 2003-06-05
  • 打赏
  • 举报
回复
//转载,调用这个函数的程序在退出之后会自动删除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;
lovelymelon 2003-06-05
  • 打赏
  • 举报
回复
帮帮忙

5,388

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 开发及应用
社区管理员
  • VCL组件开发及应用社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧