Archive: Code works in Function Not In Macro


Code works in Function Not In Macro
Hi All,

The following code works well as a function and gives no problems when compiling the script, but when I make it a macro, it complains about the lbl_done has already been defined. I have checked the script that calls the .nsh that contains this macro and there is no other label like it. I then copy it into that very same script as a function and then call it and the complaint about the label goes away. I woould appreciate it if someone can shed some light on this issue:

P.S. I have stripped the comments and headers to keep this post short.

:: CODE ::
Function IsDotNetInstalled
Push $0
Push $1
Push $2
Push $3
Push $4

ReadRegStr $4 HKEY_LOCAL_MACHINE "Software\Microsoft\.NETFramework" "InstallRoot"
# remove trailing back slash
Push $4
Exch $EXEDIR
Exch $EXEDIR
Pop $4
# if the root directory doesn't exist .NET is not installed
IfFileExists $4 0 noDotNET
StrCpy $0 0

EnumStart:
EnumRegKey $2 HKEY_LOCAL_MACHINE "Software\Microsoft\.NETFramework\Policy" $0
IntOp $0 $0 + 1
StrCmp $2 "" noDotNET
StrCpy $1 0

EnumPolicy:
EnumRegValue $3 HKEY_LOCAL_MACHINE "Software\Microsoft\.NETFramework\Policy\$2" $1
IntOp $1 $1 + 1
StrCmp $3 "" EnumStart
IfFileExists "$4\$2.$3" foundDotNET EnumPolicy

noDotNET:
StrCpy $0 "False"
Goto lbl_done

foundDotNET:
StrCpy $0 "True"
Goto lbl_done

lbl_done:
Pop $4
Pop $3
Pop $2
Pop $1
Exch $0

FunctionEnd


Macros are blocks of code inserted into your script at compile time. This means that if you use labels in your macro, the labels will be repeated each time the macro is called.

To get around this, I use a !define command to define the current line number and then append this to the end of the macro name. This makes each label unique when the macro is inserted more than once.

Example:


!macro TestMacro
!define x ${__LINE__}
; code, code, code... blah blah blah...
; now a label:

label${x}:
; more code... blah blah blah
!undef x
!macroend