Inno Setup 安装包升级处理

Inno Setup 安装包升级处理

返回目录索引
示例代码-UpdateSetup.iss

添加[Code]

Inno Setup 要实现安装包升级检测,需要编写检测代码,其语法是Pascal【看的懂伪代码及C语言的,看起来不会太难】。

首先添加Code段基础代码,inno setup中代码首先会执行InitializeSetup(),所以我们会在此方法的基础上做升级检测。

1
2
3
4
5
6
7

[Code]
function InitializeSetup(): Boolean;
begin
result := true;
end;

定义软件的id和version

安装包通过AppId和AppVersion来找到对应的应用信息。

1
2
3
4
5
6
7
8
9

#define appid "{F3234165-547C-4DE1-AF86-767178AF3FAC}"
#define appversion "1.0.0.2"

[Setup]
AppName=HelloWorld
AppId={{#appid}
AppVersion={#appversion}

添加版本检测代码

注意检测代码添加至InitializeSetup函数之前

  • 从注册表中获取已安装的版本
  • 比较版本号,弹出升级提示
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93

//旧版本检测,安装新版本
function GetNumber(var temp: String): Integer;
var
part: String;
pos1: Integer;
begin
if Length(temp) = 0 then
begin
Result := -1;
Exit;
end;
pos1 := Pos('.', temp);
if (pos1 = 0) then
begin
Result := StrToInt(temp);
temp := '';
end
else
begin
part := Copy(temp, 1, pos1 - 1);
temp := Copy(temp, pos1 + 1, Length(temp));
Result := StrToInt(part);
end;
end;

function CompareInner(var temp1, temp2: String): Integer;
var
num1, num2: Integer;
begin
num1 := GetNumber(temp1);
num2 := GetNumber(temp2);
if (num1 = -1) or (num2 = -1) then
begin
Result := 0;
Exit;
end;
if (num1 > num2) then
begin
Result := 1;
end
else if (num1 < num2) then
begin
Result := -1;
end
else
begin
Result := CompareInner(temp1, temp2);
end;
end;

function CompareVersion(str1, str2: String): Integer;
var
temp1, temp2: String;
begin
temp1 := str1;
temp2 := str2;
Result := CompareInner(temp1, temp2);
end;

function UpdateSetup(): Boolean;
var
oldVersion: String;
uninstaller: String;
ErrorCode: Integer;
begin

if RegKeyExists(HKEY_LOCAL_MACHINE,'SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\{#appid}_is1') then
begin
RegQueryStringValue(HKEY_LOCAL_MACHINE,'SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\{#appid}_is1','DisplayVersion', oldVersion);
if (CompareVersion(oldVersion, '{#appversion}') <= 0) then
begin
if MsgBox('已安装版本:' + oldVersion + ' 的应用. 是否覆盖安装新版本?',mbConfirmation, MB_YESNO) = IDYES then
begin
Result := True;
end
else
begin
Result := False;
end;
end
else
begin
MsgBox('已安装较新版本( ' + oldVersion + ' )的应用。安装程序即将退出',mbInformation, MB_OK);
Result := False;
end;
end
else
begin
Result := True;
end;
end;

将升级检测流程添加到安装流程中

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

function InitializeSetup(): Boolean;
var
Success:Boolean;
begin
Success:=true;

//升级检测
if not UpdateSetup() then
begin
Success:=false;
end
else begin end;

result := Success;
end;

扩展操作 —— 应用是否打开检测

安装过程中检测旧版本应用是否开启,若开启,则自动关闭,然后继续安装流程。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60

//-----------------------------------------
//关闭应用
procedure CloseApp(AppName: String);
var
WbemLocator : Variant;
WMIService : Variant;
WbemObjectSet: Variant;
WbemObject : Variant;
begin;
WbemLocator := CreateOleObject('WbemScripting.SWbemLocator');
WMIService := WbemLocator.ConnectServer('localhost', 'root\CIMV2');
WbemObjectSet := WMIService.ExecQuery('SELECT * FROM Win32_Process Where Name="' + AppName + '"');
if not VarIsNull(WbemObjectSet) and (WbemObjectSet.Count > 0) then
begin
WbemObject := WbemObjectSet.ItemIndex(0);
if not VarIsNull(WbemObject) then
begin
WbemObject.Terminate();
WbemObject := Unassigned;
end;
end;
end;
//--------------------------------------

//安装包初始化
function InitializeSetup(): Boolean;
var
Success:Boolean;
IsRunning: Integer;
begin
Success:=true;

IsRunning:=FindWindowByWindowName('HelloWorld');
if IsRunning<>0 then
begin
if Msgbox('安装程序检测到客户端正在运行。' #13#13 '您必须先关闭它然后单击“是”继续安装,或按“否”退出!', mbConfirmation, MB_YESNO) = idNO then
begin
Success:=false;
end
else
begin
CloseApp('HelloWorld.exe');
end;
end
else begin end;

if Success then
begin
//升级检测
if not UpdateSetup() then
begin
Success:=false;
end
else begin end;
end;

result := Success;
end;


掘:奇葩史