Dump output of macro to null - nsis

This mainly a curiosity question, but nevertheless. Imagine I have a macro with the declaration:
!define foo "!insertmacro foo"
!macro foo in1 in2 out1 out2 out3
; the code here
!macroend
with inX for inputs and outX for outputs. Now, I don't need all three outputs too often (e.g. one of them is an exit status returned by a winapi call), but still have to pass variables as placeholders to please the macro syntax:
${foo} $1 $2 $R1 $R2 $R3
Is there any syntax like
${foo} $1 $2 $R1 nul nul
to drop the unneeded output?
EDIT: Please also explain how to handle variable arguments for hybrids. SCCE:
OutFile sccce.exe
!define foo "!insertmacro foo"
!macro foo in1 out1 out2
Push "${in1}"
Call bar
Pop "${out1}"
!macroend
Section
${foo} $0 $1 $2 ; compilable
${foo} $0 $1 "" ; not compilable
SectionEnd
Function bar
Pop $0
IntOp $0 $0 + 1
Push $0
FunctionEnd

You can use any magic string you like to signal a unused macro parameter and then check for this in the macro implementation. The other alternative would be to create your own $null variable at the top of your script.
!macro Foo always maybe
IntOp ${always} 666 * 1337
!if "${maybe}" != ""
IntOp ${maybe} 1234 * 1337
!endif
!macroend
!insertmacro Foo $0 ""
!insertmacro Foo $0 $1
Edit:
There is no $optimize_me_away variable and no way to PopAndDiscard so you have to find a way to throw away the result:
!macro foo_alt1 in1 out1 ; The disadvantage with this method is that the common case is "bloated"
Push "${in1}"
Call bar_alt1 ; Will store result in $0
!if "${out1}" == ""
Pop $0
!else if "${out1}" != $0
StrCpy ${out1} $0
Pop $0
!endif
!macroend
Function bar_alt1
Exch $0
IntOp $0 $0 + 1
FunctionEnd
!include LogicLib.nsh
!macro foo_alt2 in1 out1
Push "${in1}"
Call bar_alt2
!if "${out1}" == ""
!insertmacro _LOGICLIB_TEMP ; LogicLib has a internal varible we can use, or you can make your own
Pop $_LOGICLIB_TEMP
!else
Pop ${out1}
!endif
!macroend
Function bar_alt2
Exch $0
IntOp $0 $0 + 1
Exch $0
FunctionEnd
Section
!macro test alt
StrCpy $0 PreserveMe
!insertmacro foo_alt${alt} 1337 $1
DetailPrint r0=$0,r1=$1
!insertmacro foo_alt${alt} 1337 $0
DetailPrint r0=$0
!insertmacro foo_alt${alt} 1337 ""
DetailPrint NoResult
!macroend
!insertmacro test 1
!insertmacro test 2
SectionEnd

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 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

NSIS: How to validate IP address box

