Archive: Function parameters


Function parameters
Hello!

I have just started to use NSIS for one of my projects. In my installer script I want to use functions and I want to pass parameters to these function. As far as I can see the standard way of doing this is using the stack. If I have a function taking three parameters it will look something like this


Function GetRegValue
Exch $R2 ; 3rd parameter, default
Exch
Exch $R1 ; 2nd parameter, name
Exch
Exch 2
Exch $R0 ; 1st parameter, sub_key

Push $R3 ; Save

ReadRegStr $R3 HKLM $R0 $R1

IfErrors use_default

StrCpy $R2 $R3
Goto end

use_default:
;StrCpy $R0 $R2

end:
Pop $R3 ; Restore
Pop $R0 ; Restore
Pop $R1 ; Restore
Exch $R2 ; Restore and return
FunctionEnd


All the Exch instructions to retrieve the arguments and store the values of the variables we need to use are kind of cryptic to say the least. Is this the best practice when it comes to parameters to functions in NSIS or is there a better way?

Regrads,
Mattias

You could use a macro and pass the three parameters:

!macro GetRegValue VAR1 VAR2 VAR3
!define Index "Line${__LINE__}"
ClearErrors
ReadRegStr $R3 HKLM $VAR1 $VAR2
IfErrors ${Index}_use_default

StrCpy ${VAR3} $R3
Goto ${Index}_end

${Index}_use_default:
StrCpy $R0 ${VAR3}

${Index}_end:
!undef Index
!macroend

However I am not sure where you want the output. If the ReadRegStr fails then you copy the 3rd variable over to the 1st. However if it succeeds you copy the output to the 3rd variable (?)

In general you would call the macro like this:
!insertmacro $R0 $R1 $R2

Hope this helps ...
CF