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

标签: Inno Setup, Inno

博客分类: 教程, 示例

返回目录索引
示例代码-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/ 中的其他工具和框架

``` pascal

[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文件的属性-详细信息-产品版本的第三级版本号

``` pascal

//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现在是共享相同的可再发行软件文件】

``` pascal

//安装包初始化                      
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;

```


解:奇葩史