I have added a ${NSD_CreateIPaddress} box in my nsi script, but while validating each field of IP is come up with by default to 0. I am getting the IP address value by using ${GetText}.
Is there any way to remove default value 0 and validate each field of IP address. Please help me out.
I'm not really sure what you are asking about. If you want to tell the difference between a blank address and the valid "0.0.0.0" address you can use the IPM_ISBLANK message. There should be no need to validate the individual fields because they are already limited to 0..255 and you can even set a custom range with IPM_SETRANGE.
If you still feel the need to look at the individual fields you can
A) Manually parse the string you get from ${NSD_GetText} yourself.
or
B) Unpack the 32-bit address you get from IPM_GETADDRESS:
Page Custom myPage myPageLeave
Page InstFiles
!include nsDialogs.nsh
; Using custom version of http://nsis.sourceforge.net/NsDialogs_CreateIPaddress
!ifndef NSD_CreateIPaddress
!define NSD_CreateIPaddress "!insertmacro NSD_CreateIPaddress "
!include LogicLib.nsh
!include WinMessages.nsh
!ifndef ICC_INTERNET_CLASSES
!define ICC_INTERNET_CLASSES 0x00000800
!endif
Function CCInitIP
!insertmacro _LOGICLIB_TEMP
System::Call '*(i8,i${ICC_INTERNET_CLASSES})p.s' ; NSIS 2.50+
System::Call 'COMCTL32::InitCommonControlsEx(pss)'
Pop $_LOGICLIB_TEMP
System::Free $_LOGICLIB_TEMP
FunctionEnd
!macro NSD_CreateIPaddress x y w h t
!insertmacro _LOGICLIB_TEMP
Call CCInitIP
nsDialogs::CreateControl "SysIPAddress32" ${DEFAULT_STYLES}|${WS_TABSTOP} 0 ${x} ${y} ${w} ${h} "${t}"
Exch $0
CreateFont $_LOGICLIB_TEMP "$(^Font)" "$(^FontSize)"
SendMessage $0 ${WM_SETFONT} $_LOGICLIB_TEMP 1
Exch $0
!macroend
!define /math IPM_GETADDRESS ${WM_USER} + 102
!define /math IPM_ISBLANK ${WM_USER} + 105
!endif
Function OnIPNotify ; This function displays some information about the IP
Pop $0 ; Not used
${NSD_GetText} $1 $3
StrCpy $4 "NSD_GetText: $3"
SendMessage $1 ${IPM_ISBLANK} 0 0 $0
StrCpy $4 "$4$\nIPM_ISBLANK: $0"
System::Call 'USER32::SendMessage(pr1, i ${IPM_GETADDRESS}, p 0, *i0 r3)p.r0' ; NSIS 2.50+
IntFmt $5 "0x%.8x" $3
StrCpy $4 "$4$\nIPM_GETADDRESS: ValidFields=$0 PackedIP=$5"
IntOp $0 $3 >> 24
IntOp $0 $0 & 0xff
StrCpy $4 "$4$\n$\t Field1=$0"
IntOp $0 $3 >> 16
IntOp $0 $0 & 0xff
StrCpy $4 "$4$\n$\t Field2=$0"
IntOp $0 $3 >> 8
IntOp $0 $0 & 0xff
StrCpy $4 "$4$\n$\t Field3=$0"
IntOp $0 $3 & 0xff
StrCpy $4 "$4$\n$\t Field4=$0"
${NSD_SetText} $2 $4
FunctionEnd
Function myPage
nsDialogs::Create 1018
Pop $0
${NSD_CreateIPaddress} 1% 0 50% 12u ""
Pop $1
${NSD_CreateLabel} 1% 20u 98% -20u "Enter an IP address to see information here..."
Pop $2
${NSD_OnNotify} $1 OnIPNotify ; Display some information when the control is changed
nsDialogs::Show
FunctionEnd
Function myPageLeave
Push 0
Call OnIPNotify
MessageBox mb_ok $4
FunctionEnd

nsis refresh environment during setup

