Search code examples
installationnsis

Set global variable from within macro


In NSIS Script Language I get an error when I go access a global variable from within a macro.

Is my variable installDirectory truely global? How can I set the variable installDirectory from within a macro or function(if its possible within a function)?

var installDirectory

!macro IdentifyDir
   IfFileExists C:\test\9.00\ Version9 AbortInstall

   Version9:
      $installDirectory "C:\test\9.00\"
   AbortInstall:
      Abort
!macroend

Also can a Macro &/or function return a value in NSIS(or shd I just use push/pop)?

!macro IdentifyDir
   IfFileExists C:\test\9.00\ Version9 AbortInstall

   Version9:
      return "C:\test\9.00\"
   AbortInstall:
      Abort
!macroend

var installDir !insertmacro IdentifyDir # is that valid?

Solution

  • There are two issues :

    • to assign a variable, you miss one assignment instruction; for a string variable you can use StrCpy : StrCpy $installDirectory "C:\test\9.00\"

    • as stated in your another question about labels, labels are not subs but only some defined points that can be reached by flow control, thus when jumping to the Version9 label, without another jump the execution will run to the Abort statement that follows the the AbortInstall label. You should either put another label like end_of_macro: before the the !macroEnd statement and add a goto before the AbortInstall label (beware that that the macro is expanded where you put the !insertMacro so you must choose a unique label name, it is not like for sections or functions) or (more elegant) invert the two labels : in the case of the abort the execution will stop there and in the second case it will continue outside of the macro

    !macro IdentifyDir 
        IfFileExists C:\temp\9.00 Version9 AbortInstall
    AbortInstall:
        Abort
    Version9:
        StrCpy $installDirectory "C:\test\9.00\"
    !macroend
    

    For your question concerning the return value, you can use the default registers $0 to $9 and $R0 to $R9