Inno Setup检测Net和VC是否安装

常见环境

从 Visual Studio .NET 到 Visual Studio 2013,C++ 编译器和工具的每个主版本都包含一个新的独立版本的 Microsoft C 运行 (CRT) 库。

CRT 的这些独立版本彼此独立,并在不同程度上彼此不兼容。

例如,Visual Studio 2012 使用的 CRT 库是第 11 版,名为 msvcr110.dll,而 Visual Studio 2013 使用的 CRT 是第 12 版,名为 msvcr120.dll。

从 Visual Studio 2015 开始,不再是这样。 Visual Studio 2015 及更高版本的 Visual Studio 都使用一个通用 CRT。

通用 CRT (UCRT) 是 Microsoft Windows 操作系统组件。 它包含在 Windows 10 和 Windows Server 2016 或更高版本中作为操作系统的一部分。

对于仍处于扩展支持的较早版本的操作系统,可使用 Windows 更新来提供 UCRT。 支持通用 CRT 本地部署,但有一些限制。

C++下载

最新支持的 Visual C++ 下载

32位下载地址:https://aka.ms/vs/16/release/vc_redist.x86.exe

.NET Framework下载

https://dotnet.microsoft.com/download/visual-studio-sdks

.NET Framework 4.5.2离线安装包:https://www.microsoft.com/en-us/download/details.aspx?id=42642

本地安装

运行库下载地址

链接:https://pan.baidu.com/s/1qSF3i3Ny4GddNvg7U_8EbQ
提取码:psvm

注意

这里更推荐使用本地安装的方式,虽然安装包大了12M,但是用户的安装体验会好一点。

检测C++环境

下载文件D:\Tools\runtime目录下

加载文件

1
2
[Files]
Source: "D:\Tools\runtime\vc_redist.x86.exe"; DestDir: "{app}\runtime"; Check: NeedInstallVC

运行时安装

1
2
[Run]
Filename: "{app}\runtime\vc_redist.x86.exe"; Parameters: /q; WorkingDir: {app}; Flags: skipifdoesntexist; StatusMsg: "Install Microsoft Visual C++ Runtime ..."; Check: NeedInstallVC

如果不使用静默安装

1
2
[Run]
Filename: "{app}\runtime\vc_redist.x86.exe"; WorkingDir: {app}; Flags: skipifdoesntexist; StatusMsg: "Install Microsoft Visual C++ Runtime ..."; Check: NeedInstallVC

检测是否需要安装

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
[Code]
function NeedInstallVC(): Boolean;
begin
if RegValueExists(HKLM, 'SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\{01FAEC41-B3BC-44F4-B185-5E8475AEB855}', 'Version')
or RegValueExists(HKLM, 'SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\{77EB1EA9-8E1B-459D-8CDC-1984D0FF15B6}', 'Version')
or RegValueExists(HKLM, 'SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\{5720EC03-F26F-40B7-980C-50B5D420B5DE}', 'Version')
then
begin
Result := false;
end
else
begin
Result := true;
end;
Result := true;
end;

检测运行库是否安装是通过注册表进行检测的,其中最后一段字符是运行库的产品ID,每个版本的产品ID都不一样
解压下载后的vc_redist.x86.exe文件,找到名称为0的文件使用文本文档打开,

搜索ProductCode会搜索出两个,其中一个是Minimum Runtime,一个是Additional Runtime

1
RollbackLogPathVariable="WixBundleRollbackLog_vcRuntimeMinimum_x86" ProductCode="{7D75664A-6C04-424C-82A1-EE88913E5F16}"

1
RollbackLogPathVariable="WixBundleRollbackLog_vcRuntimeAdditional_x86" ProductCode="{77EB1EA9-8E1B-459D-8CDC-1984D0FF15B6}"

我们用到的是WixBundleRollbackLog_vcRuntimeAdditional_x86,在注册表中搜索对应产品ID即可

检测.Net Framework环境

下载文件D:\Tools\runtime目录下

加载文件

1
2
[Files]
Source: "D:\Tools\runtime\NDP452-KB2901954-Web.exe"; DestDir: "{app}\runtime";

运行时安装

1
2
[Run]
Filename: "{app}\runtime\NDP452-KB2901954-Web.exe"; WorkingDir: {app}; Flags: skipifdoesntexist; StatusMsg: "Install .Net Framework ..."; Check: NeedInstallDotNet

