How to process a string in NSIS? - string

I have a string from the parameters that has a installation path using this code:
${GetParameters} $R0
${if} $R0 != ""
StrCpy $R1 $R0 "" 3
StrCpy $INSTDIR $R1 -1
${endif}
the $INSTDIR contains a path like this:
C:\Program Files (x86)\My Applicatoin
I want to get "My Application" out of it and save it in a variable. I know that I should check the characters backwards until i reach the backslash ( \ ) but I do not know how to implement it in NSIS syntax.
How can I get "My Application" from the folder path using NSIS?

Most string operations can be coded with just StrCpy, StrCmp and StrLen.
A basic version that only checks \ and not / already exists:
!include FileFunc.nsh
StrCpy $0 "C:\Program Files (x86)\My Application"
${GetFileName} $0 $1
DetailPrint $1

Related

NSIS script doesn't display service status

I created section at the end of script which suppose to display myservice status, but no messagebox displays after exec.
What might be the reason?
Section "Create Service"
ExecShellWait '' 'sc.exe' 'create myservice error= "severe" displayname= "myservice" type= "own" start= "auto" binpath= "$INSTDIR\MyService.exe"' SW_HIDE
SectionEnd
Section "Start Service"
ExecShellWait '' 'sc.exe' 'start myservice' SW_HIDE
SectionEnd
Section "Ensure Running"
StrCpy $R0 '"$SYSDIR\cmd.exe" /c "sc QUERY myservice | FIND /C "RUNNING""'
nsExec::ExecToStack '$R0'
Pop $R1 # contains return code
Pop $R2 # contains output
${If} $R1 == "0"
DetailPrint "checking if command is success"
${If} $R2 == "1"
MessageBox mb_ok "myservice is Running" # it's running
${Else}
MessageBox mb_ok "Not Running" # it's not running
${EndIf}
${Else}
DetailPrint "command failed"
${EndIf}
SectionEnd ```
nsExec::ExecToStack /OEM '$R0' didn't change anything. During installation it rushes to finish page after sc exec ..Do I need to add additional page page here ?
!insertmacro MUI_PAGE_WELCOME
!insertmacro MUI_PAGE_LICENSE ..\license.rtf
; Page custom pageRegistration
!insertmacro MUI_PAGE_INSTFILES
!insertmacro MUI_PAGE_FINISH
!insertmacro MUI_LANGUAGE English
Without seeing the output I can only guess that there is some sort of Unicode issue.
Change nsExec::ExecToStack '$R0' to nsExec::ExecToStack /OEM '$R0'.
That being said, I don't know if the RUNNING string is always in English. There are several service plug-ins and macros on the NSIS wiki you can use instead.
Or you can use this:
!include "LogicLib.nsh"
!include "Win\COM.nsh"
!include "Win\Propkey.nsh"
Function IsServiceRunning
Exch $0
Push $1
StrCpy $1 $0
!insertmacro ComHlpr_CreateInProcInstance {13709620-C279-11ce-A49E-444553540000} {A4C6892C-3BA9-11D2-9DEA-00C04FB16162} r0 "" ; IE5+?
${If} $0 P<> 0
System::Call "$0->36(wr1,#r1)i.s"
${IUnknown::Release} $0 ""
Pop $0
${If} $0 >= 0
StrCpy $0 0
Push $2
${V_GetVT} $1 r2
${V_GetBOOL} $1 r1
${If} $2 = ${VT_BOOL}
${AndIf} $1 <> ${VARIANT_FALSE}
StrCpy $0 1
${EndIf}
Pop $2
${Else}
StrCpy $0 0
${EndIf}
${EndIf}
Pop $1
Exch $0
FunctionEnd
Section "Example"
Push "RemoteRegistry"
Call IsServiceRunning
Pop $0
DetailPrint "RemoteRegistry running=$0"
Push "Themes"
Call IsServiceRunning
Pop $0
DetailPrint "Themes running=$0"
SectionEnd

Set run-time cursor position in directory page dialog box - NSIS

I want to add at the end of the path the installation folder name of my App. I do it successfully after clicking the Browse button but, after many tentatives, I couldn't do it if I modify the text directly in the textbox.
Function .onVerifyInstDir
Push $0
FindWindow $0 "#32770" "" $HWNDPARENT
GetDlgItem $0 $0 0x3FB
;in alternative for MUI >2.0 I could use directly $mui.DirectoryPage.Directory. Is it right?
;does path end with "\MyApp"?
StrLen $R1 "\${APP_FOLDER_NAME}"
StrCpy $R0 $INSTDIR "" -$R1
StrCmp $R0 "\${APP_FOLDER_NAME}" +2
;add "\MyApp" after browse button clicking (OK)
StrCpy $INSTDIR "$INSTDIR\${APP_FOLDER_NAME}"
;add "\MyApp" after typing directly into the textbox but the cursor position reset to the first character. Tried to solve saving the current cursor position and then reapply it (NOK)
SendMessage $0 ${EM_GETSEL} null $1
SendMessage $0 ${WM_SETTEXT} 0 "STR:$INSTDIR"
SendMessage $0 ${EM_SETSEL} $1 $1
FunctionEnd
I don't succeed of mantain the cursor where I'm modifying the path string and it reset always to the first char.
In a previous phase of install process I initialise the Install path as follow
StrCpy $INSTDIR "$APPDATA\${APP_FOLDER_NAME}"
NSIS is supposed to do this for you already.
From the docs:
... the part of this string following the last \ will be used if the user selects 'browse', and may be appended back on to the string at install time (to disable this, end the directory with a \ (which will require the entire parameter to be enclosed with quotes). If this doesn't make any sense, play around with the browse button a bit.
Meaning,
InstallDir "$ProgramFiles\MyApp"
is not the same as
InstallDir "$ProgramFiles\MyApp\"
Setting the text in .onVerifyInstDir is not officially supported but this code somewhat works:
!define APP_FOLDER_NAME MyApp
Page Directory
Page InstFiles
!include WinMessages.nsh
!include LogicLib.nsh
Function .onInit
StrCpy $INSTDIR "$APPDATA\${APP_FOLDER_NAME}"
FunctionEnd
Var InOnVerifyInstDir
Var SeenModal
Function .onVerifyInstDir
${IfThen} $InOnVerifyInstDir <> 0 ${|} Return ${|} ; Don't recurse into .onVerifyInstDir
!if ${MUI_SYSVERSION} >= 2.0
StrCpy $0 $mui.DirectoryPage.Directory
!else
FindWindow $0 "#32770" "" $hWndParent
GetDlgItem $0 $0 0x3FB
!endif
System::Call 'USER32::GetActiveWindow()p.r2'
System::Call 'USER32::GetFocus()p.r1'
${If} $1 P<> $0
${If} $hWndParent P<> $2
StrCpy $SeenModal 1
Return
${EndIf}
${If} $SeenModal = 0
Return
${EndIf}
${EndIf}
StrCpy $SeenModal ""
;does path end with "\MyApp"?
StrLen $R1 "\${APP_FOLDER_NAME}"
StrCpy $R0 $INSTDIR "" -$R1
${If} $R0 != "\${APP_FOLDER_NAME}"
StrCpy $InOnVerifyInstDir 1
StrCpy $INSTDIR "$INSTDIR\${APP_FOLDER_NAME}"
SendMessage $0 ${EM_GETSEL} "" "" $1
IntOp $1 $1 >> 16 ; shift hiword
IntOp $1 $1 & 0xffff ; mask possible sign bit
SendMessage $0 ${WM_SETTEXT} 0 "STR:$INSTDIR"
SendMessage $0 ${EM_SETSEL} $1 $1
StrCpy $InOnVerifyInstDir 0
${EndIf}
FunctionEnd
but I would still recommend just using InstallDir.

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}

How set options for a NSIS uninstaller

I have an NSIS installer with some options which works very well. But my "--quiet" option doesn't work for the uninstaller.
uninst:
ClearErrors
${getOPtions} $CMDLINE "--quiet" $0
${IfNot} ${Errors}
StrLen $2 "\Uninstall.exe /S"
${Else}
StrLen $2 "\Uninstall.exe"
${EndIf}
StrCpy $3 $0 -$2 # remove "\Uninstall.exe"
ExecWait '$0 _?=$3' ;Do not copy the uninstaller to a temp file`
GetOptions expects you to call GetParameters to get the parameters, not $CmdLine:
!include FileFunc.nsh
!include LogicLib.nsh
...
${GetParameters} $1
ClearErrors
${GetOptions} $1 "--quiet" $2
${IfNot} ${Errors}
MessageBox mb_ok "Quiet mode"
${EndIf}
ExecWait "\Uninstall.exe /S" is never going to work, \Uninstall.exe means Uninstall.exe in the root of the current drive. You must use the full path and it should look something like this:
StrCpy $0 "$ProgramFiles\my app" ; TODO: Get the old install path
StrCpy $1 "/S" ; TODO: Set optional parameters
ExecWait '"$0\Uninstall.exe" $1 _?=$0'

