卸载程序时,我们希望在卸载前做一些清理善后操作。
卸载事件函数
innosetup为我们提供了,卸载事件函数,卸载支持以下事件函数:
InitializeUninstall()
function InitializeUninstall(): Boolean;
Return False to abort Uninstall, True otherwise.
返回False中止卸载,返回True继续卸载。
InitializeUninstallProgressForm()
procedure InitializeUninstallProgressForm();
Use this event function to make changes to the progress form at startup. You can't use the InitializeUninstall event function for this since at the time it is triggered, the progress form does not yet exist.
使用此事件函数可在启动时更改进度窗体。不能为此使用InitializeUninstall事件函数,因为在触发该事件时,进度窗体尚不存在。
procedure DeinitializeUninstall();
取消卸载的函数
function UninstallNeedRestart(): Boolean;
Return True to instruct Uninstall to prompt the user to restart the system at the end of a successful uninstallation, False otherwise.
返回True,则在成功卸载后,提示用户重新启动系统。否则返回False。
可以做的是在[Code]部分添加一个全局变量
[Code]
var
ApplicationWasUninstalled: Boolean;
在InitializeUninstallProgressForm过程中,可以将全局变量设置为1(注意:仅当用户在系统提示是否要卸载您的应用程序时单击“是”时才会执行此功能
procedure InitializeUninstallProgressForm();
begin
ApplicationWasUninstalled := true;
end;
继续,您将检查DeinitializeUninstall函数中ApplicationWasUninstalled的值
procedure DeinitializeUninstall();
begin
if ApplicationWasUninstalled Then Begin
//your code here
end;
end;
[UninstallRun]段
在UninstallRun段内调用程序exe,在里面执行清理工作也是一种办法,但是这个时机是在已经开始卸载后的开始里发生,比InitializeUninstall晚。
[UninstallRun]
卸载时运行反注册程序
Filename: "{app}\{#MyAppExeName}"; Parameters: "--uninstall"; WorkingDir: "{app}"; Flags: waituntilterminated skipifdoesntexist
;#endif
InitializeUninstall函数执行在卸载之前,可以取消卸载。如果要在卸载前判断是不是继续执行卸载,需要在InitializeUninstall内操作。
在InitializeUninstall内部 使用 ShellExec执行相应操作的exe程序
Inno Setup 的Shellexec的功能类似乎Windows API 的ShellExecute函数,执行某种操作,可以是开启一个进程,可以打开一个浏览器等等。
ShellExec
原型:
function ShellExec(const Verb, Filename, Params, WorkingDir: String; const ShowCmd: Integer; const Wait: TExecWait; var ErrorCode: Integer): Boolean;
function ShellExec(
const Verb,
Filename,
Params,
WorkingDir: String;
const ShowCmd: Integer;
const Wait: TExecWait;
var ErrorCode: Integer): Boolean;
说明:
使用与安装/卸载相同的权限凭据,打开指定的文件或执行Verb指定的另一个操作。文件名可以是可执行文件、文档文件、文件夹或URL。Verb可以是空字符串,在这种情况下,使用文件类型的默认谓词(通常为“open”,'runas')。
第二个参数,Filename 就是表示进程名,包含路径,
第三个参数表示命令行参数,
第四个参数是工作路径,
第五个参数表示是否显示
第六个参数是 Inno Setup 的Shellexec 的优点。
第六个参数 TExecWait定义为:
TExecWait=(ewNoWait,ewwaituntilteredated,ewWaitUntilIdle);
Wait参数指定函数是立即返回,还是等到启动的进程终止或空闲。如果成功打开指定的文件,则返回True,否则返回False。如果返回False,则ErrorCode指定发生的错误。使用SysErrorMessage(ErrorCode)获取错误的描述。
请注意,如果没有生成新进程(例如,如果文件是在处理该文件类型的程序已在运行的实例中打开),则传递除ewNoWait以外的等待值将无效。
如果使用ewNoWait,那么ShellExec开启进程以后立即返回。
如果使用ewWaitUntilTerminated,则ShellExec开启进程以后阻塞,知道被开启的进程终止以后才返回。
如果使用ewWaitUntilIdle,则ShellExec在CPU空闲的时候返回。
其中ewWaitUntilTerminated很有用,这样可以在一个安装程序中随意的控制执行其他的操作。
第七个参数是 程序的退出码。
例子:
var
ErrorCode: Integer;
begin
if not ShellExec('', ExpandConstant('{app}\filename.rtf'), '', '', SW_SHOW, ewNoWait, ErrorCode) then
begin
// handle failure if necessary
end;
end;
var
ResultCode: Integer;
begin
// Launch Notepad and wait for it to terminate
if Exec(ExpandConstant('{win}\notepad.exe'), '', '', SW_SHOW, ewWaitUntilTerminated, ResultCode) then
begin
// handle success if necessary; ResultCode contains the exit code
end
else begin
// handle failure if necessary; ResultCode contains the error code
end;
end;