Skip to content
⌘ NSIS Forum Archive

macro with parameters

7 posts

isawen#

macro with parameters

Hi guys,

I would really appreciate your help over this issue I have.

I have a macro with 2 parameters like below:

!macro TestMacro _PARAM1 _PARAM2
Push $1
StrCpy $1 $OUTDIR
// ... do some stuff here with the ${_PARAM1} and ${_PARAM2}
SetOutPath $1
Pop $1
!macroend 
The problem is that when I call it like this:
${TestMacro} $1 $2 
inside the macro the ${_PARAM1} gets set to the $OUTDIR.

Is there a way to avoid this kind bad macro usage, beside:

!macro TestMacro _PARAM1 _PARAM2
!define LastKnownOutDir $OUTDIR
// ... do some stuff here with the _PARAM1 and _PARAM2
SetOutPath ${LastKnownOutDir }
!undef LastKnownOutDir 
!macroend 
Many thanks in advance,
Isawen
Afrow UK#
Well you could do:
!macro TestMacro _PARAM1 _PARAM2
!if `${_PARAM1}` == $1
!error `_PARAM1 cannot be $1`
!endif
...
Stu
Afrow UK#
And a more sophisticated solution is to switch $1 to something else if one of the macro parameters is $1, i.e.
!macro TestMacro _PARAM1 _PARAM2
!if `${_PARAM1}` == $1
!define _Var1 $2
!else
!define _Var1 $1
!endif
Push ${_Var1}
...
Pop ${_Var1}
!undef _Var1
!macroend
Here if the parameter is $1, you instead use $2 and leave $1 as it is. In fact, this is how some of the macros in Sections.nsh should be changed to use (those macros were written before !if).

Edit: I should point out that neither of these solutions will work if for example "some string containing $1" is used for _PARAM1. You are stuck there. Perhaps usage of !searchparse may work but you have to match $1 and ignore $$1 somehow.

Stu
isawen#
I'm somehow satisfied with the !define command to store the OUTDIR and to restore it at the end.
Thanks.
Afrow UK#
I am probably wrong to ask but you do realise that !define is a compile time instruction. It will not be set to the value of $OUTDIR because that is not known until run time. Storing $OUTDIR in a constant will not benefit you at all except to save you from having to repeat $OUTDIR throughout the macro. Wherever you use that constant, $OUTDIR will be inserted.

Stu
isawen#
Yes, sorry my bad. Thanks for your correction.
I didn't notice that the functionality was wrong until I changed some paths that I needed inside the macro.

Much appreciated.
Isa