避免NSIS中重复的功能代码

避免NSIS中重复的功能代码,nsis,Nsis,目前,我有以下脚本代码 Section "Uninstall" ... ... Call un.DeleteDirIfEmpty SectionEnd Function GetJRE ; Call must not be used with functions starting with "un." in the non-uninstall sections. Call FunctionEnd Function un.DeleteDirIfEmpty ... ... F

目前,我有以下脚本代码

Section "Uninstall"
...
...
Call un.DeleteDirIfEmpty 
SectionEnd


Function GetJRE
    ; Call must not be used with functions starting with "un." in the non-uninstall sections.
    Call
FunctionEnd


Function un.DeleteDirIfEmpty
...
...
FunctionEnd

Function DeleteDirIfEmpty
...
...
FunctionEnd
注意,我需要提供两个版本的DeleteDirIfEmpty,以便在非卸载部分和卸载部分执行相同的操作

他们的代码是相同的,只是命名不同
un.DeleteDirIfEmpty
DeleteDirIfEmpty


怎么可能只有一个函数,但可由任何部分调用?

请查看\Include\Util.nsh,它用于将宏转换为函数:

!include Util.nsh

!macro MyFunction
MessageBox mb_ok "Hello World"
!macroend
!define MyFunction "${CallArtificialFunction} MyFunction"

Section
${MyFunction}
SectionEnd

注意:要删除空目录,只需使用
RMDir
(不带/r开关)

这也帮助我理解。它提供了一个示例,演示如何在安装程序和卸载程序之间共享功能。比如说,您有一个应该共享的函数,名为
myfunc
,然后您也可以创建一个宏从卸载程序调用它。引用链接:

; Name of our installer.
Name "Function Sharing Example"
OutFile "FunctionShareExample.exe"
InstallDir "$PROGRAMFILES\Function Sharing Example\"

; We need some pages.
Page directory
Page instfiles
; And uninstaller pages.
UninstPage uninstconfirm
UninstPage instfiles

; Show the details.
ShowInstDetails show
ShowUninstDetails show

; ******************* The shared function. *******************
!macro MYMACRO un
  Function ${un}myfunc
    MessageBox MB_OK "This is the function ${un}myfunc."
    DetailPrint "Very ${un}funny text."
    DetailPrint "More ${un}funny text."
  FunctionEnd
!macroend

; Insert function as an installer and uninstaller function.
!insertmacro MYMACRO ""
!insertmacro MYMACRO "un."

Section "Install"
  ; ******************* Call the installer function. *******************
  Call myfunc

  SetOutPath "$INSTDIR"
  ; Write an uninstaller.
  WriteUninstaller "$INSTDIR\uninstall.exe"
  ShowWindow $HWNDPARENT 6
  ; Show the install directory, so you can run the uninstaller straight away.
  ExecShell open "$INSTDIR"
  Sleep 1000
  ShowWindow $HWNDPARENT 9
SectionEnd

Section "Uninstall"
  ; ******************* Call the un.installer function. *******************
  Call un.myfunc

  ; Clean up install directory (delete it).
  Delete "$INSTDIR\uninstall.exe"
  RMDir "$INSTDIR"
SectionEnd

“要删除空目录,只需使用RMDir(不带/r开关)”也是一个很好的提示。谢谢:)嗨,安德斯,你能用一个如何调用共享函数的例子来编辑你的答案吗?当我从节调用
${MyFunction}
时,会出现错误“从节调用的函数命令无效”或类似错误。同样,我也不确定
${CallArtificialFunction}
在您的代码中扮演什么角色。我对它感兴趣,因为您的解决方案看起来比我从本文提供的解决方案更干净。你推荐哪种方法?与强大或灵活相比,我更喜欢不太冗长的可读性。谢谢虽然此链接可以回答问题,但最好在此处包含答案的基本部分,并提供链接以供参考。如果链接页面发生变化,只有链接的答案可能会变得无效。@rakhi4110好吧,我会包括要点,但我觉得我不会仅仅因为复制而公平对待原始作者。你仍然会提供链接,并因此而信任原始作者。只需在此处提供摘录,以确保每个人都可以获得答案,即使链接已关闭/过期/迁移。