想做一个卸载程序,不知道怎样让程序在执行过程中将自己也删除掉。请各位帮帮忙
解决方案 »
- 用installshield2010做的安装包,如何在安装完后,自动打开网站?
- 水晶報表
- 用delphi存取图像的问题
- 我想用保形数组参数求一下两个矩阵的和与积,只不过程序无法运行,劳驾大虾们指点迷津……
- 请问,除了MediaPlayer控件外,有什么其他方式直接控制光驱的弹出和关闭
- 报表打印,急!!!在线等待!!!
- 请教SQL查询的问题
- 如何控制本机程序的运行!
- 程序员兄弟,从昨天早上醒来,到昨晚睡觉.你一天都做了什么?有兴趣的回忆一下
- 高手请进,其他勿入!解决了给200分!
- 如何发布基于SQL SERVER 2000的程序?
- 希望大家帮帮忙:insertdata parameter ':fanghao_p' not found 什么意思
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;
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;