Reboot and Continue

From NSIS Wiki
Jump to navigationJump to search
Author: Anders (talk, contrib)


This example shows how to perform some actions, reboot, and then continue with other actions.

RequestExecutionLevel Admin
Name "NSIS-Test-RebootContinue"
InstallDir "$ProgramFiles\NSIS-Test-RebootContinue"
ShowInstDetails Show
!define SETUPID "$(^Name)" ; TODO: Replace with a GUID from guidgen.com
 
Var Phase
 
!include MUI2.nsh
!include Sections.nsh
!include FileFunc.nsh
 
Function .onInit
${GetParameters} $0
ClearErrors
${GetOptions} $0 "/Phase2" $1
${IfNotThen} ${Errors} ${|} StrCpy $Phase 2 ${|}
 
IfSilent "" +2
	Call ConfigurePhaseSections
FunctionEnd
 
Function SkipPageInPhase2
${IfThen} $Phase >= 2 ${|} Abort ${|}
FunctionEnd
 
!define MUI_PAGE_CUSTOMFUNCTION_PRE SkipPageInPhase2
!insertmacro MUI_PAGE_WELCOME
!define MUI_PAGE_CUSTOMFUNCTION_PRE SkipPageInPhase2
!insertmacro MUI_PAGE_COMPONENTS
!define MUI_PAGE_CUSTOMFUNCTION_PRE SkipPageInPhase2
!insertmacro MUI_PAGE_DIRECTORY
!define MUI_PAGE_CUSTOMFUNCTION_PRE ConfigurePhaseSections
!insertmacro MUI_PAGE_INSTFILES
!insertmacro MUI_PAGE_FINISH
!insertmacro MUI_LANGUAGE "English"
 
 
Section "Phase1: Required" SID_P11
SectionIn RO
DetailPrint P1.1
Sleep 1000
SetRebootFlag True ; Pretend we did something that requires a reboot
SectionEnd
 
Section "Phase1: Optional" SID_P12
DetailPrint P1.2
Sleep 2000
SectionEnd
 
Section "-ConfigureRebootAndContinue" SID_P1LAST
StrCpy $0 $ExePath 1 1
${If} $0 == ':'
	StrCpy $0 $ExePath
${Else}
	StrCpy $0 "$WinDir\Temp"
	CreateDirectory "$0"
	StrCpy $0 "$0\${SETUPID}.exe"
	FileOpen $1 $0 w ; Make sure file exists to trigger a file copy instead of a directory copy
	FileClose $1
	CopyFiles /Silent /FilesOnly $ExePath "$0" ; A UNC path might not be available after a reboot, copy the setup to a local path
	WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\RunOnceEx\${SETUPID}" "2" '||"$SysDir\rundll32.exe" "$SysDir\advpack.dll",DelNodeRunDLL32 "$0"'
${EndIf}
WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\RunOnceEx\${SETUPID}" "" "$(^Name)"
WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\RunOnceEx\${SETUPID}" "1" '||"$0" /Phase2'
SectionEnd
 
Section "Phase2: Required" SID_P21
SectionIn RO
DetailPrint P2.1
Sleep 2000
SectionEnd
 
 
Function ConfigurePhaseSections
${If} $Phase >= 2
	StrCpy $1 ${SID_P11} ; Disable first
	StrCpy $2 ${SID_P1LAST} ; Disable last
${Else}
	StrCpy $1 ${SID_P21}
	StrCpy $2 ${SID_P21}
${EndIf}
loop:
	${If} $1 <= $2
		!insertmacro UnselectSection $1
		IntOp $1 $1 + 1
		Goto loop
	${EndIf}
FunctionEnd