我最初是在另一个平台(这里)上问这个问题的。
在Inno安装程序中,它具有以下消息定义:
ErrorFileHash2=Invalid file hash: expected %1, found %2当安装程序试图下载并运行具有错误哈希值的文件时,将显示此消息。
在我的剧本里:
function NextButtonClick(CurPageID: integer): boolean;
begin
Result := True;
if (CurPageID = wpSelectTasks) then
begin
DownloadPage.Clear;
if (WizardIsTaskSelected('downloadhelp')) then
AddFileForDownload('{#HelpDocSetupURL}', 'HelpDocSetup.exe',
'{#GetSHA256OfFile("..\HelpNDoc\CHM\Output\MSAHelpDocumentationSetup.exe")}');
end
else
if (CurPageID = wpReady) then
begin
DownloadPage.Show;
try
try
DownloadPage.Download;
Result := True;
except
SuppressibleMsgBox(
AddPeriod(GetExceptionMessage), mbCriticalError, MB_OK, IDOK);
Result := False;
end;
finally
DownloadPage.Hide;
end;
end;
end;出现问题时显示的错误消息相当难看。有人向我提出以下建议:
它只在不处理异常的情况下显示消息框。使用“尝试/除”,然后可以使用添加的文件名重新引发异常,或者使用任务对话框。
我想我应该试试留言框设计师:

它创建以下代码:
// Display a message box
SuppressibleTaskDialogMsgBox(
'Unable to download [file]', 'This is because the checksum value does not match',
mbError, MB_OK, ['OK'], 0, IDOK);但我不知道我在这里做什么。
发布于 2020-11-29 10:17:23
换掉你现在的样子:
SuppressibleMsgBox(
AddPeriod(GetExceptionMessage), mbCriticalError, MB_OK, IDOK);用你的新代码:
SuppressibleTaskDialogMsgBox(
'Unable to download [file]', 'This is because the checksum value does not match',
mbError, MB_OK, ['OK'], 0, IDOK);

如果要标识失败的下载,可以使用DownloadPage.Msg2Label.Caption的值(如果要移动消息框,可以看到它)。
如果需要在消息中包含散列,则必须解析错误消息中的数据。这方法有点脆弱。但是,如果您提供了一个后备消息,如果解析失败,它是可行的。
以下函数尝试将数据从任何标准的Inno安装字符串中解析出来:
function ParseDataFromSetupMessage(
Msg: string; ID: TSetupMessageID; var Data: TArrayOfString): Boolean;
var
MsgOrig, Pattern, PatternOrig, S: string;
I, P, P2: Integer;
begin
try
MsgOrig := Msg;
Pattern := SetupMessage(ID);
PatternOrig := Pattern;
while (Msg <> '') and (Pattern <> '') do
begin
P := Pos('%', Pattern);
if (P = 0) or (P = Length(Pattern)) or (P > 1) then
begin
if (P = 0) or (P = Length(Pattern)) then P := Length(Pattern) + 1;
if Copy(Msg, 1, P - 1) <> Copy(Pattern, 1, P - 1) then Abort;
Delete(Msg, 1, P - 1);
Delete(Pattern, 1, P - 1);
end
else
if (Pattern[2] < '1') or (Pattern[2] > '9') then
begin
if Copy(Msg, 1, 1) <> '%' then Abort;
Delete(Pattern, 1, 1);
Delete(Msg, 1, 1);
end
else
begin
I := StrToInt(Pattern[2]);
Delete(Pattern, 1, 2);
if Length(Pattern) = 0 then
begin
S := Msg;
SetLength(Msg, 0);
end
else
begin
P := Pos('%', Pattern);
if P = 0 then P := Length(Pattern) + 1;
P2 := Pos(Copy(Pattern, 1, P - 1), Msg);
if P2 = 0 then Abort;
S := Copy(Msg, 1, P2 - 1);
Delete(Msg, 1, P2 - 1);
end;
if GetArrayLength(Data) < I then
SetArrayLength(Data, I);
Data[I - 1] := S;
end;
end;
if Msg <> Pattern then Abort;
Result := True;
except
Log(Format('"%s" does not seem to match format string "%s".', [
MsgOrig, PatternOrig]));
Result := False;
end;
end;您可以在except块中使用这两种方法,如下所示:
except
Msg := GetExceptionMessage;
if ParseDataFromSetupMessage(Msg, msgErrorFileHash2, Data) then
begin
Expected := Data[0];
Hash := Data[1];
Msg :=
'This is because the checksum value does not match.' + #13+
'Download: ' + DownloadPage.Msg2Label.Caption + #13 +
'Expected: ' + Expected + #13 +
'Got: ' + Hash;
end
else
begin
// Failed for other reasons?
Msg :=
'Download has failed.' + #13+
'Download: ' + DownloadPage.Msg2Label.Caption + #13 +
'Details: ' + Msg;
end;
SuppressibleTaskDialogMsgBox(
'Unable to download', Msg, mbError, MB_OK, ['OK'], 0, IDOK);
Result := False;
end;https://stackoverflow.com/questions/65058580
复制相似问题