InnoSetup---使用心得記錄


 

[Registry]
Root: HKCR; Subkey: "JOY-SECURITY"; ValueType: string; ValueData: "URL:JOY-SECURITY Protocol Handler"; Flags: uninsdeletekey

 

Root (必需的)

根鍵。必須是下列值中的一個:

HKCR (HKEY_CLASSES_ROOT)
HKCU (HKEY_CURRENT_USER)
HKLM (HKEY_LOCAL_MACHINE)
HKU (HKEY_USERS)
HKCC (HKEY_CURRENT_CONFIG)

Subkey (必需的)

子鍵名,可以包含常量。

ValueType

值的數據類型。必須是下面中的一個:

none
string
expandsz
multisz
dword
qword
binary

如果指定了 none (默認設置),安裝程序將創建一個沒有鍵值的鍵,在這種情況下,ValueData 參數將被忽略。
如果指定了 string,安裝程序將創建一個字符串 (REG_SZ) 值。
如果指定了 expandsz,安裝程序將創建一個擴展字符串 (REG_EXPAND_SZ) 值。
如果指定了 multisz,安裝程序將創建一個多行文本 (REG_MULTI_SZ) 值。
如果指定了 dword,安裝程序將創建一個32位整數 (REG_DWORD) 值。
如果指定了 qdword,安裝程序將創建一個64位整數 (REG_QDWORD) 值。
如果指定了 binary,安裝程序將創建一個二進制 (REG_BINARY) 值。

Flags

這個參數是額外選項設置。多個選項可以使用空格隔開。支持下面的選項:

createvalueifdoesntexist
當指定了這個標記,安裝程序只在如果沒有相同名字的值存在時創建值。如果值類型是 none,或如果你指定了 deletevalue 標記,這個標記無效。

deletekey
當指定了這個標記,安裝程序在如果條目存在的情況下,先將嘗試刪除它,包括其中的所有值和子鍵。如果 ValueType 不是 none,那么它將創建一個新的鍵和值。

要防止意外,如果 Subkey 是空白的或只包含反斜框符號,安裝時這個標記被忽略。

deletevalue
當指定了這個標記,安裝程序在如果值存在的情況下,先將嘗試刪除值,如果 ValueType 是 none,那么在鍵不存在的情況下,它將創建鍵以及新值。

dontcreatekey
當指定了這個標記,如果鍵已經在用戶系統中不存在,安裝程序將不嘗試創建鍵或值。如果鍵不存在,不顯示錯誤消息。

一般來說,這個鍵與 uninsdeletekey 標記組合使用,在卸載時刪除鍵,但安裝時不創建鍵。

noerror
如果安裝程序因任何原因創建鍵或值失敗,不顯示錯誤消息。

preservestringtype
這只在當 ValueType 參數是 string 或 expandsz 時適用。當指定這個標記,並且值不存在或現有的值不是 string 類型 (REG_SZ 或 REG_EXPAND_SZ),它將用 ValueType 指定的類型創建。如果值存在,並且是 string 類型,它將用先存在值的相同值類型替換。

uninsclearvalue
當卸載程序時,設置值數據為空字符 (類型 REG_SZ)。這個標記不能與 uninsdeletekey 標記組合使用。

uninsdeletekey
當卸載程序時,刪除整個鍵,包含其中的所有值和子鍵。這對於 Windows 自身使用的鍵明顯不是一個好方法。你只能用於你的應用程序特有的鍵中。

為防止意外,安裝期間如果 Subkey 空白或只包含反斜框符號,這個標記被忽略。

uninsdeletekeyifempty
當程序卸載時,如果這個鍵的內部沒有值或子鍵,則刪除這個鍵。這個標記可以與 uninsdeletevalue 組合使用

為防止意外,安裝期間如果 Subkey 空白或只包含反斜框符號,這個標記被忽略。

uninsdeletevalue
當程序卸載時刪除該值。這個標記不能與 uninsdeletekeyifempty 組合使用

注意: 在早於 1.1 的 Inno Setup 版本中,你可以使用這個標記連同數據類型 none,那么它的功能與“如果空則刪除鍵”標記一樣。這個方法已經不支持了。你必須使用 uninsdeletekeyifempty 標記實現。

添加環境變量【Code】

