Skip to content

Commit 7ea3d62

Browse files
committed
fix(nsis): machine-wide boring NSIS installer launches application as administrator
Closes #864
1 parent cf24b01 commit 7ea3d62

File tree

6 files changed

+46
-42
lines changed

6 files changed

+46
-42
lines changed

templates/nsis/boringInstaller.nsh

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
!include UAC.nsh
2+
!include StdUtils.nsh
23

34
!ifndef INSTALL_MODE_PER_ALL_USERS
45
!include multiUserUi.nsh
56
!endif
67

78
!ifndef BUILD_UNINSTALLER
89
Function StartApp
9-
!insertmacro UAC_AsUser_ExecShell "" "$SMPROGRAMS\${PRODUCT_FILENAME}.lnk" "" "" ""
10+
${StdUtils.ExecShellAsUser} $0 "$SMPROGRAMS\${PRODUCT_FILENAME}.lnk" "open" ""
1011
FunctionEnd
1112

1213
!define MUI_FINISHPAGE_RUN
@@ -41,10 +42,14 @@
4142
!insertmacro UAC_PageElevation_OnInit
4243

4344
${If} ${UAC_IsInnerInstance}
44-
${AndIfNot} ${UAC_IsAdmin}
45-
# special return value for outer instance so it knows we did not have admin rights
46-
SetErrorLevel 0x666666
47-
Quit
45+
${If} ${UAC_IsAdmin}
46+
!insertmacro setInstallModePerAllUsers
47+
Goto functionEnd
48+
${else}
49+
# special return value for outer instance so it knows we did not have admin rights
50+
SetErrorLevel 0x666666
51+
Quit
52+
${EndIf}
4853
${EndIf}
4954

5055
!ifndef MULTIUSER_INIT_TEXT_ADMINREQUIRED
@@ -78,13 +83,13 @@
7883
${GetOptions} $R0 "/allusers" $R1
7984
${IfNot} ${Errors}
8085
!insertmacro setInstallModePerAllUsers
81-
Goto FEnd
86+
Goto functionEnd
8287
${EndIf}
8388

8489
${GetOptions} $R0 "/currentuser" $R1
8590
${IfNot} ${Errors}
8691
!insertmacro setInstallModePerUser
87-
Goto FEnd
92+
Goto functionEnd
8893
${EndIf}
8994

9095
${if} $hasPerUserInstallation == "1"
@@ -102,7 +107,7 @@
102107
!endif
103108
${endif}
104109

105-
FEnd:
110+
functionEnd:
106111
!endif
107112
!macroend
108113

templates/nsis/installSection.nsh

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,33 @@
4343
${endif}
4444
!macroend
4545

46+
!macro registryAddInstallInfo
47+
WriteRegStr SHCTX "${INSTALL_REGISTRY_KEY}" InstallLocation "$INSTDIR"
48+
49+
${if} $installMode == "all"
50+
StrCpy $0 "/allusers"
51+
StrCpy $1 ""
52+
${else}
53+
StrCpy $0 "/currentuser"
54+
StrCpy $1 " (only current user)"
55+
${endif}
56+
57+
WriteRegStr SHCTX "${UNINSTALL_REGISTRY_KEY}" DisplayName "${UNINSTALL_DISPLAY_NAME}$1"
58+
# https://github.com/electron-userland/electron-builder/issues/750
59+
StrCpy $2 "$INSTDIR\${UNINSTALL_FILENAME}"
60+
WriteRegStr SHCTX "${UNINSTALL_REGISTRY_KEY}" UninstallString '"$2" $0'
61+
62+
WriteRegStr SHCTX "${UNINSTALL_REGISTRY_KEY}" "DisplayVersion" "${VERSION}"
63+
WriteRegStr SHCTX "${UNINSTALL_REGISTRY_KEY}" "DisplayIcon" "$appExe,0"
64+
WriteRegStr SHCTX "${UNINSTALL_REGISTRY_KEY}" "Publisher" "${COMPANY_NAME}"
65+
WriteRegDWORD SHCTX "${UNINSTALL_REGISTRY_KEY}" NoModify 1
66+
WriteRegDWORD SHCTX "${UNINSTALL_REGISTRY_KEY}" NoRepair 1
67+
68+
${GetSize} "$INSTDIR" "/S=0K" $0 $1 $2
69+
IntFmt $0 "0x%08X" $0
70+
WriteRegDWORD SHCTX "${UNINSTALL_REGISTRY_KEY}" "EstimatedSize" "$0"
71+
!macroend
72+
4673
InitPluginsDir
4774

