Archive: Global variables scope when !define is used


Global variables scope when !define is used
Hi all

I try to use Gloval variables with !define statement
when i receive values from extended DLL. I dont wanted to call
DLL twice or 5 time when i need some global value

When i use
CODE:--------------------------------------------
pluginDllName::Fill_S0
Pop $0
!define GLOBAL1 $0
CODE:END --------------------------------------------


if after that on some place in NSIS script
CODE:--------------------------------------------
StrCpy $0 "test2"
CODE:END --------------------------------------------

${GLOBAL1} value changes to "test2" too ?!?!? ...
I thought that statement "!define GLOBAL1 $0"
can be used for init ${GLOBAL1} with value of $0
not to Link with $0
What I suppose to do if I want to keep variable GLOBAL1
with global scope not to change when $0 changes ?
Some kind of:
CODE:--------------------------------------------
pluginDllName::Fill_S0
Pop $0
!define GLOBAL1
StrCpy ${GLOBAL1} $0
CODE:END --------------------------------------------


But in this case I receive Error when compile:
ERR: ---------------------------------------
StrCpy expects 2-2 parameters, got 1.
Usage: StrCpy $(user_var: output) str [maxlen] [startoffset]
ERR:END ---------------------------------------


!define is a compile time command. It defines a symbol as a certain string at compile time, not runtime. If you define GLOBAL as $0 everytime the compiler will encouter ${GLOBAL} it will replce it with the string $0, which later, at runtime, will be translated to whatever value it holds.

You can't create more variables than what you already have in NSIS ($0...$9, $R0...$R9). Those variables are already global, you can use them everywhere in the script, and they will maintain the same value all over it.

If you want to keep a certain value in some variable you will have to reserve a variable from the existing variables for it. $R8 for example.


10ks kichik,

it helps me a lot, I'll fix my scripts


Sorry. Do you mean to say that all variables in NSIS scripting language are global? If I used $R1 in function1, the $R1 in function2 will overwrite the first one? O gee, I hope this is not the case.


I am doing something similar and it is working fine so far.
I had problems until I finally figured out that the variable I used
($0) could not be used by any function, or else it would be overwritten. I need to test on WIN_VER in several different sections
and I didn't want to use $0, I wanted a meaningful name, like WIN_VER.

The way I see it now, it's like ${WIN_VER} is a friendly name for $0.
I'm new to NSIS, so if this is not a good way, please advise.

Call GetWindowsVersion
Pop $0 ; at this point $0 is "NT 4.0" or whatnot
!define WIN_VER $0 ; create a global variable
; MessageBox MB_OK "GetWindowsVersion ${WIN_VER}"
StrCmp ${WIN_VER} '98' ContinueAsk
StrCmp ${WIN_VER} '98 SE' Continue2
StrCmp ${WIN_VER} 'XP' Continue2

; Usage:
; Call GetWindowsVersion
; Pop $R0 ; at this point $R0 is "NT 4.0" or whatnot
Function GetWindowsVersion
Push $R0
Push $R1
... snip...

Lilla


That's right. ${WINVER} will be replaced by $0. Maybe you'd better use a variable that is not so common, like $9.

Support for real user variables ($USER) will be added very soon (it's already in the development snapshot, but not enabled by default).


Originally posted by vtsaran
Sorry. Do you mean to say that all variables in NSIS scripting language are global? If I used $R1 in function1, the $R1 in function2 will overwrite the first one? O gee, I hope this is not the case.
Yep that's how it works, like a set of registers you can manipulate.