代码改变世界

Inno Setup制作安装包的几个问题

2013-06-20 22:23  鉴于  阅读(6696)  评论(1编辑  收藏  举报

系统开发好之后,通常需要制作成安装包,才能卖给用户。利用Inno Setup的向导可以制作简单的安装包,但是如果要做个好的安装包的话可能会遇到一些麻烦,今日终于抽空解决了,Inno Setup打包的一些问题。具体如下:

1. 卸载时,如何判断应用程序是否运行
   InnoSetup 提供变量AppMutex,用来保存应用程序的Mutex名称。现在很多应用程序都是唯一实例运行。这样避免配置文件被错误修改以及其他很多衍生问题。通常都会用WindowsAPI CreateMuex来创建一个Mutex;安装包卸载时会判断AppMutex是否已经被占用。如果被占用则等待并提示用户关闭应用程序。如果应用程序正在运行,通常该exe文件和被使用的dll是不会被删除的,卸载不完全。
在Inno Setup Compile 配置中,将MyProgramsMutexName 定义为 "ityujian-A75DEC53-783F-4425-8431-24D83BD4CE5F"    该字符串需要在被打包的EXE文件中使用。
#define MyProgramsMutexName "ityujian-A75DEC53-783F-4425-8431-24D83BD4CE5F"
[Setup]
AppMutex={#MyProgramsMutexName}

2. 如何提示用户是否删除配置文件
  Inno Setup提供Script Language支持,可以在[Code] 章节里使用Pascal Script编写函数 并且Inno Setup提供事件响应。在卸载时,该函数会被调用

procedure CurUninstallStepChanged(CurUninstallStep: TUninstallStep);
begin
  if CurUninstallStep = usUninstall then begin
    if MsgBox('Do you want to delete all config files and log files?', mbConfirmation, MB_YESNO) = IDYES
    then  DelFiles();
  end;
end;

这是Inno Setup提供的卸载事件函数,  if CurUninstallStep = usUninstall  判断是否正在卸载,
如果正在卸载则需判断是否删除配置文件,我开发的应用程序使用ini文件存储配置。MsgBox('Do you want to delete all config files and log files?', mbConfirmation, MB_YESNO) 用于弹出消息框,提示用户选择是否删除文件。
函数DelFiles() 为自定义函数,用来删除文件和目录。
ExpandConstant用于获取变量值,FileExists用于判断文件是否存在;DeleteFile用来删除文件;
DirExists 用于检查目录是否存在;DelTree用于删除目录和里面的文件

procedure DelFiles();
begin
  if FileExists(ExpandConstant('{app}') + '\config.ini')  then  DeleteFile(ExpandConstant('{app}') + '\config.ini');
  if FileExists(ExpandConstant('{app}') + '\log.txt')     then DeleteFile(ExpandConstant('{app}') + '\log.txt');
  if DirExists(ExpandConstant('{app}') + '\Users\')       then DelTree(ExpandConstant('{app}') + '\Users\', true, true, true);
end;

Inno Setup 编译配置文件如下,供参考:

; Script generated by the Inno Setup Script Wizard.
; SEE THE DOCUMENTATION FOR DETAILS ON CREATING INNO SETUP SCRIPT FILES!

#define MyAppName "ityujian"
;应用程序名
#define MyAppVersion "1.0"
;版本号
#define MyAppPublisher "Jianyu Studio"
;发布者名称
#define MyAppURL "
http://www.cnblogs.com/ityujian/"
;公司的网站
#define MyAppExeName "ityujian.exe"
;exe文件名
#define MyProgramsMutexName "ityujian-A75DEC53-783F-4425-8431-24D83BD4CE5F"
;应用程序中使用的Mutex,卸载时用来判断应用程序是否正在执行

[Setup]
; NOTE: The value of AppId uniquely identifies this application.
; Do not use the same AppId value in installers for other applications.
; (To generate a new GUID, click Tools | Generate GUID inside the IDE.)
AppId={{ECCF74CD-48BA-4000-988B-18E965893BD6}
AppName={#MyAppName}
AppVersion={#MyAppVersion}
;AppVerName={#MyAppName} {#MyAppVersion}
AppPublisher={#MyAppPublisher}
AppPublisherURL={#MyAppURL}
AppSupportURL={#MyAppURL}
AppUpdatesURL={#MyAppURL}
DefaultDirName={pf}\{#MyAppName}
DefaultGroupName={#MyAppName}
AllowNoIcons=yes
OutputBaseFilename=setup
SetupIconFile=.\ityujian.ico
;SetupIconFile 是打包之后exe的图标文件
Compression=lzma
SolidCompression=yes
AppMutex={#MyProgramsMutexName}
;AppMutex是需要打包的应用程序使用的Mutex,卸载之前会先判断该AppMutex是否已经被占用,如果被占用则提示应用程序正在运行,请关闭

[Languages]
Name: "english"; MessagesFile: "compiler:Default.isl"

[Tasks]
Name: "desktopicon"; Description: "{cm:CreateDesktopIcon}"; GroupDescription: "{cm:AdditionalIcons}"; Flags: unchecked
Name: "quicklaunchicon"; Description: "{cm:CreateQuickLaunchIcon}"; GroupDescription: "{cm:AdditionalIcons}"; Flags: unchecked; OnlyBelowVersion: 0,6.1

[Files]
Source: "GdiPlus.dll"; DestDir: "{app}"; Flags: ignoreversion
Source: "ityujian.exe"; DestDir: "{app}"; Flags: ignoreversion
;Source 描述需要打包的EXE文件位置,DestDir描述安装时该EXE文件的相对位置
Source: "ityujian.pdb"; DestDir: "{app}"; Flags: ignoreversion
Source: "Users\*"; DestDir: "{app}\Users\"; Flags: ignoreversion recursesubdirs createallsubdirs  uninsneveruninstall
;上句用于将Users目录打进安装包;uninsneveruninstall说明卸载时,不删除该文件夹
Source: "Doc\*"; DestDir: "{app}\Doc\"; Flags: ignoreversion recursesubdirs createallsubdirs
;用户将Doc目录下的用户手册打进安装包
; NOTE: Don't use "Flags: ignoreversion" on any shared system files

[Icons]
Name: "{group}\{#MyAppName}"; Filename: "{app}\{#MyAppExeName}"
Name: "{group}\{cm:ProgramOnTheWeb,{#MyAppName}}"; Filename: "{#MyAppURL}"
Name: "{group}\{cm:UninstallProgram,{#MyAppName}}"; Filename: "{uninstallexe}"
Name: "{commondesktop}\{#MyAppName}"; Filename: "{app}\{#MyAppExeName}"; Tasks: desktopicon
Name: "{userappdata}\Microsoft\Internet Explorer\Quick Launch\{#MyAppName}"; Filename: "{app}\{#MyAppExeName}"; Tasks: quicklaunchicon

[UninstallDelete]
Type: dirifempty; Name: "{app}\"
;Type有多种;Type: dirifempty表示如果安装本应用程序的目录不为空,则不删除安装目录

[Code]
procedure DelFiles();
begin
  if FileExists(ExpandConstant('{app}') + '\config.ini')  then  DeleteFile(ExpandConstant('{app}') + '\config.ini');
  if FileExists(ExpandConstant('{app}') + '\log.txt')     then DeleteFile(ExpandConstant('{app}') + '\log.txt');
  if DirExists(ExpandConstant('{app}') + '\Users\')       then DelTree(ExpandConstant('{app}') + '\Users\', true, true, true);
end;

procedure CurUninstallStepChanged(CurUninstallStep: TUninstallStep);
begin
  if CurUninstallStep = usUninstall then begin
    if MsgBox('Do you want to delete all config files and log files?', mbConfirmation, MB_YESNO) = IDYES
    then  DelFiles();
  end;
end;

安装之后截图:
image

工程 https://files.cnblogs.com/ityujian/ityujian.zip
Inno Setup 去http://www.jrsoftware.org/isinfo.php 下载