- NSIS Discussion
- Global Variable Init
Archive: Global Variable Init
karenk
12th April 2005 10:28 UTC
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.
Takhir
12th April 2005 11:43 UTC
.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
karenk
13th April 2005 11:54 UTC
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. :)
glory_man
13th April 2005 12:16 UTC
You can remove your code into !macro. And include
!insertmacro "yourmacro"
in .onInit and un.onInit functions.
EDIT: or somewhere else.
flizebogen
13th April 2005 12:20 UTC
Create a function where you store every instruction you want to "re-use". Than call this function in the OnInit and un.OnInit Function.
glory_man
13th April 2005 12:24 UTC
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.
flizebogen
13th April 2005 12:38 UTC
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
karenk
13th April 2005 13:11 UTC
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?)
flizebogen
13th April 2005 13:30 UTC
i guess to differ compile-time and run-time commands
Afrow UK
13th April 2005 13:48 UTC
! 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
karenk
13th April 2005 14:10 UTC
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 ...
pawcio
18th October 2005 16:11 UTC
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.
Anders
18th October 2005 16:20 UTC
File needs the path at compile time, use a !define
pawcio
19th October 2005 00:21 UTC
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.
Anders
19th October 2005 04:13 UTC
everything that uses pkgname must use ${} (Name,OutFile etc)
pawcio
20th October 2005 00:15 UTC
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>
iceman_k
20th October 2005 01:32 UTC
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.
pawcio
21st October 2005 13:16 UTC
THX
Thank You :)
Now see my misteke.
Regards