程序如何把自己完全 删除掉
也就是说一个EXE文件 运行的时候把自己完全删除掉
也就是说一个EXE文件 运行的时候把自己完全删除掉
解决方案 »
- Delphi中有个工程为什么会一运行有错的地方就卡住, 要关掉整个delphi才行? 而其它工程就没问题
- QQ那样的栏目菜单怎么做的啊?
- 请问如何在combobox中显示所有的姓名?
- 搜索表结构的问题??急用??在线关注
- 请教,超市用的钱箱怎么样用程序控制它的开闭,在线急待
- message窗口没了,很痛苦!
- 什么是Delphi?
- 看过《深入Delphi网络编程》请进!在线等侯!
- 同志们! 怎么获的本机的IP?
- 如何实现oracle中记录号自动加1
- 讨论:当前不同规模企业信息管理系统开发架构主要分为哪几种?
- 高分求:DirectDraw+Delphi的实例,不需要DelphiX,实现对图片的平滑显示,有对surface的操作。分不够的话还可以再加~~~~~
uses
Windows, Dialogs, SysUtils, Controls;procedure DeleteMe(mDeleteDir: Boolean = False); { 自己删除自己 }
var
vExeDir: string; procedure pDelDir(mDirName: string); { 删除指定路径 }
var
vSearchRec: TSearchRec;
PathName: string;
K: Integer;
begin
PathName := mDirName + '\*.*';
K := FindFirst(PathName, faAnyFile, vSearchRec);
while K = 0 do begin
if (vSearchRec.Attr and faDirectory > 0) and
(Pos(vSearchRec.Name, '..') = 0) then begin
{$WARNINGS OFF}
FileSetAttr(vSearchRec.Name, faDirectory);
{$WARNINGS ON}
pDelDir(mDirName + '\' + vSearchRec.Name);
end else if (Pos(vSearchRec.Name, '..') = 0) and
(CompareText(mDirName + '\' + vSearchRec.Name, ParamStr(0)) <> 0) then begin
{$WARNINGS OFF}
FileSetAttr(vSearchRec.Name, 0);
{$WARNINGS ON}
DeleteFile(PChar(mDirName + '\' + vSearchRec.Name));
end;
K := FindNext(vSearchRec);
end;
if CompareText(vExeDir, mDirName) <> 0 then RmDir(mDirName);
end; { pDelDir }var
BatchFile: TextFile;
BatchFileName: TFileName;
ProcessInfo: TProcessInformation;
StartUpInfo: TStartupInfo;
begin
vExeDir := ExtractFileDir(ParamStr(0));
if mDeleteDir then pDelDir(vExeDir);
BatchFileName := '..\DeleteMe.bat';
AssignFile(BatchFile, BatchFileName);
Rewrite(BatchFile);
Writeln(BatchFile, ':del');
Writeln(BatchFile, 'del "' + ParamStr(0) + '"');
Writeln(BatchFile, 'if exist "' + ParamStr(0) + '"' + ' goto try');
if mDeleteDir then Writeln(BatchFile, 'rd ' + ExtractFileDir(ParamStr(0)));
Writeln(BatchFile, 'del %0');
CloseFile(BatchFile);
FillChar(StartUpInfo, SizeOf(StartUpInfo), #0);
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; { DeleteMe }
///////代码结束///////示例开始
begin
if MessageDlg('警告执行此程序将删除所在目录的所有文件及目录',
mtWarning, [mbYes, mbNo], 0) = mrYes then
DeleteMe(True);
end.
///////示例结束
varmodule : HMODULE;buf : array[0..MAX_PATH - 1] of char;p : ULONG;hKrnl32 : HMODULE;pExitProcess, pDeleteFile, pFreeLibrary: pointer;beginmodule := 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');asmlea eax, bufpush 0push 0push eaxpush pExitProcesspush ppush pDeleteFilepush pFreeLibraryretend;end;把DeleteSelf代码放在DPR文件的最后一句即可,程序关闭之后,就会自动删除文件!此外,如果要任何时候都删除Exe的话,可以配合ExitProc&TerminateProc全局变量和AddExitProc&AddTerminateProc来做到这一点!!这个过程的优点在于不需要一个临时文件,保密程度和技术方面都比较强的。当然,你也可以利用一个临时的批处理文件来达到目的!procedure DeleteMe;varBatchFile: TextFile;BatchFileName: string;ProcessInfo: TProcessInformation;StartUpInfo: TStartupInfo;beginBatchFileName := 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) thenbeginCloseHandle(ProcessInfo.hThread);CloseHandle(ProcessInfo.hProcess);end;end;
这是超级猛料里的,我在2000下用没有一点问题