Quantcast
Channel: 簡睿隨筆
Viewing all articles
Browse latest Browse all 897

使用Inno Setup包裝安裝程式的備忘

$
0
0

最近使用Inno Setup來包裝一個Web系統,提供給使用者以執行安裝檔的簡單步驟就能把整個運行環境建置起來,以下是使用Inno Setup的一些備忘。

一開始是使用Inno Setup的Wizard來建立基本架構,再著手添加額外功能。

  1. Inno Setup是使用Delphi撰寫的,它的包裝檔是個標準的ini格式,擴充的[code]就是Delphi語法。

  2. [Files]寫要加到安裝檔裡的檔案,例如:

  [Files]
  Source: "E:\install\jdk-7u51-windows-i586.exe"; DestDir: "c:\NEW\JDK1.7"; Flags: ignoreversion; Check: IsNotJDKInstalled 
  Source: "E:\install\install\*"; DestDir: "C:\NEW\install\"; Flags: ignoreversion recursesubdirs createallsubdirs
  

第一個來源檔是JDK 1.7u51,最後面的 Check 參數可以指定檢查用的procedure,當沒有JDK存在時才需要安裝。擴充的程式碼都要寫在[code]裡。

  [code]
  function IsNotJDKInstalled: Boolean;
  begin
    if IsWin64() then begin
      Result := RegKeyExists(HKLM64, 'SOFTWARE\JavaSoft\Java Runtime Environment');
      //if Result then MsgBox('JDK YES', mbError, MB_OK)
      //else MsgBox('JDK NO', mbError, MB_OK);

      if not Result then begin
        Result := RegKeyExists(HKLM32, 'SOFTWARE\JavaSoft\Java Runtime Environment');
      end;
    end else begin
      Result := RegKeyExists(HKLM32, 'SOFTWARE\JavaSoft\Java Runtime Environment');
    end;
    //if Result then MsgBox('JDK YES', mbError, MB_OK)
    //else MsgBox('JDK NO', mbError, MB_OK);

    Result := not Result;
  end;
  
  1. 安裝程式解壓縮並建立目的檔後要額外執行的操作可以寫在[Run]裡,以下是結束安裝前執行JDK的安裝程式jdk-7u51-windows-i586.exe。Flags參數裡的shellexec指定用ShellExec的方式執行檔案,waituntilterminated讓JDK安裝完成後才會繼續Inno Setup的安裝程式:
  [Run]
  Filename: "C:\NEW\JDK1.7\jdk-7u51-windows-i586.exe"; Description: "Install JDK"; StatusMsg: "Installing JDK..."; Flags: shellexec waituntilterminated; Check:   IsNotJDKInstalled
  Filename: "C:\NEW\install\after-install.bat"; Parameters: ""
  
  1. 我們可以寫一個InitializeSetup()函數,在安裝程式開始解壓縮前做一些前期檢查,例如檢查SQL Server是否已安裝?是否有JAVA_HOME環境變數等。
  [code]
  function GetJREVersion64(): String;
  begin
    Result := '';
  
    RegQueryStringValue( HKLM64, 'SOFTWARE\JavaSoft\Java Runtime Environment', 'CurrentVersion', Result );
  end;
  
  function GetJREVersion32(): String;
  begin
    Result := '';
  
    RegQueryStringValue( HKLM32, 'SOFTWARE\Wow6432Node\JavaSoft\Java Runtime Environment', 'CurrentVersion', Result );
  end;

  function InitializeSetup(): Boolean;
  var
   ErrorCode: Integer;
   JavaInstalled : Boolean;
   Result1 : Boolean;
   JREversion: String;
  begin
    if not RegGetSubkeyNames(HKLM, 'SOFTWARE\Microsoft\Microsoft SQL Server', Versions) then begin
      Result1 := MsgBox('This tool requires Microsoft SQL Server. Please download and install it and run this setup again. Do you want to download it now?',
        mbConfirmation, MB_YESNO) = idYes;
    if Result1 = false then begin
      Result:=false;
    end else begin
      Result:=false;
      ShellExec('open',
        'http://www.microsoft.com/en-us/download/details.aspx?id=29062',
        '','',SW_SHOWNORMAL,ewNoWait,ErrorCode);
    end;
    exit;
   end;
  
    // 如果作業系統是64-bit, 則先找64-bit的JRE,找不到時再找32-bit (Wow6432node/JavaSoft) 的JRE
    if IsWin64() then begin
      JREversion := GetJREVersion64();
      //MsgBox('Ver=' + JREversion, mbError, MB_OK);
      if (JREversion = '') or (JREVersion < '1.6') then begin
        JREversion := GetJREVersion32();
      end;
    end else begin
      JREversion := GetJREVersion32();
    end;
    //MsgBox('Ver2=' + JREversion, mbError, MB_OK);
    Result := true;
  
    if (JREversion = '') or (JREVersion < '1.6') then begin
      Result1 := MsgBox('This tool requires JDK 1.6 or newer to run. Please download and install the JRE and run this setup again. '#13#10
          'Do you want to download it now?' #13#10#13#10 ' (You can answer NO to install the bundled 32-bit JDK 1.7u51)',
          mbConfirmation, MB_YESNO) = idYes;
      if Result1 then begin
        Result := false;
      end else begin
        // No download JDK
      end;
    end else begin
      if (getenv('JAVA_HOME')='') then begin
        MsgBox('Please set JAVA_HOME environment variable first.' #13#13, mbInformation, MB_OK);
        result:=false ;
        exit;
      end;
    end;
  end;

##

The post 使用Inno Setup包裝安裝程式的備忘 appeared first on 簡睿隨筆.


Viewing all articles
Browse latest Browse all 897

Trending Articles