During my setup i install redistributables. After installing my program i want to execute the program during the setup. For that the redistributables are needed. But the environment is not refreshed during setup, so i can't start the program. Can i refresh the environtment during the setup, or reread the environment? Or read the system environment which has changed after the redistributable installation?
I need the environment changes made during the installation of the redistributables during the setup.
Thanks for your help.
I had your same issue, that I solved with a workaround, but I came across this solution and I would like to share with you.
This should reload environment variables during setup:
!include LogicLib.nsh
!include WinCore.nsh
!ifndef NSIS_CHAR_SIZE
!define NSIS_CHAR_SIZE 1
!define SYSTYP_PTR i
!else
!define SYSTYP_PTR p
!endif
!ifndef ERROR_MORE_DATA
!define ERROR_MORE_DATA 234
!endif
/*!ifndef KEY_READ
!define KEY_READ 0x20019
!endif*/
Function RegReadExpandStringAlloc
System::Store S
Pop $R2 ; reg value
Pop $R3 ; reg path
Pop $R4 ; reg hkey
System::Alloc 1 ; mem
StrCpy $3 0 ; size
loop:
System::Call 'SHLWAPI::SHGetValue(${SYSTYP_PTR}R4,tR3,tR2,i0,${SYSTYP_PTR}sr2,*ir3r3)i.r0' ; NOTE: Requires SHLWAPI 4.70 (IE 3.01+ / Win95OSR2+)
${If} $0 = 0
Push $2
Push $0
${Else}
System::Free $2
${If} $0 = ${ERROR_MORE_DATA}
IntOp $3 $3 + ${NSIS_CHAR_SIZE} ; Make sure there is room for SHGetValue to \0 terminate
System::Alloc $3
Goto loop
${Else}
Push $0
${EndIf}
${EndIf}
System::Store L
FunctionEnd
Function RefreshProcessEnvironmentPath
System::Store S
Push ${HKEY_CURRENT_USER}
Push "Environment"
Push "Path"
Call RegReadExpandStringAlloc
Pop $0
${IfThen} $0 <> 0 ${|} System::Call *(i0)${SYSTYP_PTR}.s ${|}
Pop $1
Push ${HKEY_LOCAL_MACHINE}
Push "SYSTEM\CurrentControlSet\Control\Session Manager\Environment"
Push "Path"
Call RegReadExpandStringAlloc
Pop $0
${IfThen} $0 <> 0 ${|} System::Call *(i0)${SYSTYP_PTR}.s ${|}
Pop $2
System::Call 'KERNEL32::lstrlen(t)(${SYSTYP_PTR}r1)i.R1'
System::Call 'KERNEL32::lstrlen(t)(${SYSTYP_PTR}r2)i.R2'
System::Call '*(&t$R2 "",&t$R1 "",i)${SYSTYP_PTR}.r0' ; The i is 4 bytes, enough for a ';' separator and a '\0' terminator (Unicode)
StrCpy $3 ""
${If} $R1 <> 0
${AndIf} $R2 <> 0
StrCpy $3 ";"
${EndIf}
System::Call 'USER32::wsprintf(${SYSTYP_PTR}r0,t"%s%s%s",${SYSTYP_PTR}r2,tr3,${SYSTYP_PTR}r1)?c'
System::Free $1
System::Free $2
System::Call 'KERNEL32::SetEnvironmentVariable(t"PATH",${SYSTYP_PTR}r0)'
System::Free $0
System::Store L
FunctionEnd
Section
Call RefreshProcessEnvironmentPath
SectionEnd
Source: http://forums.winamp.com/showpost.php?p=3028153&postcount=12

NSIS script + Skip button alignment issue

