Skip to content
⌘ NSIS Forum Archive

Run elevated *.exe in non-elevated installer in the dual mode (SILENT/NORMAL)

5 posts

meoit#

Run elevated *.exe in non-elevated installer in the dual mode (SILENT/NORMAL)

My Installer have two mode to install: GUI and SILENT.

I want installer to run non-elevated process when user using GUI
AND
I want installer to run elevated process when user using SILENT.

But unsuccessful 🙁

This is code:

Unicode true
RequestExecutionLevel user
!include 'MUI2.nsh'
!include 'x64.nsh'
!include 'WinVer.nsh'
Var /GLOBAL Var_Silent
Var /GLOBAL Var_Silent
;--------------------------------
;General
  ;Name and file
  Name "Non-Elevated/Elevated Dual Mode Installer"
  OutFile "TEST-DualMode.exe"
  ;Default installation folder
  InstallDir "$LOCALAPPDATA\Modern UI Test"
  ;Get installation folder from registry if available
  InstallDirRegKey HKCU "Software\Dual Mode Installer" ""
;--------------------------------
;Interface Settings
  !define MUI_ABORTWARNING
;--------------------------------
;Pages
  !insertmacro MUI_PAGE_LICENSE "${NSISDIR}\Docs\Modern UI\License.txt"
  !insertmacro MUI_PAGE_COMPONENTS
  !insertmacro MUI_PAGE_DIRECTORY
  !insertmacro MUI_PAGE_INSTFILES
  !insertmacro MUI_UNPAGE_CONFIRM
  !insertmacro MUI_UNPAGE_INSTFILES
;--------------------------------
;Languages
  !insertmacro MUI_LANGUAGE "English"
Function .onInit
    ${GetParameters} $0
    ClearErrors
    ${GetOptions} `$0` '/silent-install' $1 ;Nếu không get được thì có Error, sử dụng error đó bằng IfErrors 0 LamGiDo
    IfErrors 0 SilentInstall_TRUE
        StrCpy $Var_Silent 0
        Goto SilentInstall_FALSE
    SilentInstall_TRUE:
        StrCpy $Var_Silent 1
    SilentInstall_FALSE:
    ;
    ${GetParameters} $0
    ClearErrors
    ${GetOptions} `$0` '/location=' $1
    StrLen $0 '$1'
    ${If} $0 > 3
        StrCpy $INSTDIR '$1'
    ${Else}
        StrCpy $INSTDIR 'C:\Test\Location'
    ${EndIf}
    ;
    ;If user run setup in silent mode, I want to run the Installer as Child Elevated process with parameters of silent, the Main non-Elevated process will auto quit
    ${If} $Var_Silent == 1
        ${If} ${AtLeastWinVista}
            UserInfor::GetAccountType
            Pop $0
            ${If} $0 != 'admin'
                CopyFiles '$EXEPATH' '$TEMP'
                ExecShell 'runas' '$TEMP/TEST-DualMode.exe /silent-install /location=$INSTDIR'
                Quit
            ${EndIf}
        ${EndIf}
    ${EndIf}
FunctionEnd
;--------------------------------
;Installer Sections
Section "Dummy Section" SecDummy
  SetOutPath "$INSTDIR"
  WriteRegStr HKCU "Software\Dual Mode Installer" "" $INSTDIR
  WriteUninstaller "$INSTDIR\Uninstall.exe"
SectionEnd
;--------------------------------
;Descriptions
  ;Language strings
  LangString DESC_SecDummy ${LANG_ENGLISH} "A test section."
  ;Assign language strings to sections
  !insertmacro MUI_FUNCTION_DESCRIPTION_BEGIN
    !insertmacro MUI_DESCRIPTION_TEXT ${SecDummy} $(DESC_SecDummy)
  !insertmacro MUI_FUNCTION_DESCRIPTION_END
;--------------------------------
;Uninstaller Section
Section "Uninstall"
  Delete "$INSTDIR\Uninstall.exe"
  RMDir "$INSTDIR"
  DeleteRegKey /ifempty HKCU "Software\Dual Mode Installer"
