To use the macros in StrFuncs.nsh, we're supposed to make a decleration that we want to use them, which in turn redefines the original macro name and prepares it for use, e.g. to use StrLoc:
!include StrFunc.nsh ${StrLoc} Section Push $0 ${StrLoc} $0 abcdefg def > MessageBox MB_OK "Result was $0" Pop $0 SectionEnd
That's fine in one .nsi or .nsh file, but if we include a second header that also does this...
!include StrFunc.nsh ${StrLoc}
... then things blow up.
!insertmacro: macro "FUNCTION_STRING_StrLoc_Call" requires 4 parameter(s), passed 0!
And we want to be able to do that so that if our headers depend on StrLoc they can be included easily in any script that might need them.
I don't know if there's an easy solution to this, since we don't have support for overloaded macros by argument count. However, it would be nice if a workaround for this design issue was available.
For example, I've currently done this:
!ifndef UnStrLoc ${StrLoc} !endif
This works because UnStrLoc is defined once ${StrLoc} is processed the first time. But its very non-obvious to maintainers what is going on. Maybe StrFuncs.nsh could maintain backwards-compatibility by continuing to support the old method, but provide a safer way to declare use of a mechanism that is safe to insert more than once, and just does nothing if it's being used for the nth time. E.g. perhaps I could write:
!include StrFunc.nsh ${UseStrLoc}
My workaround here actually doesn't work:
Thus I think there is currently no workaround currently except to create your own ${UseStrLoc} mechanisms.
I came up with this, for now:
Then you can do ${UseStrLoc} etc. safely.
Last edit: Alastair 2019-06-18
What about the
StrLoc_INCLUDED
andUnStrLoc_INCLUDED
defines?Anders,
These are not defined until the function has been both declared and actually used, so no good for this purpose. Even if there was a definition that could be used this way (and I don't see one so far), I think the better thing to do is to make StrFunc natively behave per the wrapper above, which could be acheived through modifying STRFUNC_DEFFUNC.
Compiles just fine, I don't see the problem.
Okay,
So one way or the other, can we get official support in StrFunc.nsh and a doc update? Because having to use a custom wrapper to make required forward-declarations of stock-NSIS functions work in more than one header in my projects is ugly.