注意

这里要从网络上下载文件不使用静默安装

检测是否需要安装

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
function IsInstallDotNet(version: string; service: cardinal): boolean;
var
key, versionKey: string;
install, release, serviceCount, versionRelease: cardinal;
success: boolean;
begin
versionKey := version;
versionRelease := 0;

// .NET 1.1 and 2.0 embed release number in version key
if version = 'v1.1' then begin
versionKey := 'v1.1.4322';
end
else if version = 'v2.0' then begin
versionKey := 'v2.0.50727';
end

// .NET 4.5 and newer install as update to .NET 4.0 Full
else if Pos('v4.', version) = 1 then begin
versionKey := 'v4\Full';
case version of
'v4.5': versionRelease := 378389;
'v4.5.1': versionRelease := 378675; // 378758 on Windows 8 and older
'v4.5.2': versionRelease := 379893;
'v4.6': versionRelease := 393295; // 393297 on Windows 8.1 and older
'v4.6.1': versionRelease := 394254; // 394271 on Windows 8.1 and older
'v4.6.2': versionRelease := 394802; // 394806 on Windows 8.1 and older
'v4.7': versionRelease := 460798; // Windows 10
'v4.7.1': versionRelease := 461308; // Windows 10
'v4.7.2': versionRelease := 461808; // Windows 10
'v4.8' : versionRelease := 528040; // Windows 10
end;
end;

// installation key group for all .NET versions
key := 'SOFTWARE\Microsoft\NET Framework Setup\NDP\' + versionKey;

// .NET 3.0 uses value InstallSuccess in subkey Setup
if Pos('v3.0', version) = 1 then begin
success := RegQueryDWordValue(HKLM, key + '\Setup', 'InstallSuccess', install);
end else begin
success := RegQueryDWordValue(HKLM, key, 'Install', install);
end;

// .NET 4.0 and newer use value Servicing instead of SP
if Pos('v4', version) = 1 then begin
success := success and RegQueryDWordValue(HKLM, key, 'Servicing', serviceCount);
end else begin
success := success and RegQueryDWordValue(HKLM, key, 'SP', serviceCount);
end;

// .NET 4.5 and newer use additional value Release
if versionRelease > 0 then begin
success := success and RegQueryDWordValue(HKLM, key, 'Release', release);
success := success and (release >= versionRelease);
end;

result := success and (install = 1) and (serviceCount >= service);
end;

// 检查.Net是否安装
function NeedInstallDotNet : Boolean;
begin
if IsInstallDotNet('v4.5.2', 0) then
begin
Result := false;
end
else
begin
Result := true;
end
end;

检测C++和.Net Framework环境

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
94
95
96
97
98
[Files]
Source: "D:\Tools\runtime\vc_redist.x86.exe"; DestDir: "{app}\runtime";
Source: "D:\Tools\runtime\NDP452-KB2901954-Web.exe"; DestDir: "{app}\runtime";

[Run]
Filename: "{app}\runtime\vc_redist.x86.exe"; Parameters: /q; WorkingDir: {app}; Flags: skipifdoesntexist; StatusMsg: "Install Microsoft Visual C++ Runtime ..."; Check: NeedInstallVC
Filename: "{app}\runtime\NDP452-KB2901954-Web.exe"; WorkingDir: {app}; Flags: skipifdoesntexist; StatusMsg: "Install .Net Framework ..."; Check: NeedInstallDotNet

[Code]
function NeedInstallVC(): Boolean;
begin
// 这里,不同版本运行环境对应的GUID不同
if RegValueExists(HKLM, 'SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\{01FAEC41-B3BC-44F4-B185-5E8475AEB855}', 'Version')
or RegValueExists(HKLM, 'SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\{77EB1EA9-8E1B-459D-8CDC-1984D0FF15B6}', 'Version')
or RegValueExists(HKLM, 'SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\{5720EC03-F26F-40B7-980C-50B5D420B5DE}', 'Version')
then
begin
Result := false;
end
else
begin
Result := true;
end;
Result := true;
end;

function IsInstallDotNet(version: string; service: cardinal): boolean;
var
key, versionKey: string;
install, release, serviceCount, versionRelease: cardinal;
success: boolean;
begin
versionKey := version;
versionRelease := 0;

