首页 > 解决方案 > 在 Inno Setup 的一个不错的任务对话框窗口中显示有关下载文件的错误哈希的信息

问题描述

我最初在另一个平台(这里)上问过这个问题。

在 Inno Setup 中,它具有以下消息定义:

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;

出现问题时显示的错误消息相当难看。以下是向我提出的建议:

如果您不处理异常,它只会显示一个消息框。使用 try/except 然后你可以做一些事情,比如通过添加文件名或使用任务对话框重新引发异常。

我想我会尝试消息框设计器:

在此处输入图像描述

这将创建以下代码:

// Display a message box
SuppressibleTaskDialogMsgBox(
  'Unable to download [file]', 'This is because the checksum value does not match',
  mbError, MB_OK, ['OK'], 0, IDOK);

但我不知道我在这里做什么。

标签: inno-setuppascalscriptinno-setup-v6

解决方案


只需替换您当前的:

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 Setup 字符串中解析数据:

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;

推荐阅读