如何在Inno Setup中对A.exe多版本执行MD5校验并管控安装流程?
Inno Setup 多版本MD5校验实现方案
现有可运行代码
[Code] var ComMD5: Integer; FileExit: Boolean; function InitializeSetup: boolean; begin FileExit := FileExists(ExpandConstant('C:\Program Files (x86)\A\A.exe')); if FileExit = false then begin MsgBox('File does not exist!', mbInformation, MB_OK); Result := False; end else begin ComMD5 := CompareStr(GetMD5OfFile(ExpandConstant('C:\Program Files (x86)\A\A.exe')), 'c2d33f81e31eb54adf9323c27a9af536'); if ComMD5 = 0 then begin MsgBox('The file version is incorrect, please install the correct version!', mbInformation, MB_OK); Result := False; end else Result := True; end; end;
需求说明
前提条件:系统中已安装A.exe,该文件共有5个版本,对应MD5值分别为aMD5、bMD5、cMD5、dMD5、eMD5。
安装包运行时需对A.exe执行MD5校验,规则如下:
- 若MD5值等于
aMD5或bMD5,安装程序继续执行; - 若MD5值等于
cMD5、dMD5或eMD5,弹出提示:
The file version is incorrect, please install the correct version!
并终止安装流程。
修改后的实现代码
[Code] var FileExit: Boolean; FileMD5: String; function InitializeSetup: boolean; begin Result := True; FileExit := FileExists(ExpandConstant('C:\Program Files (x86)\A\A.exe')); if not FileExit then begin MsgBox('文件不存在!', mbInformation, MB_OK); Result := False; Exit; end; // 获取目标文件的MD5值 FileMD5 := GetMD5OfFile(ExpandConstant('C:\Program Files (x86)\A\A.exe')); // 校验允许的MD5值(替换为实际的aMD5、bMD5字符串) if (FileMD5 = 'aMD5') or (FileMD5 = 'bMD5') then begin Result := True; end // 校验禁止的MD5值(替换为实际的cMD5、dMD5、eMD5字符串) else if (FileMD5 = 'cMD5') or (FileMD5 = 'dMD5') or (FileMD5 = 'eMD5') then begin MsgBox('The file version is incorrect, please install the correct version!', mbInformation, MB_OK); Result := False; end // 处理未在列表中的MD5值,可根据需求调整逻辑 else begin // 示例:默认允许安装,若需禁止可取消下方注释 // MsgBox('未知的文件版本!', mbInformation, MB_OK); // Result := False; Result := True; end; end;
代码说明
- 优先检查目标文件是否存在,不存在则直接终止安装并提示;
- 获取文件MD5值后,分别与允许、禁止的MD5列表进行匹配;
- 匹配允许的MD5则继续安装流程,匹配禁止的MD5则弹出提示并终止;
- 对于不在预设列表中的MD5值,可根据实际需求调整处理逻辑(代码中默认允许安装,可自行修改)。
内容的提问来源于stack exchange,提问作者i6henl
相关产品推荐
相关产品推荐

