Inno Setup 安装包Visual C++环境检测

Inno Setup 安装包Visual C++环境检测

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

将Visual C++安装包添加到安装包中

下列代码将VC_redist的x86安装包添加到安装包中,若需要其他安装包,请至 https://support.microsoft.com/zh-cn/help/2977003/the-latest-supported-visual-c-downloads 下载。

2015-2019版本见:https://visualstudio.microsoft.com/zh-hans/downloads/ 中的其他工具和框架

1
2
3
4
5

[Files]
Source: "HelloWorld.exe"; DestDir: "{app}";Components:main
Source:".\VC_redist.x86.exe";DestDir:"{tmp}";DestName:"vcredistx86.exe";Flags:ignoreversion

在[Code]段添加Visual C++环境检查代码

  • 添加Visual C++环境检查代码
    • 关于versionBuild值的来源,见下载的VC_redist.x86.exe文件的属性-详细信息-产品版本的第三级版本号
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

//vc++ 安装环境检查 plat:x64 x86
function IsVCPPDetected(version:string;plat:string): Boolean;
var
success: Boolean;
key,versionkey:string;
build,versionBuild:cardinal;
begin
versionkey:=version;
versionBuild:=0;

case version of
'13':
begin
versionkey:='12.0';
versionBuild:=40660;
end;

'15':
begin
versionkey:='14.0';
versionBuild:=23026;
end;
'19':
begin
versionkey:='14.0';
versionBuild:=28720;
end;

end;

key:='SOFTWARE\Wow6432Node\Microsoft\VisualStudio\'+versionkey+'\VC\Runtimes\'+plat;

success:=RegQueryDWordValue(HKLM, key, 'Bld', build);
success:=success and (build>=versionBuild);

result := success;
end;

  • 在初始化安装包时,检查C++环境
    • 下面示例中,检查Visual C++ 2019的32位x86版本是否安装【2015-2019现在是共享相同的可再发行软件文件】
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
         
//安装包初始化
function InitializeSetup(): Boolean;
var Path:string ;
ResultCode: Integer;
Success:Boolean;
begin
Success:=true;

if not IsVCPPDetected('19','x86') then
begin
MsgBox('软件运行需要 Microsoft Visual C++ 2019 Redistributable x86, 即将开始安装vc redist x86。', mbInformation, MB_OK);

ExtractTemporaryFile('vcredistx86.exe');
Path:=ExpandConstant('{tmp}\vcredistx86.exe')
Exec(Path, '', '', SW_SHOWNORMAL, ewWaitUntilTerminated, ResultCode);

if not IsVCPPDetected('19','x86') then
begin
if MsgBox('Microsoft Visual C++ 2019 Redistributable x86组件安装失败,部分功能无法使用,是否继续安装!',mbInformation,MB_YesNo) = IDYES then
begin
Success:=true;
end else begin
Success := false;
result:=Success;
Exit;
end;
end
else begin end;
end;

result := Success;
end;

其他检测方式:通过卸载注册表项检测

除上述 IsVCPPDetected 方式外,还可通过 HKLM\...\Uninstall 下的卸载注册表项(GUID)判断 Visual C++ 运行库是否安装。以下函数摘自社区脚本 vcredistversion.iss,支持 VC2013(13)、VC2015(15) 及 VC2015 Update3(15sp3) 的 Minimum/Additional Runtime。

  • Visual C++ x64 安装环境检查
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

//vc++ x64 安装环境检查
function NeedInstallVC9SP1(version:string;isequal:boolean): Boolean;
var
vc9SP1Missing: Boolean;
key,versionguid:string;
versionRelease,release:cardinal;
begin

case version of
//'9':versionguid:='{9A25302D-30C0-39D9-BD6F-21E6EC160475}';versionRelease:=
'13':
begin

end;

//Microsoft Visual C++ 2015 x64 Minimum Runtime
'15m':
'15':
begin
versionguid:='{0D3E9E15-DE7A-300B-96F1-B4AF12B96488}';
versionRelease:=234904050;
end;
//Microsoft Visual C++ 2015 x64 Additional Runtime
'15a':
begin
versionguid:='{BC958BD2-5DAC-3862-BB1A-C1BE0790438D}';
versionRelease:=234904050;
end;

//mu_visual_cpp_2015_redistributable_update_3_x64_9052538
//Microsoft Visual C++ 2015 x64 Minimum Runtime - 14.0.24212
'15sp3m':
'15sp3':
begin
versionguid:='{FAAD7243-0141-3987-AA2F-E56B20F80E41}';
versionRelease:=234905236;
end;
//Microsoft Visual C++ 2015 x64 Additional Runtime - 14.0.24212
'15sp3a':
begin
versionguid:='{F20396E5-D84E-3505-A7A8-7358F0155F6C}';
versionRelease:=234905236;
end;

end;

key:='SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\'+versionguid;

if isequal then begin
vc9SP1Missing:=RegQueryDWordValue(HKLM, key, 'Version', versionRelease);
end else begin
vc9SP1Missing:=RegQueryDWordValue(HKLM,key,'Version',release);
vc9SP1Missing:=vc9SP1Missing and (release>=versionRelease);
end;

result := vc9SP1Missing;
end;

  • Visual C++ x86 安装环境检查
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

//vc++ x86 安装环境检查
function NeedInstallVC9SP1x86(version:string;isequal:boolean): Boolean;
var
vc9SP1Missing: Boolean;
key,versionguid:string;
versionRelease,release:cardinal;
begin

case version of
//'9':versionguid:='{9A25302D-30C0-39D9-BD6F-21E6EC160475}';versionRelease:=
'13':
begin

end;

//Microsoft Visual C++ 2015 x86 Minimum Runtime - 14.0.23026
'15m':
'15':
begin
versionguid:='{A2563E55-3BEC-3828-8D67-E5E8B9E8B675}';
versionRelease:=234904050;
end;
//Microsoft Visual C++ 2015 x86 Additional Runtime - 14.0.23026
'15a':
begin
versionguid:='{BE960C1C-7BAD-3DE6-8B1A-2616FE532845}';
versionRelease:=234904050;
end;

end;

key:='SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\'+versionguid;


if isequal then begin
vc9SP1Missing:=RegQueryDWordValue(HKLM, key, 'Version', versionRelease);
end else begin
vc9SP1Missing:=RegQueryDWordValue(HKLM,key,'Version',release);
vc9SP1Missing:=vc9SP1Missing and (release>=versionRelease);
end;

result := vc9SP1Missing;
end;

注意事项

  • IsVCPPDetected方法的一个bug:当已经安装vc2015,然后安装vc2017,注册表没有相应的记录【应该是vc2017那个安装包有些问题】,安装完再安装一次vc2017,就解决了,但这个毕竟是个问题,可以考虑结合上述GUID检测方式
  • 静默安装:将Exec(Path, '', ...)改为Exec(Path, '/q', '', SW_SHOWNORMAL, ewWaitUntilTerminated, ResultCode);即可静默安装【/q —— 静默安装】,建议安装后再次调用检测函数确认是否成功

参考


解:奇葩史