共享在安装程序和卸载程序之间获取输入变量的函数

时间:2016-01-15 10:49:20

标签: nsis

如何修改以下语法,以便我的库函数可以在安装程序和卸载程序中使用?到目前为止,我只设法找到一个不带输入值的函数的例子。一个很好的例子可以在这里找到:

http://nsis.sourceforge.net/Sharing_functions_between_Installer_and_Uninstaller

如果你想通过变量并仍然使用安装程序和卸载程序的函数,我还没有能够计算出语法。

/* FILE: MyFunctionLibrary.nsh */

Function MyFunction
  !define MyFunction"!insertmacro MyFunctionCall"

  !macro MyFunctionCall _VAR1
    Push "${_VAR1}"
    Call MyFunction
  !macroend

  Exch $0
  MessageBox MB_OK $0
  Pop $0

FunctionEnd

/* FILE: MyInstallerScript.nsi */

/*...mui setup stuff...*/

!include "MyFunctionLibrary.nsh"

Section Install SEC01

  ${MyFunction} "install section"

SectionEnd

Section UnInstall SEC02

  ${MyFunction} "uninstall section"

SectionEnd

/*...other stuff...*/

1 个答案:

答案 0 :(得分:0)

这里有两件事。您需要生成这两个函数,并且通常使用宏来完成。您还可以(可选)提供用于调用函数的辅助宏。

最重要的一步是创建功能:

; --- MyFunctionLibrary.nsh ---
!macro DeclareMyFunction un
Function ${un}MyFunction
Exch $0
DetailPrint "MyFunction: parameter=$0"
Pop $0
FunctionEnd
!macroend

; --- MyInstallerScript.nsi ---
!insertmacro DeclareMyFunction ""
Section
Push "Hello World"
Call MyFunction
SectionEnd

!insertmacro DeclareMyFunction "un."
Section Uninstall
Push "Hello World"
Call un.MyFunction
SectionEnd

通过仅提供宏中的函数代码,您可以使用不太常见的方法:

!macro MyFunctionCode
Exch $0
DetailPrint "MyFunction: parameter=$0"
Pop $0
!macroend

Function MyFunction
!insertmacro MyFunctionCode
FunctionEnd
Function un.MyFunction
!insertmacro MyFunctionCode
FunctionEnd

帮助您调用函数的宏只需执行x个PushCall YourFunction或un.YourFunction:

!macro DeclareMyFunction un
Function ${un}MyFunction
Exch $0
DetailPrint "MyFunction: parameter=$0"
Pop $0
FunctionEnd
!macroend

!define MyFunction "!insertmacro CallMyFunction"
!macro CallMyFunction Param1
Push "${Param1}"
!ifdef __UNINSTALL__
Call un.MyFunction
!else
Call MyFunction
!endif
!macroend

!insertmacro DeclareMyFunction ""
Section
${MyFunction} "Hello World"
SectionEnd

!insertmacro DeclareMyFunction "un."
Section Uninstall
${MyFunction} "Hello World"
SectionEnd

使用util.nsh可以抽象出一些细节:

!include Util.nsh

!macro MyFunction
Exch $0
DetailPrint "MyFunction: parameter=$0"
Pop $0
!macroend

!define MyFunction "!insertmacro CallMyFunction"
!macro CallMyFunction Param1
Push "${Param1}"
!insertmacro CallArtificialFunction MyFunction
!macroend

Section
${MyFunction} "Hello World"
SectionEnd

Section Uninstall
${MyFunction} "Hello World"
SectionEnd