SectionEnd 
Thanks for help.
meoit#
Thanks Anders !

I edited code, but also un-success.

Unicode true
RequestExecutionLevel user
!include 'MUI2.nsh'
!include 'FileFunc.nsh'
!include 'x64.nsh'
!include 'WinVer.nsh'
Var /GLOBAL Var_Silent
!define My_Temp_EXEC '$TEMP\TMP.exe'
;--------------------------------
;General
  ;Name and file
  Name "Non-Elevated/Elevated Dual Mode Installer"
  OutFile "Tv.exe"
  ;Default installation folder
  InstallDir "$LOCALAPPDATA\Modern UI Test"
  ;Get installation folder from registry if available
  InstallDirRegKey HKCU "Software\Dual Mode Installer" ""
;--------------------------------
;Interface Settings
  !define MUI_ABORTWARNING
;--------------------------------
;Pages
  !insertmacro MUI_PAGE_LICENSE "${NSISDIR}\Docs\Modern UI\License.txt"
  !insertmacro MUI_PAGE_COMPONENTS
  !insertmacro MUI_PAGE_DIRECTORY
  !insertmacro MUI_PAGE_INSTFILES
  !insertmacro MUI_UNPAGE_CONFIRM
  !insertmacro MUI_UNPAGE_INSTFILES
;--------------------------------
;Languages
  !insertmacro MUI_LANGUAGE "English"
!define RunAsAdminQuitSI '!insertmacro RunAsAdminQuitSI'
!macro RunAsAdminQuitSI
    ${If} ${AtLeastWinVista}
        UserInfor::GetAccountType
        Pop $0
        ${If} $0 != 'admin'
            CopyFiles '$EXEPATH' '${My_Temp_EXEC}'
            ExecShell 'runas' '$\"${My_Temp_EXEC} $\" -silent-install -location=$INSTDIR'
            Quit
        ${EndIf}
    ${EndIf}
!macroend
Function .onInit
    ${GetParameters} $0
    ClearErrors
    ${GetOptions} `$0` '-silent-install' $1
    ${IfNot} ${Errors}
        StrCpy $Var_Silent 1
    ${Else}
        StrCpy $Var_Silent 0
    ${EndIf}
    ;
    ${GetParameters} $0
    ClearErrors
    ${GetOptions} `$0` '-location=' $1
    StrLen $0 '$1'
    ${If} $0 > 3
        StrCpy $INSTDIR '$1'
    ${Else}
        StrCpy $INSTDIR 'C:\Test\Location'
    ${EndIf}
    ;
    ;If user run setup in silent mode, I want to run the Installer as Child Elevated process with parameters of silent, the Main non-Elevated process will auto quit
    ${If} $Var_Silent == 1
        ${RunAsAdminQuitSI}
    ${EndIf}
FunctionEnd
;--------------------------------
;Installer Sections
Section "Dummy Section" SecDummy
  SetOutPath "$INSTDIR"
  WriteRegStr HKCU "Software\Dual Mode Installer" "" $INSTDIR
  WriteUninstaller "$INSTDIR\Uninstall.exe"
SectionEnd
;--------------------------------
;Descriptions
  ;Language strings
  LangString DESC_SecDummy ${LANG_ENGLISH} "A test section."
  ;Assign language strings to sections
  !insertmacro MUI_FUNCTION_DESCRIPTION_BEGIN
    !insertmacro MUI_DESCRIPTION_TEXT ${SecDummy} $(DESC_SecDummy)
  !insertmacro MUI_FUNCTION_DESCRIPTION_END
;--------------------------------
;Uninstaller Section
Section "Uninstall"
  Delete "$INSTDIR\Uninstall.exe"
  RMDir "$INSTDIR"
  DeleteRegKey /ifempty HKCU "Software\Dual Mode Installer"
SectionEnd 
Anders#
It is still wrong, please try reading the documentation to understand the syntax.

ExecShell 'open' 'cmd.exe' '/K echo hello'