How do you request administrator permissions using NSIS?

前端 未结 1 608
鱼传尺愫
鱼传尺愫 2020-11-30 08:13

I am quite new with NSIS. I am trying to request administrator permissions in order to run the installer, as it messes around a bit with registries. My problem with \"Reques

1条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-11-30 08:41

    Outfile RequireAdmin.exe
    RequestExecutionLevel admin ;Require admin rights on NT6+ (When UAC is turned on)
    
    !include LogicLib.nsh
    
    Function .onInit
    UserInfo::GetAccountType
    pop $0
    ${If} $0 != "admin" ;Require admin rights on NT4+
        MessageBox mb_iconstop "Administrator rights required!"
        SetErrorLevel 740 ;ERROR_ELEVATION_REQUIRED
        Quit
    ${EndIf}
    FunctionEnd
    
    Page InstFiles
    
    Section
    SectionEnd
    

    is the basic code I usually recommend to make sure the installer is running as an Administrator.

    IMHO it does not make sense to prompt for credentials on a custom page unless only parts of the install process requires administrator access and the other part requires access to the users profile. If this applies to you then you should take a look at the UAC plug-in (It is a bit complicated to use and makes it impossible for your exe file to get the shield overlay icon)

    I don't think the RunAs plug-in works correctly on Vista+ when UAC is on so trying to get it to work might be a dead end...

    The recommended way to get the shield is to request elevation in the exe manifest, RequestExecutionLevel admin does that. If you don't use RequestExecutionLevel at all in your script your installer might be detected as a legacy installer and it will also get the shield overlay.

    In Windows Vista, if an executable file requires elevation to launch, then the executable's icon should be "stamped" with a shield icon to indicate this fact. The executable's application manifest must mark "requireAdministrator" to designate the executable as requiring a full administrative access token. The shield icon overlay will also be automatically placed on executables that are deemed to require elevation as per the installer detection heuristics. For example, a file named setup.exe will automatically receive a shield icon overlay even if the executable does not have an embedded application manifest.

    0 讨论(0)
提交回复
热议问题