Inno Setup:如何实现下载文件哈希错误的美观任务对话框展示
Customizing Hash Mismatch Error Dialog in Inno Setup
I get it, the default error message for hash mismatches during downloads is pretty barebones and not user-friendly. Let's fix that by catching the exception, extracting critical details (filename, expected/found hashes), and displaying them in a clean, informative task dialog.
Modified NextButtonClick Function
Replace your existing except block with the code below to add the customized error handling:
function NextButtonClick(CurPageID: integer): boolean; var ExpectedHash, FoundHash, FileName: string; MsgParts: TArrayOfString; 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 // Initialize fallback values ExpectedHash := 'N/A'; FoundHash := 'N/A'; FileName := 'Unknown file'; // Get the failed filename from DownloadPage's failed files list if DownloadPage.FailedFiles.Count > 0 then FileName := ExtractFileName(DownloadPage.FailedFiles[0]); // Parse the exception message to extract hash values MsgParts := SplitString(GetExceptionMessage, ': '); if Length(MsgParts) >= 2 then begin MsgParts := SplitString(MsgParts[1], ', '); if Length(MsgParts) >= 2 then begin // Strip the "expected " prefix to get the raw hash ExpectedHash := Trim(Copy(MsgParts[0], 10, MaxInt)); // Strip the "found " prefix to get the raw hash FoundHash := Trim(Copy(MsgParts[1], 7, MaxInt)); end; end; // Display the customized task dialog with all details SuppressibleTaskDialogMsgBox( Format('Unable to download %s', [FileName]), Format('The file checksum does not match:'#13#10 + 'Expected SHA-256 hash: %s'#13#10 + 'Found SHA-256 hash: %s', [ExpectedHash, FoundHash]), mbError, MB_OK, ['OK'], 0, IDOK); Result := False; end; finally DownloadPage.Hide; end; end; end;
Key Details Explained
- Failed File Context: We use
DownloadPage.FailedFilesto fetch the name of the problematic file—this lets the user know exactly which download failed, adding critical context. - Exception Message Parsing: The default error message follows your
ErrorFileHash2template. We split this string to pull out the expected and actual hash values, so we can display them clearly instead of a generic message. - Clean Dialog Layout: Using
SuppressibleTaskDialogMsgBoxcreates a modern dialog with a clear title and formatted details (line breaks separate the hash values for easy readability).
Notes
- Ensure you're using Inno Setup 5.6 or later—
SplitStringis a built-in function available from this version onwards. - If you've modified the
ErrorFileHash2message template, adjust the string parsing logic (like theCopyoffsets) to match your custom message format.
内容的提问来源于stack exchange,提问作者Andrew Truckle
相关产品推荐
相关产品推荐