How can I find an application if i don't know its GUID

In order to compare versions, I have to find out if my app has been already installed.
I use registry to store whole necessary inforamtion and it would be very useful if i could somehow read strings from registry. Main issue here is that I don't know my own GUID which was randomized during previous installation.
To generate my registry path I wrote following script:
Function .onInit
${If} ${RunningX64}
StrCpy $R0 "SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall"
${Else}
StrCpy $R0 "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall"
${EndIf}
FunctionEnd
MainSection:
Section "Main" sec
System::Call 'ole32::CoCreateGuid(g .s)'
Pop $0
WriteRegStr HKLM "$R0\$0" 'DisplayVersion' '${AppVersion}'
SectionEnd
So, basically I need to find a way to read DisplayVersion string. I wish there was some variation of FindFirst but for registry.
Use EnumRegKey to enumerate registry keys:
!include LogicLib.nsh
Section
StrCpy $0 0
loop:
EnumRegKey $1 HKLM "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall" $0
StrCmp $1 "" done
ReadRegStr $2 HKLM "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\$1" "DisplayName"
${If} $2 == "My Application Name"
ReadRegStr $2 HKLM "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\$1" "DisplayVersion"
DetailPrint "TODO: Compare $2 to version here..."
${EndIf}
IntOp $0 $0 + 1
Goto loop
done:
SectionEnd

Resources