VerCmp() : Compare two software version numbers

Post your working scripts, libraries and tools
User avatar
SKAN
Posts: 612
Joined: 29 Sep 2013, 16:58

VerCmp() : Compare two software version numbers

29 May 2020, 09:41

Topic split from: https://www.autohotkey.com/boards/viewtopic.php?t=76612
According to Wiki: Software versioning
on Windows, version number 6.3.9600.16384 would be represented as hexadecimal 0x0006000325804000

Code: Select all

VerCmp(V1, V2) { ; VerCmp() for Windows by SKAN on D35T @ tiny.cc/p332208 
Return DllCall("shlwapi\StrCmp"
              ,"Str",Format("{:04X}{:04X}{:04X}{:04X}", StrSplit(V1 . "...", ".",, 5)*)
              ,"Str",Format("{:04X}{:04X}{:04X}{:04X}", StrSplit(V2 . "...", ".",, 5)*))
}

#NoEnv
#Warn
#SingleInstance, Force

MsgBox % VerCmp("13.15.16654.0", "14.1.16654")


/*
VerCmp()
                                                            
Returns  0 if versions are identical.
Returns  1 if version1 is newer
Returns -1 if version2 is newer
*/
garry
Posts: 2285
Joined: 22 Dec 2013, 12:50

Re: Compare two software version numbers

29 May 2020, 10:44

@SKAN , thank you
for test :

Code: Select all

;- Compare two software version numbers SKAN
;- https://www.autohotkey.com/boards/viewtopic.php?t=77109

#NoEnv
#Warn
#SingleInstance, Force

;V1:= "13.15.16654.0"
;V2:= "14.1.16654"		  ; 2nd one is newer
;----------------------------------------------
V1:= "13.9.16654"		  ; first one is newer
V2:= "13.08.16644.99"
;----------------------------------------------
;V1:= "13.15.16654.0"	  ; both are the same
;V2:= "13.15.16654.0"	  ; both are the same

aa:=VerCmp(v1,v2)
if (aa=0)
  msgbox, 262208,VERSION-COMPARE ,V1=%v1%`nV2=%v2%`n--------`nVersions are identical
else if (aa=1)
  msgbox, 262208,VERSION-COMPARE ,V1=%v1%`nV2=%v2%`n--------`nV1=%v1% is newer
else if (aa=-1)
  msgbox, 262208,VERSION-COMPARE ,V1=%v1%`nV2=%v2%`n--------`nV2=%v2% is newer
exitapp
;-----------------------------------------------------
VerCmp(V1, V2) { ; VerCmp() for Windows by SKAN on D35T @ tiny.cc/p332208 
Return DllCall("shlwapi\StrCmp"
              ,"Str",Format("{:04X}{:04X}{:04X}{:04X}", StrSplit(V1 . "...", ".",, 5)*)
              ,"Str",Format("{:04X}{:04X}{:04X}{:04X}", StrSplit(V2 . "...", ".",, 5)*))
}
;-----------------------------------------------------
/*
VerCmp()
--------
Returns  0 if versions are identical.
Returns  1 if version1 is newer
Returns -1 if version2 is newer
*/
;=====================================================

Return to “Scripts and Functions”

Who is online

Users browsing this forum: comvox, IzaO, malcev, rfmarves, robodesign and 159 guests