Version comparison in NSIS installer - nsis

I have to compare the versions of current file and already the installed in NSIS installer. I understand the version numbers are in string but I read at couple of places that the NSIS automaticaly converts the strings to intergers if needed in logic operations.
Just for the test purpose, I wrote the following script:
Var Test1
Var Test2
section
StrCpy $Test1 "4.3.1.50245"
StrCpy $Test2 "4.2.1.50245"
${If} $Test1 > $Test2
MessageBox MB_ICONSTOP "$Test1 is bigger than $Test2"
${Else}
MessageBox MB_ICONSTOP "$Test2 is bigger than $Test1"
${EndIf}
Quit
sectionEnd
PROBLEM: It gives me the result 4.2.1.50245 is greated than 4.3.1.50245. However, it gives me correct results if I try to compare 4.2.1.50245with 3.2.1.50245 (or if I compare 50245and 40256 etc.)

Check out the following function, Version Compare
http://nsis.sourceforge.net/VersionCompare
Section
${VersionCompare} "1.1.1.9" "1.1.1.01" $R0
; $R0="1"
SectionEnd
; Result:
; $var=0 Versions are equal
; $var=1 Version1 is newer
; $var=2 Version2 is newer
If not, another method would be to implement an Explode of the string and then compare the Major, Minor, Revision, ... portions. But that may be overkill:
http://nsis.sourceforge.net/Explode

Related

Signing NSIS Uninstaller from Linux or Mac

