Sharing functions between Installer and Uninstaller

From NSIS Wiki
Jump to navigationJump to search
Author: JasonFriday13 (talk, contrib)


Description

Don Tommaso wrote in this thread that he didn't want to write out his functions for both the installer and uninstaller. So here is an example that makes it easier (and quicker) to share functions between installer and uninstaller.
Thanks to kichik for the example code.

Code Example

The original code by kichik:

!macro myfunc un
Function ${un}myfunc
  Call ${un}someotherfunc
  DetailPrint something
FunctionEnd
!macroend
 
!insertmacro myfunc ""
!insertmacro myfunc "un."

And an example script:

; 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
 
; Create 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