// .NET 1.1 and 2.0 embed release number in version key
if version = 'v1.1' then begin
versionKey := 'v1.1.4322';
end
else if version = 'v2.0' then begin
versionKey := 'v2.0.50727';
end

// .NET 4.5 and newer install as update to .NET 4.0 Full
else if Pos('v4.', version) = 1 then begin
versionKey := 'v4\Full';
case version of
'v4.5': versionRelease := 378389;
'v4.5.1': versionRelease := 378675; // 378758 on Windows 8 and older
'v4.5.2': versionRelease := 379893;
'v4.6': versionRelease := 393295; // 393297 on Windows 8.1 and older
'v4.6.1': versionRelease := 394254; // 394271 on Windows 8.1 and older
'v4.6.2': versionRelease := 394802; // 394806 on Windows 8.1 and older
'v4.7': versionRelease := 460798; // Windows 10
'v4.7.1': versionRelease := 461308; // Windows 10
'v4.7.2': versionRelease := 461808; // Windows 10
'v4.8' : versionRelease := 528040; // Windows 10
end;
end;

// installation key group for all .NET versions
key := 'SOFTWARE\Microsoft\NET Framework Setup\NDP\' + versionKey;

// .NET 3.0 uses value InstallSuccess in subkey Setup
if Pos('v3.0', version) = 1 then begin
success := RegQueryDWordValue(HKLM, key + '\Setup', 'InstallSuccess', install);
end else begin
success := RegQueryDWordValue(HKLM, key, 'Install', install);
end;

// .NET 4.0 and newer use value Servicing instead of SP
if Pos('v4', version) = 1 then begin
success := success and RegQueryDWordValue(HKLM, key, 'Servicing', serviceCount);
end else begin
success := success and RegQueryDWordValue(HKLM, key, 'SP', serviceCount);
end;

// .NET 4.5 and newer use additional value Release
if versionRelease > 0 then begin
success := success and RegQueryDWordValue(HKLM, key, 'Release', release);
success := success and (release >= versionRelease);
end;

result := success and (install = 1) and (serviceCount >= service);
end;

// 检查.Net是否安装
function NeedInstallDotNet : Boolean;
begin
if IsInstallDotNet('v4.5.2', 0) then
begin
Result := false;
end
else
begin
Result := true;
end
end;

在线下载

检测C++安装环境

这两种方式各有利弊,推荐方式1。

  • 方式1 如果用户有更新的版本,会检测不存在,新版本存在的时候旧版本又无法安装,但是它比较准确。
  • 方式2 不存在方式1的问题 但是如果用户安装后又卸载了VC环境的话,这种方式检测VC依旧存在,但实际已经不可用了。

方式1(推荐)

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
[Code]   
function CheckVC():boolean;
var Path:string;
ResultCode: Integer;
begin
if RegValueExists(HKLM, 'SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\{01FAEC41-B3BC-44F4-B185-5E8475AEB855}', 'Version')
or RegValueExists(HKLM, 'SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\{77EB1EA9-8E1B-459D-8CDC-1984D0FF15B6}', 'Version')
or RegValueExists(HKLM, 'SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\{5720EC03-F26F-40B7-980C-50B5D420B5DE}', 'Version')
then
begin
Result := true;
end
else
begin
if MsgBox('系统检测到您没有安装VC++环境,是否立刻下载并安装?', mbConfirmation, MB_YESNO) = idYes then
begin
Path := ExpandConstant('{pf}/Internet Explorer/iexplore.exe');
Exec(Path, 'https://xhkjedu.oss-cn-huhehaote.aliyuncs.com/runtime/vc_redist.x86.exe', '', SW_SHOWNORMAL, ewWaitUntilTerminated, ResultCode);
MsgBox('请安装好VC++环境后,再运行本安装包程序!',mbInformation,MB_OK);
Result := false;
end
else
begin
MsgBox('取消后请手动安装VC2015-2019环境!',mbInformation,MB_OK);
Result := true;
end;
end;
end;

function InitializeSetup: Boolean;
begin
Result := CheckVC();
end;

注意:

这一种方式能够保证VC环境一定没问题,但是如果电脑上本身已安装其他相同大版本的其他版本就会导致安装失败!

