如何为Microsoft Store上架的InnoSetup安装程序设置错误返回码
问题描述
我想用Inno Setup打包exe程序并上架Microsoft Store。在Microsoft Store后台注册应用时,需要为exe程序注册「错误返回码」,可选类型如下:
- 用户取消安装
- 应用已存在
- 安装已在进行中
- 磁盘空间不足
- 需要重启
- 网络故障
- 安装时包被拒绝
- 安装成功
我了解到Inno Setup中用户取消操作可能返回2或5等不同错误码,但Microsoft Store对应仅需注册一个用户取消的条目。这种情况下是否需要自定义Inno Setup的返回码?对应的代码该怎么写?以下是我的原始Inno Setup代码:
; Script generated by the Inno Setup Script Wizard. ; SEE THE DOCUMENTATION FOR DETAILS ON CREATING INNO SETUP SCRIPT FILES! #define MyAppName "testApp" #define MyAppVersion "1.00" #define MyAppPublisher "My Company, Inc." #define MyAppURL "http://www.example.com/" #define MyAppExeName "test.exe" [Setup] ; NOTE: The value of AppId uniquely identifies this application. Do not use the same AppId value in installers for other applications. ; (To generate a new GUID, click Tools | Generate GUID inside the IDE.) AppId={{3D8E37F3-FB32-4AF9-8C64-58C37D542248} AppName={#MyAppName} AppVersion={#MyAppVersion} ;AppVerName={#MyAppName} {#MyAppVersion} AppPublisher={#MyAppPublisher} AppPublisherURL={#MyAppURL} AppSupportURL={#MyAppURL} AppUpdatesURL={#MyAppURL} DefaultDirName={autopf}\{#MyAppName} DisableProgramGroupPage=yes ; Uncomment the following line to run in non administrative install mode (install for current user only.) ;PrivilegesRequired=lowest OutputDir=C:\Users\taichi\Desktop OutputBaseFilename=mysetup Compression=lzma SolidCompression=yes WizardStyle=modern [Languages] Name: "english"; MessagesFile: "compiler:Default.isl" [Tasks] Name: "desktopicon"; Description: "{cm:CreateDesktopIcon}"; GroupDescription: "{cm:AdditionalIcons}"; Flags: unchecked [Files] Source: "C:\Users\taichi\Desktop\dist\test\test.exe"; DestDir: "{app}"; Flags: ignoreversion Source: "C:\Users\taichi\Desktop\dist\test\*"; DestDir: "{app}"; Flags: ignoreversion recursesubdirs createallsubdirs ; NOTE: Don't use "Flags: ignoreversion" on any shared system files [Icons] Name: "{autoprograms}\{#MyAppName}"; Filename: "{app}\{#MyAppExeName}" Name: "{autodesktop}\{#MyAppName}"; Filename: "{app}\{#MyAppExeName}"; Tasks: desktopicon [Run] Filename: "{app}\{#MyAppExeName}"; Description: "{cm:LaunchProgram,{#StringChange(MyAppName, '&', '&&')}}"; Flags: nowait postinstall skipifsilent
解决方案
需要自定义Inno Setup的返回码,把多种用户取消场景的返回码统一为一个标准值,同时为其他错误场景匹配Microsoft Store支持的返回码,确保平台能正确识别安装状态。
修改后的完整代码
; Script generated by the Inno Setup Script Wizard. ; SEE THE DOCUMENTATION FOR DETAILS ON CREATING INNO SETUP SCRIPT FILES! #define MyAppName "testApp" #define MyAppVersion "1.00" #define MyAppPublisher "My Company, Inc." #define MyAppURL "http://www.example.com/" #define MyAppExeName "test.exe" [Setup] ; NOTE: The value of AppId uniquely identifies this application. Do not use the same AppId value in installers for other applications. ; (To generate a new GUID, click Tools | Generate GUID inside the IDE.) AppId={{3D8E37F3-FB32-4AF9-8C64-58C37D542248} AppName={#MyAppName} AppVersion={#MyAppVersion} ;AppVerName={#MyAppName} {#MyAppVersion} AppPublisher={#MyAppPublisher} AppPublisherURL={#MyAppURL} AppSupportURL={#MyAppURL} AppUpdatesURL={#MyAppURL} DefaultDirName={autopf}\{#MyAppName} DisableProgramGroupPage=yes ; Uncomment the following line to run in non administrative install mode (install for current user only.) ;PrivilegesRequired=lowest OutputDir=C:\Users\taichi\Desktop OutputBaseFilename=mysetup Compression=lzma SolidCompression=yes WizardStyle=modern ; 声明允许使用的自定义退出码,避免被Inno默认逻辑覆盖 ExitCodeList=0,1602,1603,1607,3010 [Languages] Name: "english"; MessagesFile: "compiler:Default.isl" [Tasks] Name: "desktopicon"; Description: "{cm:CreateDesktopIcon}"; GroupDescription: "{cm:AdditionalIcons}"; Flags: unchecked [Files] Source: "C:\Users\taichi\Desktop\dist\test\test.exe"; DestDir: "{app}"; Flags: ignoreversion Source: "C:\Users\taichi\Desktop\dist\test\*"; DestDir: "{app}"; Flags: ignoreversion recursesubdirs createallsubdirs ; NOTE: Don't use "Flags: ignoreversion" on any shared system files [Icons] Name: "{autoprograms}\{#MyAppName}"; Filename: "{app}\{#MyAppExeName}" Name: "{autodesktop}\{#MyAppName}"; Filename: "{app}\{#MyAppExeName}"; Tasks: desktopicon [Run] Filename: "{app}\{#MyAppExeName}"; Description: "{cm:LaunchProgram,{#StringChange(MyAppName, '&', '&&')}}"; Flags: nowait postinstall skipifsilent [Code] function InitializeSetup(): Boolean; begin Result := True; // 预检查应用是否已存在,匹配Microsoft Store的「应用已存在」类型 if DirExists(ExpandConstant('{app}')) then begin MsgBox('应用已存在!', mbError, MB_OK); ExitCode := 1603; // 对应「应用已存在」的标准返回码 Result := False; end; end; procedure DeinitializeSetup(); begin // 统一Inno默认的用户取消返回码(2、5)为Microsoft Store识别的1602 if (ExitCode = 2) or (ExitCode = 5) then begin ExitCode := 1602; // 对应「用户取消安装」的标准返回码 end; end; // 捕获安装过程中的取消操作 procedure CurStepChanged(CurStep: TSetupStep); begin if CurStep = ssInstall then begin if WizardForm.CancelButton.Enabled then begin ExitCode := 1602; end; end; end;
关键说明
- 标准返回码映射:使用Microsoft Store兼容的标准返回码:
- 0:安装成功
- 1602:用户取消安装
- 1603:应用已存在
- 1607:磁盘空间不足
- 3010:需要重启
- ExitCodeList配置:在
[Setup]段声明允许使用的退出码,防止Inno Setup覆盖自定义值。 - 统一取消场景:通过
DeinitializeSetup将Inno默认的用户取消返回码(2、5)替换为1602,确保Microsoft Store能统一识别该场景。
内容的提问来源于stack exchange,提问作者taichi
相关产品推荐
相关产品推荐