//添加環境變量
procedure CurStepChanged(CurStep: TSetupStep);
var
oldpath:    String;
newpath:    String;
ErrorCode: Integer;
begin
if CurStep = ssPostInstall then
begin
   RegQueryStringValue(HKEY_LOCAL_MACHINE, 'SYSTEM\CurrentControlSet\Control\Session Manager\Environment', 'Path', oldPath);
   newPath := oldPath + ';%JAVA_HOME%\bin\;';
   RegWriteStringValue(HKEY_LOCAL_MACHINE, 'SYSTEM\CurrentControlSet\Control\Session Manager\Environment', 'PATH', newPath);
   RegWriteStringValue(HKEY_LOCAL_MACHINE, 'SYSTEM\CurrentControlSet\Control\Session Manager\Environment', 'JAVA_HOME', ExpandConstant('{app}\java\jdk1.8.0_45'));
end;
end;

 

添加環境變量后記得在 setup 中配置 ChangesEnvironment=yes 通知其他應用程序從注冊表重新獲取環境變量

刪除環境變量【Code】

procedure CurUninstallStepChanged(CurUninstallStep: TUninstallStep);
var
oldpath:    String;
newpath:    String;
begin
if CurUninstallStep = usDone then
   RegDeleteValue(HKEY_LOCAL_MACHINE, 'SYSTEM\CurrentControlSet\Control\Session Manager\Environment', 'JAVA_HOME');
   RegQueryStringValue(HKEY_LOCAL_MACHINE, 'SYSTEM\CurrentControlSet\Control\Session Manager\Environment', 'Path', oldPath);
   StringChangeEx(oldPath, ';%JAVA_HOME%\bin\;', '', True);
   newPath := oldPath;
   RegWriteStringValue(HKEY_LOCAL_MACHINE, 'SYSTEM\CurrentControlSet\Control\Session Manager\Environment', 'PATH', newPath);
end;

 

安裝完成后執行腳本

[Run]
Filename: "{app}\service-install.bat"; Description: "{cm:LaunchProgram,{#StringChange('SERVICE_INSTALL', '&', '&&')}}"; Flags: shellexec postinstall waituntilterminated runascurrentuser

 

Parameters

程序的可選命令行參數,可以包含常量。

Flags

這個參數是額外選項設置。多個選項可以使用空格隔開。支持下面的選項:

32bit
Causes the {sys} constant to map to the 32-bit System directory when used in the Filename and WorkingDir parameters. This is the default behavior in a 32-bit mode install。

這個標記不能與 shellexec 組合使用。

64bit
Causes the {sys} constant to map to the 64-bit System directory when used in the Filename and WorkingDir parameters. This is the default behavior in a 64-bit mode install。

This flag can only be used when Setup is running on 64-bit Windows, otherwise an error will occur. On an installation supporting both 32- and 64-bit architectures, it is possible to avoid the error by adding a Check: IsWin64 parameter, which will cause the entry to be silently skipped when running on 32-bit Windows。

這個標記不能與 shellexec 組合使用。

hidewizard
如果指定了這個標記,向導將在程序運行期間隱藏。

nowait
如果指定了這個標記,它將在處理下一個 [Run] 條目前或完成安裝前不等待進程執行完成。不能與 waituntilidle 或 waituntilterminated 組合使用。

postinstall
僅在 [Run] 段有效。告訴安裝程序在安裝完成向導頁創建一個選擇框,用戶可以選中或不選中這個選擇框從而決定是否處理這個條目。以前這個標記調用 showcheckbox。

如果安裝程序已經重新啟動了用戶的電腦 (安裝了一個帶 restartreplace 標記的文件或如果 [Setup] 段的 AlwaysRestart 指令是 yes 引起的),選擇框沒有機會出現,因此這些條目不會被處理。

[Files] 段條目中的 isreadme 標記現在已被廢棄。如果編譯器帶 isreadme 標記的條目,它將從 [Files] 段條目中忽略這個標記,並在 [Run] 段條目列表的開頭插入一個生成的 [Run] 條目。這相生成的 [Run] 段條目運行自述文件,並帶有 shellexec,skipifdoesntexist,postinstall 和 skipifsilent 標記。

runascurrentuser
如果指定了這個標記,the spawned process will inherit Setup/Uninstall’s user credentials (typically, full administrative privileges)。

This is the default behavior when the postinstall flag is not used。

這個標記不能與 runasoriginaluser 組合使用。

runasoriginaluser
僅在 [Run] 段有效。If this flag is specified and the system is running Windows Vista or later, the spawned process will execute with the (normally non-elevated) credentials of the user that started Setup initially (i.e., the “pre-UAC dialog” credentials)。

This is the default behavior when the postinstall flag is used。

If a user launches Setup by right-clicking its EXE file and selecting “Run as administrator”, then this flag, unfortunately, will have no effect, because Setup has no opportunity to run any code with the original user credentials. The same is true if Setup is launched from an already-elevated process. Note, however, that this is not an Inno Setup-specific limitation; Windows Installer-based installers cannot return to the original user credentials either in such cases。