微软的不同版本的VC包的产品编号都是不一样的,所以这里为了保证安装旧版的VC的用户不再提示安装新版,所以添加了判断。

推荐使用这种方式。

方式2

另外一种方法

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
[Code]   
//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:=30037;
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;

function CheckVC: Boolean;
var Path:string;
ResultCode: Integer;
begin
if IsVCPPDetected('19','x86') then
begin
Result := true;
end
else
begin
if MsgBox('系统检测到您没有安装VC++环境,是否立刻下载并安装?', mbConfirmation, MB_YESNO) = idYes then
begin
Path := ExpandConstant('{pf}/Internet Explorer/iexplore.exe');
Exec(Path, 'https://xhkjedu.oss-cn-huhehaote.aliyuncs.com/runtime/vc_redist.x86.exe', '', SW_SHOWNORMAL, ewWaitUntilTerminated, ResultCode);
MsgBox('请安装好VC++环境后,再运行本安装包程序!',mbInformation,MB_OK);
Result := false;
end
else
begin
MsgBox('没有安装VC++环境,无法运行程序,本安装程序即将退出!',mbInformation,MB_OK);
Result := false;
end;
end;
end;

function InitializeSetup: Boolean;
begin
Result := CheckVC();
end;

注意

这一种方式解决了相同大版本下不同小版本判断是否安装的问题,但是如果电脑上曾经安装过符合要求的版本后,再卸载,这种方法判断的VC环境还存在,就会导致问题。

检测.Net Framework环境

查看各版本和系统的关系:https://docs.microsoft.com/zh-cn/dotnet/framework/migration-guide/how-to-determine-which-versions-are-installed

查看本机的版本

输入regedit.exe

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\NET Framework Setup\NDP\
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft.NETFramework\policy\

脚本

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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
[Code]  
// Indicates whether the specified version and service pack of the .NET Framework is installed.
//
// version -- Specify one of these strings for the required .NET Framework version:
// 'v1.1' .NET Framework 1.1
// 'v2.0' .NET Framework 2.0
// 'v3.0' .NET Framework 3.0
// 'v3.5' .NET Framework 3.5
// 'v4\Client' .NET Framework 4.0 Client Profile
// 'v4\Full' .NET Framework 4.0 Full Installation
// 'v4.5' .NET Framework 4.5
// 'v4.5.1' .NET Framework 4.5.1
// 'v4.5.2' .NET Framework 4.5.2
// 'v4.6' .NET Framework 4.6
// 'v4.6.1' .NET Framework 4.6.1
// 'v4.6.2' .NET Framework 4.6.2
// 'v4.7' .NET Framework 4.7
// 'v4.7.1' .NET Framework 4.7.1
// 'v4.7.2' .NET Framework 4.7.2
// 'v4.8' .NET Framework 4.8
//
// service -- Specify any non-negative integer for the required service pack level:
// 0 No service packs required
// 1, 2, etc. Service pack 1, 2, etc. required
function IsInstallDotNet(version: string; service: cardinal): boolean;
var
key, versionKey: string;
install, release, serviceCount, versionRelease: cardinal;
success: boolean;
begin
versionKey := version;
versionRelease := 0;

// .NET 1.1 and 2.0 embed release number in version key
if version = 'v1.1' then begin
versionKey := 'v1.1.4322';
end
else if version = 'v2.0' then begin
versionKey := 'v2.0.50727';
end

// .NET 4.5 and newer install as update to .NET 4.0 Full
else if Pos('v4.', version) = 1 then begin
versionKey := 'v4\Full';
case version of
'v4.5': versionRelease := 378389;
'v4.5.1': versionRelease := 378675; // 378758 on Windows 8 and older
'v4.5.2': versionRelease := 379893;
'v4.6': versionRelease := 393295; // 393297 on Windows 8.1 and older
'v4.6.1': versionRelease := 394254; // 394271 on Windows 8.1 and older
'v4.6.2': versionRelease := 394802; // 394806 on Windows 8.1 and older
'v4.7': versionRelease := 460798; // Windows 10
'v4.7.1': versionRelease := 461308; // Windows 10
'v4.7.2': versionRelease := 461808; // Windows 10
'v4.8' : versionRelease := 528040; // Windows 10
end;
end;

// installation key group for all .NET versions
key := 'SOFTWARE\Microsoft\NET Framework Setup\NDP\' + versionKey;

// .NET 3.0 uses value InstallSuccess in subkey Setup
if Pos('v3.0', version) = 1 then begin
success := RegQueryDWordValue(HKLM, key + '\Setup', 'InstallSuccess', install);
end else begin
success := RegQueryDWordValue(HKLM, key, 'Install', install);
end;

// .NET 4.0 and newer use value Servicing instead of SP
if Pos('v4', version) = 1 then begin
success := success and RegQueryDWordValue(HKLM, key, 'Servicing', serviceCount);
end else begin
success := success and RegQueryDWordValue(HKLM, key, 'SP', serviceCount);
end;

// .NET 4.5 and newer use additional value Release
if versionRelease > 0 then begin
success := success and RegQueryDWordValue(HKLM, key, 'Release', release);
success := success and (release >= versionRelease);
end;

result := success and (install = 1) and (serviceCount >= service);
end;

function CheckDotNet : Boolean;
var Path:string;
ResultCode: Integer;
begin
if IsInstallDotNet('v4.5.2', 0) then
begin
Result := true;
end
else
begin
if MsgBox('系统检测到您没有安装.Net Framework4.5.2,是否立刻下载并安装?', mbConfirmation, MB_YESNO) = idYes then
begin
Path := ExpandConstant('{pf}/Internet Explorer/iexplore.exe');
Exec(Path, 'https://xhkjedu.oss-cn-huhehaote.aliyuncs.com/runtime/NDP452-KB2901907-x86-x64-AllOS-ENU.exe', '', SW_SHOWNORMAL, ewWaitUntilTerminated, ResultCode);
MsgBox('请安装好.Net Framework环境后,再运行本安装包程序!',mbInformation,MB_OK);
Result := false;
end
else
begin
MsgBox('没有安装.Net Framework环境,无法运行程序,本安装程序即将退出!',mbInformation,MB_OK);
Result := false;
end;
end;
end;

function InitializeSetup: Boolean;
begin
Result := CheckDotNet();
end;

注意很多文章查找注册表的地址为

1
RegKeyExists(HKLM, 'SOFTWARE/Microsoft/.NETFramework/policy/v2.0')

这个获取的版本比较粗略(类似于v2.0/v4.0)不建议使用这个地址。

如图

image-20210717164543929

地址HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\NET Framework Setup\NDP\获取的就比较细致了

image-20210717164514721

检测C++和.Net Framework环境

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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
[Code]
// 检查VC是否安装
function CheckVC():boolean;
var Path:string;
ResultCode: Integer;
begin
if RegValueExists(HKLM, 'SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\{01FAEC41-B3BC-44F4-B185-5E8475AEB855}', 'Version')
or RegValueExists(HKLM, 'SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\{77EB1EA9-8E1B-459D-8CDC-1984D0FF15B6}', 'Version')
then
begin
Result := true;
end
else
begin
if MsgBox('系统检测到您没有安装VC++环境,是否立刻下载并安装?', mbConfirmation, MB_YESNO) = idYes then
begin
Path := ExpandConstant('{pf}/Internet Explorer/iexplore.exe');
Exec(Path, 'https://xhkjedu.oss-cn-huhehaote.aliyuncs.com/runtime/vc_redist.x86.exe', '', SW_SHOWNORMAL, ewWaitUntilTerminated, ResultCode);
MsgBox('请安装好VC++环境后,再运行本安装包程序!',mbInformation,MB_OK);
Result := false;
end
else
begin
MsgBox('取消后请手动安装VC2015-2019环境!',mbInformation,MB_OK);
Result := true;
end;
end;
end;


function IsInstallDotNet(version: string; service: cardinal): boolean;
var
key, versionKey: string;
install, release, serviceCount, versionRelease: cardinal;
success: boolean;
begin
versionKey := version;
versionRelease := 0;

// .NET 1.1 and 2.0 embed release number in version key
if version = 'v1.1' then begin
versionKey := 'v1.1.4322';
end
else if version = 'v2.0' then begin
versionKey := 'v2.0.50727';
end