I am porting our NSI installer to Linux and Mac instead of Windows to better integrate with our Maven build system.
We need to sign our installer and uninstaller. This was done as suggested at http://nsis.sourceforge.net/Signing_an_Uninstaller, but I just realized that it tries to run the tempinstaller to force it to produce the uninstaller.exe which can then be signed.
Obviously this trick doesn't work too well on *Nix systems and make this part of the process non-portable.
Does anyone has a better solution. I'm no expert at NSIS and wondering if there is a clever way to get the uninstall.exe so that it can be signed?
I don't think there is a real solution to this.
The installer and uninstaller uses the same exe code and only checks a flag (FH_FLAGS_UNINSTALL in firstheader) on startup to see if it is a uninstaller. Just flipping this bit is not enough though, the program would fail the CRC check and even if you bypass that the uninstaller data is compressed so you would have to decompress that to the correct location in the file. To actually accomplish this you would have to write a custom tool. You can see this operation in the NSIS source in exec.c if you search for EW_WRITEUNINSTALLER.
We need to sign our installer and uninstaller. This was done as suggested at http://nsis.sourceforge.net/Signing_an_Uninstaller, but I just realized that it tries to run the tempinstaller to force it to produce the uninstaller.exe which can then be signed. [...] this trick doesn't work too well on *Nix systems and make this part of the process non-portable.
If you exploit a stub installer for uninstall operations (no payload), this appears to be possible.
It will spawn an uninstall.exe process from the $TEMP folder, which is then capable of deleting $INSTDIR.
This script will create a stub (un)installer which can then be Authenticode Signed. It will compile on Windows, MacOS and Linux.
Caveats:
You'll have to manually bundle this into the installer (trivial)
You'll have to manage your own uninstall registry entries (trivial)
The look and feel may not match NSIS's default for uninstallers
You'll see the installer open twice (first from $INSTDIR, second from $TEMP). This is a child process which allows uninstall.exe to delete itself, similar to how NSIS does it in the Section "Uninstall".
You'll need a secondary .nsi script dedicated to uninstall operations, cumbersome if you have a lot of shared logic between your install/uninstall sections.
Worse, you'll have to AVOID the "Uninstall" section title, as you'll be placed into the same problem as the OP when that bytecode is generated.
When explicitly running from $TEMP some relative file logic will be incorrect. The example passes these back in as a $DELETE_DIR, $DELETE_EXE respectively.
The code:
!include MUI2.nsh
!include x64.nsh
!include LogicLib.nsh
!include FileFunc.nsh
!include WinMessages.nsh
!define MUI_PRODUCT "My App"
!define MUI_VERSION "1.0.0"
; Masquerade the title
!define MUI_PAGE_HEADER_TEXT "Uninstall My App"
!define MUI_PAGE_HEADER_SUBTEXT "Remove My App from your computer"
!define MUI_INSTFILESPAGE_FINISHHEADER_TEXT "Uninstallation Complete"
!define MUI_INSTFILESPAGE_FINISHHEADER_SUBTEXT "Uninstall was completed successfully."
!insertmacro MUI_PAGE_INSTFILES
!insertmacro MUI_LANGUAGE "English"
!insertmacro GetParameters
RequestExecutionLevel admin
CRCCheck On
OutFile "uninstall.exe"
Name "Uninstall"
Var /GLOBAL RESPAWN
Var /GLOBAL DELETE_DIR
Var /GLOBAL DELETE_EXE
Section
; Masquerade as uninstall
SendMessage $HWNDPARENT ${WM_SETTEXT} 0 "STR:Uninstall"
${GetParameters} $0
${GetOptions} "$0" "/RESPAWN=" $RESPAWN
${GetOptions} "$0" "/DELETE_DIR=" $DELETE_DIR
${GetOptions} "$0" "/DELETE_EXE=" $DELETE_EXE
${If} $RESPAWN != ""
; We're running from $TEMP; Perform the uninstall
!define yay "We're running from $EXEPATH, yay, we can remove the install directory!$\n$\n"
!define myvars "$\tRESPAWN$\t$RESPAWN$\n$\tDELETE_EXE$\t$DELETE_EXE$\n$\tDELETE_DIR$\t$DELETE_DIR"
MessageBox MB_OK "${yay}${myvars}"
; Your uninstall code goes here
; RMDir /r $DELETE_DIR\*.*
; Delete "$DESKTOP\${MUI_PRODUCT}.lnk"
; Delete "$SMPROGRAMS\${MUI_PRODUCT}\*.*"
; RmDir "$SMPROGRAMS\${MUI_PRODUCT}"
; Delete Uninstaller And Unistall Registry Entries
; DeleteRegKey HKEY_LOCAL_MACHINE "SOFTWARE\${MUI_PRODUCT}"
; DeleteRegKey HKEY_LOCAL_MACHINE "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\${MUI_PRODUCT}"
; Remove the old version of ourself
ClearErrors
Delete $DELETE_EXE
IfErrors 0 +3
MessageBox MB_OK "File could NOT be deleted: $DELETE_EXE"
Goto +2
MessageBox MB_OK "File was successfully deleted: $DELETE_EXE"
; Remove ourself from $TEMP after reboot
Delete /REBOOTOK $EXEPATH
; ${If} ${RunningX64}
; ${EnableX64FSRedirection}
; ${EndIf}
SetDetailsPrint textonly
DetailPrint "Completed"
${Else}
; We're NOT running from $TEMP, copy to temp and respawn ourself
GetTempFileName $0
CopyFiles "$EXEPATH" "$0"
Exec '"$0" /RESPAWN=1 /DELETE_DIR="$EXEDIR" /DELETE_EXE="$EXEPATH"'
Quit
${EndIf}
SectionEnd
Function .onInit
; ${If} ${RunningX64}
; SetRegView 64
; ${DisableX64FSRedirection}
; ${EndIf}
FunctionEnd

get hard disk drive name in which windows OS installed using NSIS

