NSIS版本号比较-VersionCompare Bug

本文档介绍了在NSIS脚本中遇到的VersionCompare函数比较版本号时出现的错误,以及如何通过自定义函数解决这个问题。详细讨论了错误的原因,并提供了修正后的代码实现。
摘要由CSDN通过智能技术生成

问题:

今天发现自己之前写的NSIS脚本中有个BUG,BUG的来源正是NSIS的版本号比较函数:VersionCompare

这个函数在比较"2008.3.15.1"和"2008.05.1.10"返回的不是期待的2,却是1

于是,可怕的事情发生了……

 

 

解决:

同类的几个函数,经过一番测试,采用下面这个函数。

http://nsis.sourceforge.net/Comparing_Two_Version_Numbers

 

不过原来有个小bug,就是没有初始化自己的变量,会导致多次调用时的结果出错。

于是我也修改之后直接在wiki上提交了我的修改,并在这里转载这个函数,以便下次使用。

 

  1. ;  From:http://nsis.sourceforge.net/Comparing_Two_Version_Numbers
  2. ;  Created 1/5/05 by Comperio
  3. ;
  4. ;   Usage:
  5. ;   ${VersionCheck} "Version1" "Version2" "outputVar"
  6. ;       Version1 = 1st version number
  7. ;       Version2 = 2nd version number
  8. ;       outputVar = Variable used to store the ouput
  9. ;
  10. ;   Return values:
  11. ;       0 = both versions are equal
  12. ;       1 = Version 1 is NEWER than version 2
  13. ;       2 = Version1 is OLDER than version 2
  14. ;   Rules for Version Numbers:
  15. ;       Version numbers must always be a string of numbers only.  (A dot
  16. ;       in the name is optional). 
  17. ;
  18. ;   [Variables]
  19. var P1  ; file pointer, used to "remember" the position in the Version1 string
  20. var P2  ; file pointer, used to "remember" the position in the Version2 string
  21. var V1  ;version number from Version1
  22. var V2  ;version number from Version2
  23. Var Reslt   ; holds the return flag
  24. ;   [Macros]
  25. !macro VersionCheck Ver1 Ver2 OutVar
  26.     ;   To make this work, one character must be added to the version string:
  27.     Push "x${Ver2}"
  28.     Push "x${Ver1}"
  29.     Call VersionCheckF
  30.     Pop ${OutVar}
  31. !macroend
  32. ;   [Defines]
  33. !define VersionCheck "!insertmacro VersionCheck"
  34. ;   [Functions]
  35. Function VersionCheckF
  36.     Exch $1 ; $1 contains Version 1
  37.     Exch
  38.     Exch $2 ; $2 contains Version 2
  39.     Exch
  40.     Push $R0
  41.     ;   initialize Variables
  42.     StrCpy $V1 ""
  43.     StrCpy $V2 ""
  44.     StrCpy $P1 ""
  45.     StrCpy $P2 ""
  46.     StrCpy $Reslt ""
  47.     ;   Set the file pointers:
  48.     IntOp $P1 $P1 + 1
  49.     IntOp $P2 $P2 + 1
  50.     ;  ******************* Get 1st version number for Ver1 **********************
  51.     V11:
  52.     ;   I use $1 and $
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值