// .NET 4.5 and newer install as update to .NET 4.0 Full
else if Pos('v4.', version) = 1 then begin
versionKey := 'v4\Full';
case version of
'v4.5': versionRelease := 378389;
'v4.5.1': versionRelease := 378675; // 378758 on Windows 8 and older
'v4.5.2': versionRelease := 379893;
'v4.6': versionRelease := 393295; // 393297 on Windows 8.1 and older
'v4.6.1': versionRelease := 394254; // 394271 on Windows 8.1 and older
'v4.6.2': versionRelease := 394802; // 394806 on Windows 8.1 and older
'v4.7': versionRelease := 460798; // Windows 10
'v4.7.1': versionRelease := 461308; // Windows 10
'v4.7.2': versionRelease := 461808; // Windows 10
'v4.8' : versionRelease := 528040; // Windows 10
end;
end;

// installation key group for all .NET versions
key := 'SOFTWARE\Microsoft\NET Framework Setup\NDP\' + versionKey;

// .NET 3.0 uses value InstallSuccess in subkey Setup
if Pos('v3.0', version) = 1 then begin
success := RegQueryDWordValue(HKLM, key + '\Setup', 'InstallSuccess', install);
end else begin
success := RegQueryDWordValue(HKLM, key, 'Install', install);
end;

// .NET 4.0 and newer use value Servicing instead of SP
if Pos('v4', version) = 1 then begin
success := success and RegQueryDWordValue(HKLM, key, 'Servicing', serviceCount);
end else begin
success := success and RegQueryDWordValue(HKLM, key, 'SP', serviceCount);
end;

// .NET 4.5 and newer use additional value Release
if versionRelease > 0 then begin
success := success and RegQueryDWordValue(HKLM, key, 'Release', release);
success := success and (release >= versionRelease);
end;

result := success and (install = 1) and (serviceCount >= service);
end;

// 检查.Net是否安装
function CheckDotNet : Boolean;
var Path:string;
ResultCode: Integer;
begin
if IsInstallDotNet('v4.5.2', 0) then
begin
Result := true;
end
else
begin
if MsgBox('系统检测到您没有安装.Net Framework4.5.2,是否立刻下载并安装?', mbConfirmation, MB_YESNO) = idYes then
begin
Path := ExpandConstant('{pf}/Internet Explorer/iexplore.exe');
Exec(Path, 'https://xhkjedu.oss-cn-huhehaote.aliyuncs.com/runtime/NDP452-KB2901907-x86-x64-AllOS-ENU.exe', '', SW_SHOWNORMAL, ewWaitUntilTerminated, ResultCode);
MsgBox('请安装好.Net Framework环境后,再运行本安装包程序!',mbInformation,MB_OK);
Result := false;
end
else
begin
MsgBox('没有安装.Net Framework环境,无法运行程序,本安装程序即将退出!',mbInformation,MB_OK);
Result := false;
end;
end;
end;

function InitializeSetup: Boolean;
begin
Result := CheckDotNet();
Result := CheckVC();
end;

检测其它环境

检测Screen Capturer Recorder

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
[Code]   
function InitializeSetup: Boolean;
var Path:string;
ResultCode: Integer;
begin
if RegValueExists(HKLM, 'SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\Screen Capturer Recorder_is1', 'DisplayName') then
begin
Result := true;
end
else
begin
if MsgBox('系统检测到您没有安装录制环境,是否立刻下载并安装?', mbConfirmation, MB_YESNO) = idYes then
begin
Path := ExpandConstant('{pf}/Internet Explorer/iexplore.exe');
Exec(Path, 'https://xhkjedu.oss-cn-huhehaote.aliyuncs.com/runtime/Setup%20Screen%20Capturer%20Recorder%20v0.12.10.exe', '', SW_SHOWNORMAL, ewWaitUntilTerminated, ResultCode);
MsgBox('请安装好录制环境后,再运行本安装包程序!',mbInformation,MB_OK);
Result := false;
end
else
begin
MsgBox('取消后请手动安装录制环境!',mbInformation,MB_OK);
Result := true;
end;
end;
end;

这个我们会发现其实注册表的真实路径为

SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall\Screen Capturer Recorder_is1

但是我们使用上面的路径依旧能找到,是因为系统会自动重定向到这个目录去找

但是不推荐使用下面的这个路径,下面的这个路径不支持XP,上面的那种都支持。