innosetup 卸载前的准备工作

本文详细介绍了InnoSetup在卸载程序时如何利用卸载事件函数如InitializeUninstall()和InitializeUninstallProgressForm()进行必要的清理工作。通过设置全局变量和使用ShellExec函数,可以在卸载前执行特定操作,例如调用反注册程序或执行自定义脚本。同时,文章提及了[UninstallRun]段的使用,但指出它在卸载过程已经开始后才执行。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

卸载程序时,我们希望在卸载前做一些清理善后操作。

 

卸载事件函数

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;

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值