杉宫竹苑工作室

 找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 2700|回复: 0

Inno Setup 制作部署安装包

[复制链接]
发表于 2017-3-27 17:03:34 | 显示全部楼层 |阅读模式

正式会员享受无限制浏览网站功能和高速网盘下载,赶快加入本站吧!

您需要 登录 才可以下载或查看,没有账号?立即注册

x
要求:
1、打包指定文件(夹)时,该文件(夹)必须存在文件,不然编译时提示错误。路径(可使用相对路径)CheckOfficeConsole\DotNetFramework必须要存在文件,可以是任何类型的。
2、检测.net framework,如果不存在则从指定网址下载安装包时,需要用到isxdl.dll。这个dll需要下载并安装ISTool。
3、.net framework 的下载地址,我找到了除v3.0、v1版本以外的安装包地址,测试时都可用。未来微软网站也许会对这些资源包地址更新,所以不保证一直可用。
4、安装过程中,可通过Inno的函数读取指定目录的.ini文件,并获得文件里的参数值。这个太赞了有木有!一来可以减少在Inno脚本里硬编码,二来可以在安装过程中初始化相关数据。


  1. ; Script generated by the Inno Setup Script Wizard.
  2. ; SEE THE DOCUMENTATION FOR DETAILS ON CREATING INNO SETUP SCRIPT FILES!
  3. ; Ivan 2015-1-30

  4. #define MyAppName "VSTO Excel by Ivan"
  5. #define MyAppVersion "1.0"
  6. #define MyAppPublisher "My Company, Inc."
  7. #define MyAppURL "http://www.IvanBy.com/"
  8. #define MyAppExeName "CheckOfficeConsole.exe"

  9. [Setup]
  10. ; NOTE: The value of AppId uniquely identifies this application.
  11. ; Do not use the same AppId value in installers for other applications.
  12. ; (To generate a new GUID, click Tools | Generate GUID inside the IDE.)
  13. AppId={{6DFC5AE2-4844-4440-A6B3-284E15A14AEA}
  14. AppName={#MyAppName}
  15. AppVersion={#MyAppVersion}
  16. ;AppVerName={#MyAppName} {#MyAppVersion}
  17. AppPublisher={#MyAppPublisher}
  18. AppPublisherURL={#MyAppURL}
  19. AppSupportURL={#MyAppURL}
  20. AppUpdatesURL={#MyAppURL}
  21. DefaultDirName={pf}\{#MyAppName}
  22. DefaultGroupName={#MyAppName}
  23. OutputBaseFilename=setup
  24. Compression=lzma
  25. SolidCompression=yes

  26. [Languages]
  27. Name: "english"; MessagesFile: "compiler:Default.isl"

  28. [Tasks]
  29. Name: "desktopicon"; Description: "{cm:CreateDesktopIcon}"; GroupDescription: "{cm:AdditionalIcons}"; Flags: unchecked

  30. [Files]
  31. Source: C:\Program Files (x86)\ISTool\isxdl.dll; Flags: dontcopy;
  32. Source: "CheckOfficeConsole\bin\Release\CheckOfficeConsole.exe"; DestDir: "{app}"; Flags: ignoreversion
  33. Source: "CheckOfficeConsole\DotNetFramework\*"; DestDir: "{tmp}"; Flags: ignoreversion
  34. Source: "Excel2010Setup\Excel2010Setup\Express\DVD-5\DiskImages\*"; DestDir: "{app}\2010"; Flags: ignoreversion recursesubdirs createallsubdirs
  35. ; NOTE: Don't use "Flags: ignoreversion" on any shared system files

  36. [Icons]
  37. Name: "{group}\{#MyAppName}"; Filename: "{app}\{#MyAppExeName}"
  38. Name: "{commondesktop}\{#MyAppName}"; Filename: "{app}\{#MyAppExeName}"; Tasks: desktopicon
  39. Name: "{group}\{cm:UninstallProgram,{#MyAppName}}"; Filename: "{uninstallexe}"

  40. ;更改显示在程序中显示的消息文本
  41. [Messages]
  42. BeveledLabel=Ivan Code

  43. [Run]
  44. ;Filename: "{app}\{#MyAppExeName}"; Description: "{cm:LaunchProgram,{#StringChange(MyAppName, '&', '&&')}}"; Flags: nowait postinstall skipifsilent
  45. Filename: "{app}\{#MyAppExeName}"

  46. [Code]
  47. var
  48.     dotNetDownloadNeeded: boolean;
  49.     dotNetLocalPath:string;
  50.   
  51. procedure isxdl_AddFile(URL, Filename: PAnsiChar);
  52. external 'isxdl_AddFile@files:isxdl.dll stdcall';
  53. function isxdl_DownloadFiles(hWnd: Integer): Integer;
  54. external 'isxdl_DownloadFiles@files:isxdl.dll stdcall';
  55. function isxdl_SetOption(Option, Value: PAnsiChar): Integer;
  56. external 'isxdl_SetOption@files:isxdl.dll stdcall';

  57. //检测是否存在特定版本的.net framework
  58. function IsDotNetDetected(version: string; service:cardinal): boolean;
  59. // Indicates whether the specified version and service pack of the .NET Framework is installed.
  60. //
  61. // version -- Specify one of these strings for the required .NET Framework version:
  62. //    'v1.1.4322'     .NET Framework 1.1
  63. //    'v2.0.50727'    .NET Framework 2.0
  64. //    'v3.0'          .NET Framework 3.0
  65. //    'v3.5'          .NET Framework 3.5
  66. //    'v4\Client'     .NET Framework 4.0 Client Profile
  67. //    'v4\Full'       .NET Framework 4.0 Full Installation
  68. //    'v4.5'          .NET Framework 4.5
  69. //
  70. // service -- Specify any non-negative integer for the required service pack level:
  71. //    0               No service packs required
  72. //    1, 2, etc.      Service pack 1, 2, etc. required
  73. var
  74.     key: string;
  75.     install, release, serviceCount: cardinal;
  76.     check45, success: boolean;
  77. begin
  78.     // .NET 4.5 installs as update to .NET 4.0 Full
  79.     if version = 'v4.5' then begin
  80.         version := 'v4\Full';
  81.         check45 := true;
  82.     end else
  83.         check45 := false;

  84.     // installation key group for all .NET versions
  85.     key := 'SOFTWARE\Microsoft\NET Framework Setup\NDP\' + version;
  86.    
  87.     // .NET 3.0 uses value InstallSuccess in subkey Setup
  88.     if Pos('v3.0', version) = 1 then begin
  89.         success := RegQueryDWordValue(HKLM, key + '\Setup', 'InstallSuccess', install);
  90.     end else begin
  91.         success := RegQueryDWordValue(HKLM, key, 'Install', install);
  92.     end;

  93.     // .NET 4.0/4.5 uses value Servicing instead of SP
  94.     if Pos('v4', version) = 1 then begin
  95.         success := success and RegQueryDWordValue(HKLM, key, 'Servicing', serviceCount);
  96.     end else begin
  97.         success := success and RegQueryDWordValue(HKLM, key, 'SP', serviceCount);
  98.     end;

  99.     // .NET 4.5 uses additional value Release
  100.     if check45 then begin
  101.         success := success and RegQueryDWordValue(HKLM, key, 'Release', release);
  102.         success := success and (release >= 378389);
  103.     end;

  104.     result := success and (install = 1) and (serviceCount >= service);
  105. end;

  106. //准备安装.net framework需要的条件(本地还是联网)
  107. function PreInstallDotNet(dotNetName:string;dotNetDownloadUrl:string):boolean;
  108. begin
  109.     if (not IsAdminLoggedOn()) then begin
  110.       MsgBox('您电脑安装 Microsoft .NET Framework 需要管理员权限', mbInformation, MB_OK);
  111.       Result := false;
  112.     end else begin
  113.         dotNetLocalPath := ExpandConstant('{src}') + '\'+dotNetName;
  114.         if not FileExists(dotNetLocalPath)  then begin
  115.             dotNetLocalPath := ExpandConstant('{tmp}') + '\'+dotNetName;
  116.             if not FileExists(dotNetLocalPath)  then begin
  117.                 isxdl_AddFile(dotNetDownloadUrl, dotNetLocalPath);
  118.                 dotNetDownloadNeeded := true;
  119.             end;
  120.         end;
  121.         
  122.         SetIniString('install', 'dotnetRedist', dotNetLocalPath, ExpandConstant('{tmp}\dep.ini'));
  123.     end;
  124.    
  125. end;

  126. //执行安装.net framework
  127. function DoInstallDotNet():boolean;
  128. var
  129.   hWnd: Integer;
  130.   ResultCode: Integer;
  131. begin
  132.     result := true;
  133.     hWnd := StrToInt(ExpandConstant('{wizardhwnd}'));

  134.     // don’t try to init isxdl if it’s not needed because it will error on < ie 3
  135.     if dotNetDownloadNeeded then begin
  136.       isxdl_SetOption('label', '正在下载 Microsoft .NET Framework');
  137.       isxdl_SetOption('des-c-r-i-p-tion', '您还未安装Microsoft .NET Framework. 请您耐心等待几分钟,下载完成后会安装到您的的计算机中。');
  138.       if isxdl_DownloadFiles(hWnd) = 0 then result := false;
  139.     end;
  140.    
  141.     if result = true  then begin
  142.       if Exec(ExpandConstant(dotNetLocalPath), '/qb', '', SW_SHOW, ewWaitUntilTerminated, ResultCode) then begin
  143.          // handle success if necessary; ResultCode contains the exit code
  144.          if not (ResultCode = 0) then begin
  145.            result := false;
  146.          end;
  147.       end else begin
  148.          // handle failure if necessary; ResultCode contains the error code
  149.          result := false;
  150.       end;
  151.     end;
  152.    
  153. end;

  154. //检测是否安装了等于大于指定版本的.net framework
  155. function IsIncludeFramework(version: string): boolean;
  156. var
  157.     isInclued:boolean;     
  158. begin   
  159.    
  160.     //最高版本的
  161.     if IsDotNetDetected('v4.5',0) then begin
  162.         isInclued := true;         
  163.     end else if version = 'v4.5' then begin
  164.         PreInstallDotNet('dotNetFx45_Full_setup.exe','http://download.microsoft.com/download/B/A/4/BA4A7E71-2906-4B2D-A0E1-80CF16844F5F/dotNetFx45_Full_setup.exe');
  165.     end else if IsDotNetDetected('v4\Full',0) then begin
  166.         isInclued := true;
  167.     end else if version = 'v4\Full' then begin
  168.         PreInstallDotNet('dotNetFx40_Full_x86_x64.exe','http://download.microsoft.com/download/9/5/A/95A9616B-7A37-4AF6-BC36-D6EA96C8DAAE/dotNetFx40_Full_x86_x64.exe');
  169.     end else if IsDotNetDetected('v4\Client',0) then begin
  170.         isInclued := true;     
  171.     end else if version = 'v4\Client' then begin
  172.          PreInstallDotNet('dotNetFx40_Client_x86_x64.exe','http://download.microsoft.com/download/5/6/2/562A10F9-C9F4-4313-A044-9C94E0A8FAC8/dotNetFx40_Client_x86_x64.exe');
  173.     end else if IsDotNetDetected('v3.5',0) then begin
  174.         isInclued := true;     
  175.     end else if Pos('v3.5',version) = 1 then begin
  176.          PreInstallDotNet('dotNetFx35setup.exe','http://download.microsoft.com/download/7/0/3/703455ee-a747-4cc8-bd3e-98a615c3aedb/dotNetFx35setup.exe');
  177.     end else if IsDotNetDetected('v3.0',0) then begin
  178.         isInclued := true;     
  179.     end else if Pos('v3.0',version) = 1 then begin
  180.          PreInstallDotNet('dotNetFx35setup.exe','http://download.microsoft.com/download/7/0/3/703455ee-a747-4cc8-bd3e-98a615c3aedb/dotNetFx35setup.exe');
  181.     end else if IsDotNetDetected('v2.0.50727',0) then begin
  182.         isInclued := true;     
  183.     end else if Pos('v2',version) = 1 then begin
  184.         PreInstallDotNet('dotnetfx.exe','http://download.microsoft.com/download/5/6/7/567758a3-759e-473e-bf8f-52154438565a/dotnetfx.exe');
  185.     end else if IsDotNetDetected('v1.1.4322',0) then begin
  186.         isInclued:= true;
  187.     end else if Pos('v1',version)=1 then begin
  188.         PreInstallDotNet('dotNetFx35setup.exe','http://download.microsoft.com/download/7/0/3/703455ee-a747-4cc8-bd3e-98a615c3aedb/dotNetFx35setup.exe');
  189.     end;
  190.    
  191.     result := isInclued;
  192. end;

  193. //取得自定义的配置
  194. //     Setup.ini
  195. //     [Custom]
  196. //   dotNetVersion =  v4.5
  197. function GetCustomConfig(key:string):string;
  198. var
  199.   myValue:string;
  200. begin
  201.   myValue:=ExpandConstant('{ini:{src}\Setup.ini,Custom,'+key+'}')
  202.   result := myValue;
  203. end;

  204. function InitializeSetup(): Boolean;
  205. begin
  206.     //do something

  207.   result:= true;
  208. end;

  209. function NextButtonClick(CurPage: Integer): Boolean;
  210. var
  211.   dotNetVersion:string;
  212. begin
  213.     Result := true;

  214.   if (CurPage = wpReady) then begin

  215.     dotNetVersion := GetCustomConfig('dotNetVersion');
  216.     if Length(dotNetVersion) = 0 then begin
  217.        dotNetVersion := 'v4.0';
  218.     end else if not (Pos('v',dotNetVersion) = 1) then begin
  219.         dotNetVersion := 'v'+dotNetVersion;
  220.     end;
  221.      
  222.     if not IsIncludeFramework(dotNetVersion) then begin
  223.       if not DoInstallDotNet() then begin
  224.          MsgBox('当前操作需要安装.NET Framework ' + dotNetVersion + '或以上版本。'#13#13
  225.           '在尝试自动安装期间,似乎出现一些小问题(或用户取消了安装),'#13
  226.           '请重试尝试安装。', mbInformation, MB_OK);
  227.         result:= false;
  228.       end;
  229.     end;

  230.   end;
  231.   
  232. end;

  233. procedure CurUninstallStepChanged(CurUninstallStep: TUninstallStep);
  234. var
  235.   ErrorCode: Integer;
  236. begin
  237.   case CurUninstallStep of
  238.     usUninstall:
  239.       begin        
  240.         // 正在卸载
  241.       end;
  242.     usPostUninstall:
  243.       begin
  244.         //卸载完成      
  245.         ShellExec('open', 'http://www.IvanBy.com', '', '', SW_SHOW, ewNoWait, ErrorCode)

  246.       end;
  247.   end;
  248. end;
复制代码
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|小黑屋|手机版|Archiver|SgzyStudio

GMT+8, 2024-5-17 14:31 , Processed in 0.109417 second(s), 22 queries .

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表