Archive: Global Variable Init


Global Variable Init
Hi! I'm a real newbie (been pouring over NSIS for about 12 hours). And I've finally stumped myself. :)

I understand all variables are global. Var statements are fine outside Sections.

Now I'd like to specify variable's value once, then use it in several sections. What's the best way to go about this?

Adding code like StrCpy $VarName "Value" doesn't work outside a section. Can I create a section that always executes? Is there another way?

Thanks!

P.S. !define seems to do the job, if the value is static. But sometimes the value must be determined at runtime.


.onInit function may be a good place to set variable value. After this you can use it in any section.
NSIS Documentation, 4.7.2.1.2 .onInit


Originally posted by Takhir
.onInit function may be a good place to set variable value. After this you can use it in any section.
NSIS Documentation, 4.7.2.1.2 .onInit
Thanks for the reply and suggestion! But it looks like .onInit is only called when the installer is performing an installation. During an uninstall, un.onInit is called instead. Is that right?

If so, I'd have to duplicate my code in the .onInit and un.OnInit functions, something I'd like to avoid.

What I'm hoping to find is a way to set my values in one "easy to maintain" location, so I don't have to rely on my old brain to tell me to reproduce each change in another location. :)

You can remove your code into !macro. And include

!insertmacro "yourmacro"
in .onInit and un.onInit functions.

EDIT: or somewhere else.

Create a function where you store every instruction you want to "re-use". Than call this function in the OnInit and un.OnInit Function.


Originally posted by flizebogen
Create a function where you store every instruction you want to "re-use". Than call this function in the OnInit and un.OnInit Function.
In this case two functions must be created. Because uninstaller functions must contain un.-prefix.

you're right - sorry for the wrong posting.

The docs says that it is not possible for the installer to call an uninstaller function vice-versa


Originally posted by flizebogen
The docs says that it is not possible for the installer to call an uninstaller function vice-versa
And I can confirm the docs are correct. :)

For now, I have two functions, AppInit and un.AppInit. They contain identical contents. So far, I haven't found any way to breach the wall between the Install and Uninstall sides, except via !defines.

(BTW, just curious -- what's the significance of the ! in front of define and insertmacro?)

i guess to differ compile-time and run-time commands


! declares that it is a compile-time command (just a way of breaking up the code and making it more user-friendly).

Just use a !macro (as glory_man has already suggested)...

!macro setVars
StrCpy $Var1 "blah"
StrCpy $Var2 "blah"
!macroend

Function .onInit
!insertmacro setVars
FunctionEnd

Function un.onInit
!insertmacro setVars
FunctionEnd


If you are worried about it taking up more memory, then don't because it really doesn't take up a more noticeable amount.

-Stu

Originally posted by glory_man
You can remove your code into !macro. And include
!insertmacro "yourmacro"
in .onInit and un.onInit functions.
Thanks! (and thanks to Afro UK for pointing out I'd somehow missed this gem!)

That seems like the best solution. Off to implement it ...

global vars
Hi
What if I would like use my global vars not in the section?
And How can I use it in File?
Do I have to put my funcion and call to function in the section or just !insertmacro "setVars" and how can I do it not in the section?

for example
<code>
Var "pkgname"

!macro setVars
StrCpy $pkgname "mojapaczka"
!macroend

Name "$pkgname"
InstallDir "c:\$pkgname\$pkgname"

Section "install"
!insertmacro setVars
SetOutPath $INSTDIR
File /a /r c:\packages\$pkgname\*
SectionEnd
</code>
=cut

Any advice would be appreciated.


File needs the path at compile time, use a !define


global vars
THX for advice :)

but I still can't find the way to write it properly.
<code>
!define $pkgname "mysoft234"
!define $soft "monday"

Name $pkgname
OutFile d:\nsis\$pkgname-bedzie.exe
Section "ins"
SetOutPath $INSTDIR
File /r "D:\nsis\${soft}\${pkgname}\*"
SectionEnd

I assume that this is wrong, is't it?
maybe:
my $var="value";
;)
Can I ask for anather prompt please.


everything that uses pkgname must use ${} (Name,OutFile etc)


still won't work
thx for reply
but still it seems to be a problem.
Compiler just ignore my vars, said that File is missing
and my OutFile have ${var} in name instead of myname-name.exe

<code>
; eh kurwa...
Var pkgname
;
!system $pkgname "dup"

Name ${pkgname}
OutFile d:\nsis\${pkgname}bedzie.exe

InstallDir "d:\kupa"

Section "install"

SetOutPath $INSTDIR
;StrCpy ${pkgname} "dup" '' ''
File /r "D:\nsis\${pkgname"

SectionEnd

</code>


Var is runtime. !system is compiletime.
How do you expect them to work together?
What do expect to accomplish with:


!system $pkgname "dup"


Instead try this:

Name ${pkgname}
OutFile d:\nsis\${pkgname}\bedzie.exe

InstallDir "d:\kupa"

Section "install"

SetOutPath $INSTDIR
File /r "D:\nsis\${pkgname}"

SectionEnd


Then call MakeNSIS.exe with /Dpkgname=dup option.
Incidentally, if your outfile is going to d:\nsis\${pkgname}\bedzie.exe, then everytime you build the installer, File /r "D:\nsis\${pkgname}" is going to pick up the previous build.

THX
Thank You :)
Now see my misteke.

Regards