VersionCheck function

From NSIS Wiki
Jump to navigationJump to search
Author: sunjammer (talk, contrib)


Description

Please note that this function has been superseded by the VersionCompare function.

Hendri Adriaens aka Smile2Me provided this in response to a question from a forum user.

Basically it contains a small function called VersionCheck which correctly compares two dotted notation version strings (e.g. 3.5 and 3.5.1.4) and indicates which of the two is the higher version.

Hendri described it as "a function that compares 2 version numbers no matter what their structure is (eg.: 3.5 and 2.4.6.1). [snip] ... the source is a bit messy, but anyway, it works (I tested it a few times and I think it's OK, just test it yourself...)".

The Script

; function versioncheck
; by Hendri Adriaens
 
; HendriAdriaens@hotmail.com
 
outfile versioncheck.exe
name versioncheck
 
Section
  Push "3.5"
  Push "3.5.1.4"
  Call VersionCheck
  Pop $0
  MessageBox MB_OK "Number $0 was newer"
  ; output 1 - if number 1 is newer
  ;        2 - if number 2 is newer
  ;        0 - if it is the same verion
  ; output 2 in case above
SectionEnd
 
Function VersionCheck
  Exch $0 ;second versionnumber
  Exch
  Exch $1 ;first versionnumber
  Push $R0 ;counter for $0
  Push $R1 ;counter for $1
  Push $3 ;temp char
  Push $4 ;temp string for $0
  Push $5 ;temp string for $1
  StrCpy $R0 "-1"
  StrCpy $R1 "-1"
  Start:
  StrCpy $4 ""
  DotLoop0:
  IntOp $R0 $R0 + 1
  StrCpy $3 $0 1 $R0
  StrCmp $3 "" DotFound0
  StrCmp $3 "." DotFound0
  StrCpy $4 $4$3
  Goto DotLoop0
  DotFound0:
  StrCpy $5 ""
  DotLoop1:
  IntOp $R1 $R1 + 1
  StrCpy $3 $1 1 $R1
  StrCmp $3 "" DotFound1
  StrCmp $3 "." DotFound1
  StrCpy $5 $5$3
  Goto DotLoop1
  DotFound1:
  Strcmp $4 "" 0 Not4
    StrCmp $5 "" Equal
    Goto Ver2Less
  Not4:
  StrCmp $5 "" Ver2More
  IntCmp $4 $5 Start Ver2Less Ver2More
  Equal:
  StrCpy $0 "0"
  Goto Finish
  Ver2Less:
  StrCpy $0 "1"
  Goto Finish
  Ver2More:
  StrCpy $0 "2"
  Finish:
  Pop $5
  Pop $4
  Pop $3
  Pop $R1
  Pop $R0
  Pop $1
  Exch $0
FunctionEnd