How to detect whether Vista UAC is enabled?

后端 未结 8 1382
渐次进展
渐次进展 2021-01-31 04:48

I need my application to behave differently depending on whether Vista UAC is enabled or not. How can my application detect the state of UAC on the user\'s computer?

8条回答
  •  轮回少年
    2021-01-31 05:15

    For anyone else that finds this and is looking for a VBScript solution. Here is what I came up with to detect if UAC is enabled and if so relaunch my script with elevated privileges. Just put your code in the Body() function. I found there were problems with transportability between XP and Windows 7 if I wrote code to always launch elevated. Using this method I bypass the elevation if there is no UAC. Should also take into account 2008 and above server versions that have UAC enabled.

    On Error Resume Next
    UACPath = "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System\EnableLUA"
    Dim WshShell
    Set WshShell = CreateObject("wscript.Shell")
    UACValue = WshShell.RegRead(UACPath)
    If UACValue = 1 Then
    'Run Elevated
        If WScript.Arguments.length =0 Then
          Set objShell = CreateObject("Shell.Application")
          'Pass a bogus argument with leading blank space, say [ uac]
          objShell.ShellExecute "wscript.exe", Chr(34) & _
          WScript.ScriptFullName & Chr(34) & " uac", "", "runas", 1
          WScript.Quit
        Else 
            Body()
        End If
    Else
    Body()
    End If
    
    Function Body()
    MsgBox "This is the body of the script"
    End Function
    

提交回复
热议问题