首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在Inno安装程序中的好任务对话框窗口中显示有关下载文件的错误散列的信息。

在Inno安装程序中的好任务对话框窗口中显示有关下载文件的错误散列的信息。
EN

Stack Overflow用户
提问于 2020-11-29 09:17:51
回答 1查看 146关注 0票数 2

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

在Inno安装程序中,它具有以下消息定义:

代码语言:javascript
复制
ErrorFileHash2=Invalid file hash: expected %1, found %2

当安装程序试图下载并运行具有错误哈希值的文件时,将显示此消息。

在我的剧本里:

代码语言:javascript
复制
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;

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

它只在不处理异常的情况下显示消息框。使用“尝试/除”,然后可以使用添加的文件名重新引发异常,或者使用任务对话框。

我想我应该试试留言框设计师:

它创建以下代码:

代码语言:javascript
复制
// Display a message box
SuppressibleTaskDialogMsgBox(
  'Unable to download [file]', 'This is because the checksum value does not match',
  mbError, MB_OK, ['OK'], 0, IDOK);

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

  • 如何处理显示此错误的异常?
  • 如何显示更好的任务对话框?一旦其中还包括有详细信息和文件名?
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-11-29 10:17:23

换掉你现在的样子:

代码语言:javascript
复制
SuppressibleMsgBox(
  AddPeriod(GetExceptionMessage), mbCriticalError, MB_OK, IDOK);

用你的新代码:

代码语言:javascript
复制
SuppressibleTaskDialogMsgBox(
  'Unable to download [file]', 'This is because the checksum value does not match',
  mbError, MB_OK, ['OK'], 0, IDOK);

如果要标识失败的下载,可以使用DownloadPage.Msg2Label.Caption的值(如果要移动消息框,可以看到它)。

如果需要在消息中包含散列,则必须解析错误消息中的数据。这方法有点脆弱。但是,如果您提供了一个后备消息,如果解析失败,它是可行的。

以下函数尝试将数据从任何标准的Inno安装字符串中解析出来:

代码语言:javascript
复制
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块中使用这两种方法,如下所示:

代码语言:javascript
复制
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;
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65058580

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档