Old 26th December 2008, 15:23   #1
fontale
Junior Member
 
Join Date: Dec 2008
Posts: 2
Question Are the 20 Registers considered Global?

Greetings from a newbie!

I am writing my first ever function in NSIS and for the life of me I can't figure out from reading the manual whether I should ALWAYS push (in the beginning of the function) ALL registers used inside the function - and pop them at the end of the function?

If NSIS automatically saves all $0..$9 and $R0..$R9 variables on entering a function then restores them on returning, then I don't have to do that as a programmer.

But does NSIS really do that?

Thanks -- Phil
fontale is offline   Reply With Quote
Old 26th December 2008, 16:48   #2
JasonFriday13
Major Dude
 
JasonFriday13's Avatar
 
Join Date: May 2005
Location: New Zealand
Posts: 916
Yes, you have to manually push and pop the registers. Only push and pop the minimum you need, because it saves typing out all of them. Example:
code:
Function GetHWNDDialog
;Push in order. For the sake of clarity, I used two variables.
Push $0
Push $1

;Gets the inner dialog HWND.
FindWindow $0 "#32770" 0 $HWNDPARENT

;Get a label's HWND on the inner dialog.
GetDlgItem $1 $0 1003

;Make it black.
SetCtlColors $1 0x000000
;If you want to return a value, put it in the first
;variable you pushed, in this function its $0 (StrCpy $0 $1)

;Pop off in opposite order.
Pop $1
Pop $0 ;To return a value, don't use pop here. Use Exch to
;swap the top of the stack with the variable.
FunctionEnd


If you have returned a value from your function, you pop it off the stack so that you can use it in the rest of your script (if you want to).

Hope this helps, Jason Ross aka JasonFriday13.

"Only a MouseHelmet will save you from a MouseTrap" -Jason Ross (Me)
NSIS 3 POSIX Ninja
Wiki Profile
JasonFriday13 is offline   Reply With Quote
Old 28th December 2008, 16:48   #3
fontale
Junior Member
 
Join Date: Dec 2008
Posts: 2
Thank you Jason.

So if I understand correctly, there is a bug in the following code, right?
PHP Code:
Function DoesUserHaveAdminPrivileges
  Push $R0
  Call IsUserAdmin 
# see nsis.sourceforge.net/IsUserAdmin
  
Pop $R0
  
${If} $R0 == "false"
    
MessageBox MB_OK|MB_ICONEXCLAMATION "Must run with Admin Privileges"
    
Abort
  
${EndIf}
FunctionEnd 
Because although $R0 is pushed in the beginning and popped somewhere in the function, it is popped only to be modified by IsUserAdmin. So it must be popped again, at the end. Did I get this right?
fontale is offline   Reply With Quote
Old 28th December 2008, 18:10   #4
Yathosho
Forum King
 
Yathosho's Avatar
 
Join Date: Jan 2002
Location: AT-DE
Posts: 3,366
the DumpState is very helpful to find the problem, when it comes to pushing and popping
Yathosho is offline   Reply With Quote
Old 28th December 2008, 22:10   #5
{_trueparuex^}
Senior Member
 
{_trueparuex^}'s Avatar
 
Join Date: Dec 2005
Location: Glow
Posts: 285
Quote:
Originally posted by fontale
...
Because although $R0 is pushed in the beginning and popped somewhere in the function, it is popped only to be modified by IsUserAdmin. So it must be popped again, at the end. Did I get this right?
Yes, but not necessarily. That function is definitely using a bad practice, but if it's documented properly it is usable.

ps. All variables in NSIS are global, even if defined in a function.

PaR
{_trueparuex^} is offline   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