這個標記不能與 runascurrentuser 組合使用。

runhidden
如果指定了這個標記,它將在隱藏窗口中運行程序。請在執行一個要提示用戶輸入的程序中不要使用這個標記。

runmaximized
如果指定了這個標記,將在最大化窗口運行程序或文檔。

runminimized
如果指定了這個標記,將在最小化窗口運行程序或文檔。

shellexec
如果 Filename 不是一個直接可執行文件 (.exe 或 .com 文件),這個標記是必需的。當設置這個標記時,Filename 可以是一個文件夾或任何已注冊的文件類型 – 包括 .hlp,.doc 等。該文件將用用戶系統中與這個文件類型關聯的應用程序打開,與在資源管理器雙擊文件的方法是相同的。

按默認,當使用 shellexec 標記時,將不等待,直到生成的進程終止。
如果你需要,你必須添加標記 waituntilterminated。注意,如果新進程未生成,它不能執行也將不等待 – 例如,文件指定指定為一個文件夾。

skipifdoesntexist
如果這個標記在 [Run] 段中指定,如果 Filename 不存在,安裝程序不顯示錯誤消息。

如果這個標記在 [UninstallRun] 段中指定,如果 Filename 不存在,卸載程序不顯示“一些元素不能刪除”的警告。

在使用這個標記時, Filename 必須是一個絕對路徑。

skipifnotsilent
僅在 [Run] 段有效。告訴安裝程序如果安裝程序未在后台運行則跳過這個條目。

skipifsilent
僅在 [Run] 段有效。告訴安裝程序如果安裝程序在后台運行則跳過這個條目。

unchecked
僅在 [Run] 段有效。告訴安裝程序初始為不選中選擇框。如果用戶希望處理這個條目,可以通過選取選擇框執行。如果 postinstall 標記未同時指定,這個標記被忽略。

waituntilidle
如果指定了這個標記,它將在未輸入期間等待,直到進程等待用戶輸入,而不是等待進程終止。(調用 WaitForInputIdle Win32 函數。) 不能與 nowait 或 waituntilterminated 組合使用。

waituntilterminated
如果指定這個標記,將等待到進程完全終止。注意這是一個默認動作 (也就是你不需要指定這個標記),除非你使用了 shellexec 標記,在這種情況下,如果你要等待,需要指定這個標記。不能與 nowait 或 waituntilidle 組合使用。

安裝前卸載舊版

function InitializeSetup(): boolean;
var
bRes: Boolean;
ResultStr: String;
ResultCode: Integer;
begin
if RegQueryStringValue(HKLM, 'SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall\{4AA89D60-9EB2-4A69-B73E-67E3AC22CF8E}_is1', 'UninstallString', ResultStr) then
  begin
    MsgBox('檢測到系統之前安裝過本程序,即將卸載低版本!', mbInformation, MB_OK);
    ResultStr := RemoveQuotes(ResultStr);
    bRes := Exec(ResultStr, '/silent', '', SW_HIDE, ewWaitUntilTerminated, ResultCode);
    if bRes and (ResultCode = 0) then begin
      result := true;
      Exit;
    end else
      MsgBox('卸載低版本失敗!', mbInformation, MB_OK);
      result:= false;
      Exit;
  end;
  result := true;
end;

 

檢測服務是否存在並刪除

function DeleteService(strExeName: String): Boolean;
var
ErrorCode: Integer;
bRes: Boolean;
strCmdFind: String;
strCmdDelete: String;
begin
  strCmdFind := Format('/c sc query "%s"', [strExeName]);
  strCmdDelete := Format('/c sc stop "%s" & sc delete "%s"', [strExeName, strExeName]);
  bRes := ShellExec('open', ExpandConstant('{cmd}'), strCmdFind, '', SW_HIDE, ewWaitUntilTerminated, ErrorCode);
  if bRes and (ErrorCode = 0) then begin
      if MsgBox('檢測到 ' + strExeName + ' 服務存在,需要刪除,是否繼續?', mbConfirmation, MB_YESNO) = IDYES then begin
          bRes := ShellExec('open', ExpandConstant('{cmd}'), strCmdDelete, '', SW_HIDE, ewWaitUntilTerminated, ErrorCode);
          if bRes and (ErrorCode = 0) then begin
             MsgBox('服務 '+strExeName+' 刪除成功!', mbInformation, MB_OK);
             result := true;
             Exit;
          end else
             MsgBox('刪除失敗,請手動刪除服務 ' + strExeName, mbError, MB_OK);
             result := false;
             Exit;
      end else
      result := false;
      Exit;
  end;
  MsgBox('服務 '+strExeName+' 不存在!', mbInformation, MB_OK);
  result := true;
end;

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM