How to compare two variables using If Else in NSIS? - nsis

Var first
Var second
Section
Strcpy $first "1.0"
Strcpy $Second "2.1"
${If} $second > $first
MessageBox MB_OK "Grater"
${Else}
MessageBox MB_OK "Smaller"
${EndIf}
SectionEnd
I have written the above code but it is showing me result as smaller. And how to compare a integer or double value coming from a text file with a predefined double or integer value?

Using LogicLib, You may compare two integers like this:
Var first
Var second
Section
StrCpy $first 1
StrCpy $Second 2
${If} $second > $first
MessageBox MB_OK "Grater"
${Else}
MessageBox MB_OK "Smaller"
${EndIf}
SectionEnd
with capital C in StrCpy. Also try removing quotes (") from numbers to make them integers.
Another way would be this:
Push $first
Push $Second
StrCpy $first 8
StrCpy $Second 2
IntCmp $first $Second Equal Val1Less Val1More
Equal:
DetailPrint "$first = $Second"
Goto End
Val1Less:
DetailPrint "$first < $Second"
Goto End
Val1More:
DetailPrint "$first > $Second"
Goto End
End:
Pop $Second
Pop $first

NSIS does not support floating point numbers in the basic instructions, you need to use the Math plugin that is part of the default install...

Related

Disable empty SectionGroup (NSIS)

In my script I have the conditional header inside a SectionGroup where sections are created at compile time.
If sections are not created SectionGroup exists and not checkable. How can I disable it if sections are not exist?
Set the name to empty to hide a section and/or group.
!include Sections.nsh
!include LogicLib.nsh
Page Components
Page InstFiles
Function HideEmptyGroup
Exch $3
Push $0
Push $1
Push $2
StrCpy $0 $3
StrCpy $2 ""
ClearErrors
loop:
SectionGetFlags $0 $1
IfErrors done
${If} $1 & ${SF_SECGRPEND}
${If} $2 = 1
SectionSetText $3 ""
${EndIf}
IntOp $2 $2 - 1
${ElseIf} $1 & ${SF_SECGRP}
IntOp $2 $2 + 1
${Else}
Goto done
${EndIf}
IntOp $0 $0 + 1
Goto loop
done:
Pop $2
Pop $1
Pop $0
Pop $3
FunctionEnd
SectionGroup /e "My Group" SID_MYGROUP
!if 0 ; Set to 1 to show section and group
Section "Thing"
SectionEnd
!endif
SectionGroupEnd
Function .onInit ; Note: This must come after the sections!
Push ${SID_MYGROUP}
Call HideEmptyGroup
FunctionEnd

NSIS window disappear

I have tried a lot of possibility with this code but it doesn't work. I need to check a VPN connection. If I run the code firs time it works, but if I don't restart the program and push the button secondly then the program just disappear. Any idea how to fix it? thanks.
Function VpnLoginWindow
${Do}
!insertmacro VpnLoginWindow
StrCpy $4 0
${If} $3 == 1
${Do}
!insertmacro VpnLoginWindow
StrCpy $4 1
StrCmp $3 "0" 0 +2
${ExitDo}
${Loop}
${EndIf}
${If} $4 == 1
${ExitDo}
${EndIf}
${Loop}
FunctionEnd
!macro VpnLoginWindow
StrCpy $5 $1
System::Get "(i.r1) iss"
Pop $R0
System::Call "user32::EnumWindows(k R0, i) i.s"
${Do}
Pop $0
StrCmp $0 "1" 0 +2
${ExitDo}
System::Call "user32::GetWindowText(ir1,t.r2,i ${NSIS_MAX_STRLEN})"
StrCpy $2 $2 7
StrCpy $3 0
${If} $2 == "SSL VPN"
StrCpy $3 1
${ExitDo}
${EndIf}
Push 1 # callback's return value
System::Call "$R0"
${Loop}
System::Free $R0
StrCpy $1 $5
!macroend
Callbacks are tricky to get right and the plug-in is a little buggy. Also, you should not use relative jumps to jump over macros like ${ExitDo}!
!include LogicLib.nsh
!macro FindVpnLoginWindow
Push "" ; Result: Window not found
System::Store S
System::Get '(p.r1, p)ir0r0'
Pop $9
System::Call 'USER32::EnumWindows(k r9, p 0)'
${Do}
${IfThen} $0 != "callback1" ${|} ${ExitDo} ${|} ; <-- adjust the callback# if required
StrCpy $0 1 ; Set callback return value, continue search
System::Call "USER32::GetWindowText(pr1, t.r2, i ${NSIS_MAX_STRLEN})"
StrCpy $2 $2 7
${If} $2 == "SSL VPN"
Pop $2 ; Throw away old result
Push $1 ; Result: HWND
StrCpy $0 0 ; Set callback return value, stop enum with 0
${EndIf}
System::Call $9 ; Return from callback function
${Loop}
System::Free $9
System::Store L
!macroend
Function BackgroundFindWindow
!insertmacro FindVpnLoginWindow
Pop $0
DetailPrint "Result:$0"
FunctionEnd
...
GetFunctionAddress $0 BackgroundFindWindow
BgWorker::CallAndWait
GetFunctionAddress $0 BackgroundFindWindow
BgWorker::CallAndWait

How to compare two string in NSIS [duplicate]

This question already has answers here:
How to compare two variables using If Else in NSIS?
(2 answers)
Closed 4 years ago.
I want to compare two strings in NSIS, For example.How to do if else condition for the below code.
ReadRegStr $R0 HKLM "${PRODUCT_UNINST_KEY}" "InstallLocation"
if ;$R0 has some values then it needs to be copied else this " $INSTDIR "$PROGRAMFILES64\${PRODUCT_NAME}""values should be assigned to INSTDIR
StrCpy $INSTDIR "$R0"
else
StrCpy $INSTDIR "$PROGRAMFILES64\${PRODUCT_NAME}"
The StrCmp and StrCmpS instructions can be used to compare strings:
StrCmp $myvar "somestring" 0 jump_to_if_not_equal
DetailPrint "myvar was somestring"
goto end
jump_to_if_not_equal:
DetailPrint "not a match"
end:
You can also use the LogicLib helper macros:
!include LogicLib.nsh
${If} $myvar == "something"
DetailPrint "match"
${Else}
DetailPrint "not a match"
${EndIf}

Product version string from an exe - nsis

I want to read the product version (optional string) from a given executable (actually from the installer I'm trying to create if it makes any difference), if it is possible at the run-time. This string will be further used to download files from a link.
Thank you very much !
NSIS does not have native support for reading anything other than VS_FIXEDFILEINFO->dwFileVersion so you have to call the Windows API directly:
; Add some version information so we have something to test
VIProductVersion 1.2.3.4
VIAddVersionKey "ProductVersion" "One Two Three Four"
VIAddVersionKey "FileVersion" "Whatever"
VIAddVersionKey "FileDescription" "Whatever"
VIAddVersionKey "LegalCopyright" "(C) Whatever"
!include LogicLib.nsh
Function GetFileVerFirstLangProductVersion
System::Store S
pop $3
push "" ;failed ret
System::Call 'version::GetFileVersionInfoSize(t"$3",i.r2)i.r0'
${If} $0 <> 0
System::Alloc $0
System::Call 'version::GetFileVersionInfo(t"$3",ir2,ir0,isr1)i.r0 ? e'
pop $2
${If} $0 <> 0
${AndIf} $2 = 0 ;a user comment on MSDN said you should check GLE to avoid crash
System::Call 'version::VerQueryValue(i r1,t "\VarFileInfo\Translation",*i0r2,*i0)i.r0'
${If} $0 <> 0
System::Call '*$2(&i2.r2,&i2.r3)'
IntFmt $2 %04x $2
IntFmt $3 %04x $3
System::Call 'version::VerQueryValue(i r1,t "\StringFileInfo\$2$3\ProductVersion",*i0r2,*i0r3)i.r0'
${If} $0 <> 0
pop $0
System::Call *$2(&t$3.s)
${EndIf}
${EndIf}
${EndIf}
System::Free $1
${EndIf}
System::Store L
FunctionEnd
Section
Push "$ExePath" ; Read our own version information in this example
Call GetFileVerFirstLangProductVersion
Pop $0
DetailPrint "ProductVersion=$0"
SectionEnd

compound string comparison in nsis

How do I do a compound string comparison in NSIS?
essentially something like: if ( str1 == "" || str2 == "" ) ...
strcpy $1 "c:\foo"
strcpy $2 "d:\bar"
${if} strcmp $1 ""
${orif} strcmp $2 ""
MessageBox MB_OK "one or both are empty"
${else}
messagebox mb_ok "both are not"
${endif}
SectionEnd
StrCmp is the low-level instruction at the heart of NSIS string comparisons but when using the LogicLib you must use the correct operators: ==, !=, S== or S!= (all of them are listed at the top of LogicLib.nsh and the case-insensitive operators use StrCmp internally)
!include LogicLib.nsh
Section
StrCpy $1 "c:\foo"
StrCpy $2 "d:\bar"
${If} $1 == ""
${OrIf} $2 == ""
MessageBox MB_OK "one or both are empty"
${Else}
MessageBox MB_OK "both are not"
${EndIf}
SectionEnd

Resources