Old 12th April 2005, 09:28   #1
karenk
Junior Member
 
Join Date: Apr 2005
Location: Oklahoma, USA
Posts: 4
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.
karenk is offline   Reply With Quote
Old 12th April 2005, 10:43   #2
Takhir
Major Dude
 
Join Date: Feb 2004
Location: Moscow, Russia
Posts: 1,222
.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
Takhir is offline   Reply With Quote
Old 13th April 2005, 10:54   #3
karenk
Junior Member
 
Join Date: Apr 2005
Location: Oklahoma, USA
Posts: 4
Quote:
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.
karenk is offline   Reply With Quote
Old 13th April 2005, 11:16   #4
glory_man
Senior Member
 
Join Date: Sep 2004
Location: Mogilev (Belarus)
Posts: 372
You can remove your code into !macro. And include
code:
!insertmacro "yourmacro"
in .onInit and un.onInit functions.

EDIT: or somewhere else.
glory_man is offline   Reply With Quote
Old 13th April 2005, 11:20   #5
flizebogen
Senior Member
 
Join Date: Jan 2002
Location: Berlin
Posts: 172
Create a function where you store every instruction you want to "re-use". Than call this function in the OnInit and un.OnInit Function.
flizebogen is offline   Reply With Quote
Old 13th April 2005, 11:24   #6
glory_man
Senior Member
 
Join Date: Sep 2004
Location: Mogilev (Belarus)
Posts: 372
Quote:
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.
glory_man is offline   Reply With Quote
Old 13th April 2005, 11:38   #7
flizebogen
Senior Member
 
Join Date: Jan 2002
Location: Berlin
Posts: 172
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
flizebogen is offline   Reply With Quote
Old 13th April 2005, 12:11   #8
karenk
Junior Member
 
Join Date: Apr 2005
Location: Oklahoma, USA
Posts: 4
Quote:
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?)
karenk is offline   Reply With Quote
Old 13th April 2005, 12:30   #9
flizebogen
Senior Member
 
Join Date: Jan 2002
Location: Berlin
Posts: 172
i guess to differ compile-time and run-time commands
flizebogen is offline   Reply With Quote
Old 13th April 2005, 12:48   #10
Afrow UK
Moderator
 
Afrow UK's Avatar
 
Join Date: Nov 2002
Location: Surrey, England
Posts: 8,434
! 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)...

code:
!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
Afrow UK is offline   Reply With Quote
Old 13th April 2005, 13:10   #11
karenk
Junior Member
 
Join Date: Apr 2005
Location: Oklahoma, USA
Posts: 4
Quote:
Originally posted by glory_man
You can remove your code into !macro. And include
code:
!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 ...
karenk is offline   Reply With Quote
Old 18th October 2005, 15:11   #12
pawcio
Guest
 
Posts: n/a
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.
  Reply With Quote
Old 18th October 2005, 15:20   #13
Anders
Moderator
 
Anders's Avatar
 
Join Date: Jun 2002
Location: ${NSISDIR}
Posts: 5,449
File needs the path at compile time, use a !define

IntOp $PostCount $PostCount + 1
Anders is offline   Reply With Quote
Old 18th October 2005, 23:21   #14
pawcio
Guest
 
Posts: n/a
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.
  Reply With Quote
Old 19th October 2005, 03:13   #15
Anders
Moderator
 
Anders's Avatar
 
Join Date: Jun 2002
Location: ${NSISDIR}
Posts: 5,449
everything that uses pkgname must use ${} (Name,OutFile etc)

IntOp $PostCount $PostCount + 1
Anders is offline   Reply With Quote
Old 19th October 2005, 23:15   #16
pawcio
Guest
 
Posts: n/a
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>
  Reply With Quote
Old 20th October 2005, 00:32   #17
iceman_k
NSIS Dev
 
iceman_k's Avatar
 
Join Date: Feb 2003
Location: Boston, MA, U.S.A.
Posts: 455
Var is runtime. !system is compiletime.
How do you expect them to work together?
What do expect to accomplish with:
code:

!system $pkgname "dup"



Instead try this:
code:

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.

Cheers,
Iceman_K

EclipseNSIS - An NSIS IDE for the Eclipse Platform | My contributions to the wiki
iceman_k is offline   Reply With Quote
Old 21st October 2005, 12:16   #18
pawcio
Guest
 
Posts: n/a
THX

Thank You
Now see my misteke.

Regards
  Reply With Quote
Reply
Go Back   Winamp & Shoutcast Forums > Developer Center > NSIS Discussion

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump