Archive: pass a variable to a macro


pass a variable to a macro
Hi,

I am trying to pass a variable to a macro and have the macro have a condition based on that variable.
I tried usign VAR and !define and it didnt work.

I need to have the macro do different things based on the variables sent to it.
how can I do it ?

this will not work:

Var /GLOBAL XYZ
StrCpy $XYZ "New"
MessageBox MB_OK "$XYZ" ; I get "New"
!define CONST_XYZ "$XYZ" ; not working


insertmacro MACRO_TEST $XYZ ; Not working

!insertmacro MACRO_TEST "${$XYZ}" ; not wokring



!macro MACRO_TEST varXYZ

!If ${varXYZ} != "New"

;....
!endif

!macroend


I've said this countless times and no doubt I will have to carry on saying it. You are confusing run time (variables) with compile time (macros, defines etc). Hopefully I need not say any more...

Edit: Tip; ${If} not !if.

Stu


the problem is that I have a VAR and I want to pass it to the macro.

VAR XYZ "New"
!define XYZ_CON "$XYZ"

!insertmacro SEARCH_OFFERING "${XYZ_CON}"
!insertmacro SEARCH_OFFERING "$XYZ"
!insertmacro SEARCH_OFFERING $XYZ

none of the above will compile as

!insertmacro SEARCH_OFFERING "New"

why ???


Why?? Because the macro is compiled long before the code is run. The variable doesn't have a value until the code is run; the macro cannot be compiled with a value that does not exist.


I thought that you are abusing or misapplying some commands. Like Afrow UK & demiller9 said, the commands start with an exclamation are compiling commands. They are only used to tell compiler which lines to compile. The macro is similar, it will be expanded. Thought you see maro is short when using in script, but in fact the script is the same.

Example 1:
!define VAR1 AAA
!define VAR2 ${VAR1}BBB
Equals to you !define VAR2 AAABBB

Example 2:
!macro MYMACRO PARAM1 PARAM2 PARAM3
MessageBox MB_OK `${PARAM1}`
MessageBox MB_YESNO `${PARAM2}`
MessageBox MB_OKCANCEL `${PARAM3}`
!macroend
When using:
!insertmacro MYMACRO A B C
!insertmacro MYMACRO D E F
It completely equals to 6 lines:
MessageBox MB_OK `A`
MessageBox MB_YESNO `B`
MessageBox MB_OKCANCEL `C`
MessageBox MB_OK `D`
MessageBox MB_YESNO `E`
MessageBox MB_OKCANCEL `F`

In NSIS script, !define and !macro command only replace the text simplely, except some especial pre-defined constants, like !define a constant to %x with "/date" option. The command !define is commonly used to define a name to a fixed constant value, so that you can remember it easily rather than an irregular value, or define a value that used in your script mutiple times. This is almost the same to C++.

Define a easy-remembered name:
!define WM_CLOSE 0x0010
Modify this line will change all values of ${PI} in script rather than modify every one:
!define PI 3.14

Fisrt, "var" command can't assign a value.
VAR XYZ "New"
Second, you define XYZ_CON to $XYZ means tell the compiler that the value of constant ${XYZ_CON} is $XYZ.
!define XYZ_CON "$XYZ"

So when compiling the !insertmacro SEARCH_OFFERING "${XYZ_CON}" line, ${XYZ_CON} would be replaced with $XYZ simplely. That is the following 3 lines are the same.
!insertmacro SEARCH_OFFERING "${XYZ_CON}"
!insertmacro SEARCH_OFFERING "$XYZ"
!insertmacro SEARCH_OFFERING $XYZ


To pass variables to a macro, you can use CallArtificialFunciton macro in Util.nsh, or use a function call, and you must use NSIS stack for exchange values.