I need to get hard disk drive letter like C: or D: or any in which the OS is installed on any windows.
Kindly help me get out of this.
Section
StrCpy $0 $sysdir 3
DetailPrint $0
SectionEnd
$SYSDIR is the Windows system directory (usually C:\Windows\System or C:\WinNT\System32, but that's detected at runtime).
StrCpy has the syntax StrCpy user_var(destination) str(source) [maxlen] [start_offset] where maxlen and start_offset are optional.
So the code above, copies the first three letters from $SYSDIR into the $0 variable.

Checking for Registry in NSIS

I'm using NSIS to make an executable for a project I'm doing in Visual C++ 2012 Express. Because I'm a beginner, I started with the Setup Wizard in HM NIS Edit.
I'm trying to bundle the Visual C++ 2012 Redistributable with my program, but every time the installer is re-ran, the Redistributable pops up with a "Repair" & "Remove" option, and that looks annoying.
So I decided to write a little bit of NSIS script, and this is the start of the script so far:
Var STR
Section CheckForReg
ClearErrors
ReadRegDWORD $0 HKLM "SOFTWARE\Classes\Installer\Dependencies\{8e70e4e1-06d7-470b-9f74-a51bef21088e}" "Version"
ifErrors 0 Blank
StrCpy $STR "$INSTDIR\vcredist_x86.exe"
GoTo End
Blank:
StrCpy $STR ""
End:
SectionEnd
This piece is called at the very start of the script, and the global variable STR is applied to:
!define MUI_FINISHPAGE_RUN $STR
Shortly after.
Obviously this is a really silly way to do it, but I don't need too much out of it.
The issue is that the CheckForReg always thinks the registry doesn't exist, and doesn't move to the label Blank. As a note, I'm manually checking the registry every time, and the registry entry looks like:
[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\Installer\Dependencies\{8e70e4e1-06d7-470b-9f74-a51bef21088e}]
"Version"="11.0.51106.1"
"DisplayName"="Microsoft Visual C++ 2012 Redistributable (x86) - 11.0.51106"
So the question is: Where am I going wrong with this? It looks very simple, but obviously I have something backwards.
-- Removed Code Dump
You can only read DWORDs with ReadRegDWORD!
!include LogicLib.nsh ; So we don't have to use all these labels
StrCpy $STR ""
ReadRegStr $0 HKLM "SOFTWARE\Classes\Installer\Dependencies\{8e70e4e1-06d7-470b-9f74-a51bef21088e}" "Version"
${If} $0 == ""
StrCpy $STR "$INSTDIR\vcredist_x86.exe"
${EndIf}

How to detect windows 32bit or 64 bit using NSIS script?

I have written nsis script for java project.I have Batch file in my project.I have written batch file for commonly windows 32bit and 64 bit.After installing i have started batch file automatically using Exec command.Its woks fine in 32bit windows.but the same time this is not worked well in 64 bit.so i suspect that before installing i should check whether windows is 32 bit or 64 bit version.please share your views how to check?
For future lazy googlers - A small snippet:
Include this:
!include x64.nsh
And use this if:
${If} ${RunningX64}
# 64 bit code
${Else}
# 32 bit code
${EndIf}
Use the RunningX64 macro in the x64.nsh header:
!include LogicLib.nsh
!include x64.nsh
Section
${If} ${RunningX64}
DetailPrint "64-bit Windows"
${Else}
DetailPrint "32-bit Windows"
${EndIf}
SectionEnd
Here's what I use most of the time without the need for x64.nsh
Var Bit
System::Call "kernel32::GetCurrentProcess()i.s"
System::Call "kernel32::IsWow64Process(is,*i.r0)"
StrCmpS $0 0 +3
StrCpy $Bit 64
Goto +2
StrCpy $Bit 32
Now $Bit holds either 64 or 32 which can be used like this:
${If} $Bit == 64
...64-bit code..
${Else}
..32-bit code...
${EndIf}
Or
StrCmpS $Bit 64 SixtyFour ThirtyTwo
SixtyFour:
...
Goto End
ThirtyTwo:
...
End:
I used StrCmpS as I believe it's a hair faster. Lol. Hope this helps! =)

How do I use/include StrContains in NSIS

Do I need to include a specific .nsh library or function definition to use the function 'StrContains' in NSIS?
I have looked for a download for the library but I cant seem to find it?
When I go to compile this code I get the compile error: "Invalid command: ${StrContains}"
!include "LogicLib.nsh"
# Compile error below
!macro test
${StrContains} $0 $1 "abc"
!macroend
Section
DetailPrint ""
SectionEnd
You need to add the function definition shown in the StrContains function page of the NSIS wiki (in the category of strings functions) in your code.
Don't forget the last statement !define StrContains ... to be able to call it with ${StrContains}

Resources