I am trying NSIS script to add "SKIP" button.
I have tested below code and it works well but when I actually integrate with the main code which is having a screen of option "back" "Next" and "Cancel" button. this skip button is not visible at all, it is in extreme right of cancel button and hardly visible.
How can move this "Skip" button towards left side.
Name BtnTest
Outfile test.exe
Installdir "$temp"
RequestExecutionLevel user
BrandingText " " ;Button covers this text
!include nsDialogs.nsh ;For WS_*
!define MUI_CUSTOMFUNCTION_GUIINIT SKIP
Function SKIP
; You are supposed to use ChangeUI (or MUI_UI) and a modified ui file to add new buttons but this example adds the button at run-time...
GetDlgItem $0 $hwndparent 2 ; Find cancel button
System::Call *(i,i,i,i)i.r1
System::Call 'USER32::GetWindowRect(ir0,ir1)'
System::Call *$1(i.r2,i.r3,i.r4,i.r5)
IntOp $5 $5 - $3 ;height
IntOp $4 $4 - $2 ;width
System::Call 'USER32::ScreenToClient(i$hwndparent,ir1)'
System::Call *$1(i.r2,i.r3)
System::Free $1
IntOp $2 $2 + $4 ;x
IntOp $2 $2 + 8 ;x+padding
System::Call 'USER32::CreateWindowEx(i0,t "Button",t "Click Me",i${WS_CHILD}|${WS_VISIBLE}|${WS_TABSTOP},ir2,ir3,ir4,ir5,i $hwndparent,i 0x666,i0,i0)i.r0'
SendMessage $hwndparent ${WM_GETFONT} 0 0 $1
SendMessage $0 ${WM_SETFONT} $1 1
GetFunctionAddress $0 onmybtnclick
ButtonEvent::AddEventHandler 0x666 $0
FunctionEnd
Function onmybtnclick
MessageBox mb_ok "You clicked me!"
FunctionEnd
Page Directory
Page Instfiles
Section
SectionEnd
And also this button appears on all screen How can I display only in one screen.
Change $2 and $3 before the call to CreateWindowEx to change the button position. You can save the handle to the button in another variable after CreateWindowEx but I just used the Id in this example (0x666).
Name BtnTest
Outfile test.exe
Installdir "$temp"
RequestExecutionLevel user
BrandingText " " ;Button covers this text
!include nsDialogs.nsh ;For WS_*
!define MUI_CUSTOMFUNCTION_GUIINIT SKIP
/**/ !include MUI2.nsh #*/
!ifndef MUI_INCLUDED
Function .onGuiInit
Call SKIP
FunctionEnd
!endif
Function SKIP
; You are supposed to use ChangeUI (or MUI_UI) and a modified ui file to add new buttons but this example adds the button at run-time...
GetDlgItem $0 $hwndparent 2 ; Find cancel button
System::Call *(i,i,i,i)i.r1
System::Call 'USER32::GetWindowRect(ir0,ir1)'
System::Call *$1(i.r2,i.r3,i.r4,i.r5)
IntOp $5 $5 - $3 ;height
IntOp $4 $4 - $2 ;width
System::Call 'USER32::ScreenToClient(i$hwndparent,ir1)'
System::Call *$1(i.r2,i.r3)
System::Free $1
!ifdef MUI_INCLUDED
StrCpy $2 15 ; Whatever you feel looks good
!else
IntOp $2 $2 + $4 ;x
IntOp $2 $2 + 8 ;x+padding
!endif
System::Call 'USER32::CreateWindowEx(i0,t "Button",t "Click Me",i${WS_CHILD}|${WS_VISIBLE}|${WS_TABSTOP},ir2,ir3,ir4,ir5,i $hwndparent,i 0x666,i0,i0)i.r0'
SendMessage $hwndparent ${WM_GETFONT} 0 0 $1
SendMessage $0 ${WM_SETFONT} $1 1
GetFunctionAddress $0 onmybtnclick
ButtonEvent::AddEventHandler 0x666 $0
FunctionEnd
Function onmybtnclick
MessageBox mb_ok "You clicked me!"
FunctionEnd
Function HideIt
GetDlgItem $0 $hwndparent 0x666
ShowWindow $0 0
FunctionEnd
Function ShowIt
GetDlgItem $0 $hwndparent 0x666
ShowWindow $0 1
FunctionEnd
!ifdef MUI_INCLUDED
!define MUI_PAGE_CUSTOMFUNCTION_SHOW HideIt
!insertmacro MUI_PAGE_COMPONENTS
!define MUI_PAGE_CUSTOMFUNCTION_SHOW ShowIt
!insertmacro MUI_PAGE_COMPONENTS
!define MUI_PAGE_CUSTOMFUNCTION_SHOW HideIt
!insertmacro MUI_PAGE_COMPONENTS
!insertmacro MUI_PAGE_INSTFILES
!insertmacro MUI_LANGUAGE English
!else
Page Components "" HideIt
Page Components "" ShowIt
Page Components "" HideIt
Page Instfiles
!endif
Section
SectionEnd

Resources