Hi Arrestme, and thank you for joining Seven Forums,
And nice image there.
Those compatibility configurations are saved to the registry. Specifically to the key of
- HKCU\Software\Microsoft\Windows NT\CurrentVersion\AppCompatFlags\Layers
or instead
- HKLM\Software\Microsoft\Windows NT\CurrentVersion\AppCompatFlags\Layers
if the settings should apply for all users. Note that that second location, in HKLM, is a virtual key (a.k.a. "hidden registry key") so don't expect to be able to search it through RegEdit.
When a compatibility setting is configured on an application, the path of that executable is written to the according registry key, as a value. And the value's data contains the compatibility configuration information for that executable, in the form of a space separated list of strings, each string called a 'flag'.
The flag for configuring an application to run with administrative rights is 'RUNASADMIN'.
E.g. Instead of using the gui to set the "Run this program as an administrator" compatibility setting for all users, one could alternatively choose to run the command
Code:
reg add "HKLM\Software\Microsoft\Windows NT\CurrentVersion\AppCompatFlags\Layers" /v "C:\path\to\FL.exe" /t "REG_SZ" /d "RUNASADMIN"
to succeed the same effect. (N.B., generally, writing to the HKLM hive requires local administrative rights, so an elevated command prompt would be needed.)
Can I make a .bat file or must I do a wscript for that? (I can then take that & add into context menu to my preference, but not important)
Yes. If you can do the adding-to-the-context-menu part, I may be able to create some VBScript that aptly writes to the registry for you.
However, unfortunately, you'll have to abandon the idea of being able to configure the "Run this program as an administrator" compatibility setting for all users, because you'll have a hard time getting the script to run as administrator, launching it from the context menu (as 'HKLM\Software\...\Layers' requires administrative permissions to modify).
Toggle Run as Admin.vbs
Code:
' ToggleRunAsAdmin.vbs ExeFile
' Toggle the "Run this program as an administrator" compatibility setting on 'ExeFile'.
Function PromptForElevation
If Not CreateObject("WScript.Shell").Run("Net Sess", 0, True) = 0 Then
CreateObject("Shell.Application").ShellExecute "WScript", _
"""" & WScript.ScriptFullName & """" & " " & """" & WScript.Arguments.Item(0) & """", "","RunAs",1
WScript.Quit
End If
End Function
If WScript.Arguments.Unnamed.Count = 0 Then WScript.Quit(1)
If WScript.Arguments.Item(0) = "" Then WScript.Quit(1)
Const HKCU = &H80000001 'HKEY_CURRENT_USER
Const HKLM = &H80000002 'HKEY_LOCAL_MACHINE
'PromptForElevation()
Set reLetters = New RegExp
reLetters.Pattern = "[a-z]"
reLetters.IgnoreCase = True
reLetters.Global = True
Set reRunAsAdmin = New RegExp
reRunAsAdmin.Pattern = " *RUNASADMIN *"
reRunAsAdmin.IgnoreCase = True
reRunAsAdmin.Global = True
hDefKey = HKCU
strSubKey = "Software\Microsoft\Windows NT\CurrentVersion\AppCompatFlags\Layers"
strValue = WScript.Arguments.Item(0)
strData = "RUNASADMIN"
Set objReg = GetObject("WinMgmts:\\.\root\default:StdRegProv")
objReg.GetStringValue hDefKey, strSubKey, strValue, ExistingData
If IsNull(ExistingData) Then
objReg.SetStringValue hDefKey, strSubKey, strValue, strData
Else
If reLetters.Test( Replace(ExistingData, strData, "") ) Then
If reRunAsAdmin.Test( ExistingData ) Then
objReg.SetStringValue hDefKey, strSubKey, strValue, reRunAsAdmin.Replace(ExistingData, "")
Else
objReg.SetStringValue hDefKey, strSubKey, strValue, ExistingData & " " & strData
End If
Else
objReg.DeleteValue hDefKey, strSubKey, strValue
End If
End If