显示产品版本?

时间:2012-05-16 05:30:48

标签: nsis

我的产品有w.x.y.z版本。我对该产品的升级有w.x.y.z + 1。但要求是在升级版本的第四个字段之前,即; z不应该被检查...即; myproduct不应该考虑z字段.....请回答我的回答。

2 个答案:

答案 0 :(得分:1)

您可以从注册表中读取App Version并将其保存为Variable,然后将其修剪为特定数量的字符。查看http://nsis.sourceforge.net/TrimText:_Trim_text_e.g._to_fit_in_a_label

以下脚本适用于ANSI NSIS如果您安装了此类版本,请相应更改ReadRegStr

Name "TrimTest"
OutFile "TrimTest.exe"

!include "TextFunc.nsh"
!include "nsDialogs.nsh"
!include "LogicLib.nsh"
!include "WinMessages.nsh"
!include "WordFunc.nsh"
!insertmacro VersionCompare

Function TrimText
 Exch $R0 ; char
 Exch
 Exch $R1 ; length
 Exch 2
 Exch $R2 ; text
 Push $R3
 Push $R4

 StrLen $R3 $R2
 IntCmp $R3 $R1 Done Done

 StrCpy $R2 $R2 $R1

 StrCpy $R3 0
  IntOp $R3 $R3 + 1
  StrCpy $R4 $R2 1 -$R3
  StrCmp $R4 "" Done
  StrCmp $R4 $R0 0 -3

  IntOp $R3 $R3 + 1
  StrCpy $R4 $R2 1 -$R3
  StrCmp $R4 "" Done
  StrCmp $R4 $R0 -3

  IntOp $R3 $R3 - 1
  StrCpy $R2 $R2 -$R3
  StrCpy $R2 $R2

 Done:
 StrCpy $R0 $R2
  Pop $R4
  Pop $R3
  Pop $R2
  Pop $R1
  Exch $R0 ; output
FunctionEnd

!macro TrimText Text Length Char Var
  Push "${Text}"
  Push "${Length}"
  Push "${Char}"
   Call TrimText
  Pop "${Var}"
!macroend

!define TrimText "!insertmacro TrimText"

Section
  ReadRegStr $0 HKLM "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\NSIS ANSI" "DisplayVersion"
  ${TrimText} $0 7 "-" $R0 
;set max lenght to 7, where only 6 chars are needed, as trimmer will stop on - char
;in your case you will probably want to trim to first . from right side
  ;${TrimText} "string" "max_length" "up_to_char" "$out_var"
  MessageBox MB_ICONEXCLAMATION "Line: $R0"
  ;Input $0 from registry should be "2.46.4-ANSI" if the latest ANSI NSIS is installed
  ;$R0 after trimm will be "Line: 2.46.4"
${VersionCompare} $R0 "2.46.6" $R2
  ${If} $R2 == 1 ;if 2.46.6 is lower that $R0
    MessageBox MB_ICONEXCLAMATION "Your App Version is newer than this version of update"
  ${EndIf}
  ${If} $R2 == 0 ;versions are equal
    MessageBox MB_ICONEXCLAMATION "Your App Version is equal to this version of update"
  ${EndIf}      
  ${If} $R2 == 2 ;if 2.46.6 is higher that $R0
    MessageBox MB_ICONEXCLAMATION "Your App Version is older and will be updated"
  ;here you can write your update section
  ${EndIf}
SectionEnd

答案 1 :(得分:1)

在这种情况下,这可以是一个解决方案(基于http://nsis.sourceforge.net/Sort_String_2)。 我创建了Test Registry Key只是为了检查{3.1.123 4.0.112 4.1.119}。请替换您的应用程序使用的那个。

Name "TrimTest2"
OutFile "TrimTest2.exe"

!include "TextFunc.nsh"
!include "nsDialogs.nsh"
!include "LogicLib.nsh"
!include "WinMessages.nsh"
!include "WordFunc.nsh"
!insertmacro VersionCompare

Function AdvStrSort
  Exch $0 ; input string
  Exch
  Exch $1 ; count to get part
  Exch
  Exch 2
  Exch $2 ; get ammount of chunks from end
  Exch 2
  Push $3
  Push $4
  Push $5
  Push $6
  Push $7
  StrCpy $0 " $0"
  StrCpy $3 0
  StrCpy $4 0
  loop:
    IntOp $3 $3 - 1
    StrCpy $6 $0 1 $3
    StrCmp $6 "" skip
    StrCmp $6 " " roger ; to change chunk seperators, edit this (" ")
    Goto loop
  roger:
    StrCpy $7 $0 "" $3
    IntOp $4 $4 + 1
    StrCmp $4 $2 0 loop
    StrCmp $1 $2 0 +3
    StrCpy $0 $7
    Goto end
  skip:
    StrCpy $4 0
    StrLen $5 $7
  top:
    IntOp $4 $4 + 1
  loop2:
    IntOp $5 $5 - 1
    StrCpy $3 $7 1 -$5
    StrCmp $3 "" end
    StrCmp $3 " " 0 loop2 ; to change chunk seperators, edit this too (" ")
    StrCmp $4 $1 0 top
    StrCpy $0 $7 -$5
  end:
    StrLen $1 $0
    IntOp $1 $1 - 1
    StrCpy $0 $0 $1 -$1
  Pop $7
  Pop $6
  Pop $5
  Pop $4
  Pop $3
  Pop $2
  Pop $1
  Exch $0 ; output string
FunctionEnd

Section
  ReadRegStr $R1 HKLM "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\UserApplicationName" "Version"
Push 1 ; get text after 1st chunk from string end
 Push 1 ; get 1 chunk before the 1st
  Push "$R1" ; input string (string that has spaces)
    Call AdvStrSort
  Pop $R0
MessageBox MB_ICONEXCLAMATION "Result: $R0"
${VersionCompare} $R0 "5.0.114" $R2
 ;MessageBox MB_ICONEXCLAMATION "Result: $R2"
  ${If} $R2 == 1 ;if 5.0.114 is lower that $R0
    MessageBox MB_ICONEXCLAMATION "Your App Version is newer than this version of update"
  ${EndIf}
  ${If} $R2 == 0 ;versions are equal
    MessageBox MB_ICONEXCLAMATION "Your App Version is equal to this version of update"
  ${EndIf}
  ${If} $R2 == 2 ;if 5.0.114 is higher that $R0
    MessageBox MB_ICONEXCLAMATION "Your App Version is older and will be updated"
  ;here you can write your update section
  ${EndIf}
SectionEnd