4875
!ifdef HEADER_ICO

templates/nsis/installer.nsi

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ Function .onInit
3838
!insertmacro quitSuccess
3939
!else
4040
!insertmacro check64BitAndSetRegView
41-
!insertmacro initMultiUser
4241

4342
!ifdef ONE_CLICK
4443
!insertmacro ALLOW_ONLY_ONE_INSTALLER_INSTANCE
@@ -48,6 +47,8 @@ Function .onInit
4847
${EndIf}
4948
!endif
5049

50+
!insertmacro initMultiUser
51+
5152
!ifmacrodef customInit
5253
!insertmacro customInit
5354
!endif

templates/nsis/multiUser.nsh

Lines changed: 1 addition & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -59,32 +59,4 @@ Var installMode
5959
${endif}
6060
!endif
6161
!macroend
62-
!endif
63-
64-
# SHCTX is the hive HKLM if SetShellVarContext all, or HKCU if SetShellVarContext user
65-
!macro registryAddInstallInfo
66-
WriteRegStr SHCTX "${INSTALL_REGISTRY_KEY}" InstallLocation "$INSTDIR"
67-
68-
${if} $installMode == "all"
69-
StrCpy $0 "/allusers"
70-
StrCpy $1 ""
71-
${else}
72-
StrCpy $0 "/currentuser"
73-
StrCpy $1 " (only current user)"
74-
${endif}
75-
76-
WriteRegStr SHCTX "${UNINSTALL_REGISTRY_KEY}" DisplayName "${UNINSTALL_DISPLAY_NAME}$1"
77-
# https://github.com/electron-userland/electron-builder/issues/750
78-
StrCpy $2 "$INSTDIR\${UNINSTALL_FILENAME}"
79-
WriteRegStr SHCTX "${UNINSTALL_REGISTRY_KEY}" UninstallString '"$2" $0'
80-
81-
WriteRegStr SHCTX "${UNINSTALL_REGISTRY_KEY}" "DisplayVersion" "${VERSION}"
82-
WriteRegStr SHCTX "${UNINSTALL_REGISTRY_KEY}" "DisplayIcon" "$appExe,0"
83-
WriteRegStr SHCTX "${UNINSTALL_REGISTRY_KEY}" "Publisher" "${COMPANY_NAME}"
84-
WriteRegDWORD SHCTX "${UNINSTALL_REGISTRY_KEY}" NoModify 1
85-
WriteRegDWORD SHCTX "${UNINSTALL_REGISTRY_KEY}" NoRepair 1
86-
87-
${GetSize} "$INSTDIR" "/S=0K" $0 $1 $2
88-
IntFmt $0 "0x%08X" $0
89-
WriteRegDWORD SHCTX "${UNINSTALL_REGISTRY_KEY}" "EstimatedSize" "$0"
90-
!macroend
62+
!endif

templates/nsis/multiUserUi.nsh

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -126,9 +126,8 @@ Var RadioButtonLabel1
126126
SendMessage $MultiUser.InstallModePage.AllUsers ${BM_GETCHECK} 0 0 $MultiUser.InstallModePage.ReturnValue
127127

128128
${if} $MultiUser.InstallModePage.ReturnValue = ${BST_CHECKED}
129-
${if} ${UAC_IsAdmin}
130-
!insertmacro setInstallModePerAllUsers
131-
${else}
129+
!insertmacro setInstallModePerAllUsers
130+
${IfNot} ${UAC_IsAdmin}
132131
!ifdef MULTIUSER_INSTALLMODE_ALLOW_ELEVATION
133132
GetDlgItem $9 $HWNDParent 1
134133
System::Call user32::GetFocus()i.s

templates/nsis/oneClick.nsh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
!ifdef RUN_AFTER_FINISH
22
!ifndef BUILD_UNINSTALLER
3+
!include StdUtils.nsh
34
Function StartApp
45
!ifdef INSTALL_MODE_PER_ALL_USERS
5-
!include StdUtils.nsh
66
${StdUtils.ExecShellAsUser} $0 "$SMPROGRAMS\${PRODUCT_FILENAME}.lnk" "open" ""
77
!else
88
ExecShell "" "$SMPROGRAMS\${PRODUCT_FILENAME}.lnk"

0 commit comments

Comments
 (0)