What does '__COMPAT_LAYER' actually do?
Asked Answered
V

1

33

Recently, i was trying to give my application administrator rights without system asking for "Do you want to give administrator rights?" and i found a way which is working perfectly.

Solution I Found

I created a bat file named nonadmin.bat and wrote the below code in it

cmd min C set __COMPAT_LAYER=RunAsInvoker && start "" %1

and if we drag any exe on it, it gives them administrator rights (before it was not letting me access environment variables without it but after draging the file on bat it did work).

Question

Now my question is:-

  1. What actually '__COMPAT_LAYER' means and what does it do?
  2. How do i remove such a thing so that it asks for administrator rights again?
  3. Does this reduce system security?
Vaca answered 17/6, 2016 at 9:38 Comment(4)
It does what is says. Sets compatibility options. Your examples will only work for administrators. Non admins will error.Novellanovello
@Noodles can you please elaborate and explain the answer of those 3 questions.Vaca
Presumably that should read cmd /min /c ...?Salmanazar
Please @Agent_Spock, always put "" after start in cmd, because if %1 is quoted, then start will interpret it as a window title. Try start "C:\Program Files\whatever" vs start "" "C:\Program Files\whatever".Infusorian
J
60

__COMPAT_LAYER, and How To Use It
__COMPAT_LAYER is a system environment variable that allows you to set compatibility layers, which are the settings you can adjust when you right-click on an executable, select Properties, and go to the Compatibility tab.

Imgur

There are several options to choose from in addition to the one you know about:

  • 256Color - Runs in 256 colors
  • 640x480 - Runs in 640x480 screen resolution
  • DisableThemes - Disables Visual Themes
  • Win95 - Runs the program in compatibility mode for Windows 95
  • Win98 - Runs the program in compatibility mode for Windows 98/ME
  • Win2000 - Runs the program in compatibility mode for Windows 2000
  • NT4SP5 - Runs the program in compatibility mode for Windows NT 4.0 SP5
  • VISTARTM - Runs the program in compatibility mode for Windows Vista
  • VISTASP1 - Runs the program in compatibility mode for Windows Vista (Service Pack 1)
  • VISTASP2 - Runs the program in compatibility mode for Windows Vista (Service Pack 2)
  • WIN7RTM - Runs the program in compatibility mode for Windows 7
  • WIN8RTM - - Runs the program in compatibility mode for Windows 8
  • DISABLEDXMAXIMIZEDWINDOWEDMODE - Disables fullscreen optimizations
  • TRANSFORMLEGACYCOLORMANAGED - Uses the legacy display ICC color management

You can use multiple options by separating them with a space: set "__COMPAT_LAYER=Win98 640x480"

Unsetting the __COMPAT_LAYER Variable
These settings persist for as long as the variable exists. The variable stops existing when either the command prompt in which the variable was set is closed, or when the variable is manually unset with the command set __COMPAT_LAYER=.

Since you are setting the variable via batch script, the variable is automatically unset once the executable you drag onto it completes and the script closes. It is important to note that the variable settings persist to any child processes that are spawned by the executable you select.

The Security of Using __COMPAT_LAYER
Setting __COMPAT_LAYER to RunAsInvoker does not actually give you administrator privileges if you do not have them; it simply prevents the UAC pop-up from appearing and then runs the program as whatever user called it. As such, it is safe to use this since you are not magically obtaining admin rights.

You can also set the variable to RunAsHighest (only triggers UAC if you have admin rights, but also does not grant admin rights if you do not have them) or RunAsAdmin (always triggers UAC).

Jagged answered 17/6, 2016 at 12:21 Comment(11)
Thank you for the detailed explanation my friend but i need to know 2 things. 1) I have read it is an environment variable but whenever i search for it in the environment variables there is no such thing mentioned there. 2) As you said to unset it i need to write "set __COMPAT_LAYER=" OR "set __COMPAT_LAYER=NULL" but when i execute these commands the UAC prompt does not reappear. What should i do to make them reappear?Vaca
I'm unable to replicate your problem, but as I say in my last sentence, you should be able to say set __COMPAT_LAYER=RunAsAdminJagged
@Agent_Spock: some environment variables exist by default, some only exist if you set them; this is one of the latter. Also, if double-clicking the executable in question no longer brings up the UAC prompt, that may indicate that it doesn't contain a manifest telling Windows whether it requires elevation or not. When that happens, Windows makes a guess. So perhaps Windows has noticed that you successfully ran it as non-admin, and decided to follow suite. At any rate, unless you permanently set the environment variable in the registry or via the control panel, that isn't the cause.Claudclauddetta
If it actually is the kind of variable that only exists when you set it, it should stop existing when the command prompt in which it was set (and all applications that were owned by that prompt) is closed. Unless they use setx for some reason or something.Jagged
Before i applied the code i wasn't able to get environment variable as it was saying that you need administrator privilege. Then i dragged my exe file on the "nonadmin.bat" and then i was able to get the values of environment variable. But now when i "__COMPAT_LAYER" as blank or null the UAC prompt will not appear. What should i do?Vaca
@Jagged i have already set __COMPAT_LAYER to RunAsAdmin but now i want my UAC prompt back. Can you help me with this?Vaca
No, I can't; what I've suggested works for me and so the fact that it's not working for you suggests something is very, very wrong.Jagged
@Vaca just an idea, albeit late. Have you checked that there is no cached PCA data (by the name of the path to the .exe) underneath HKCU\Software\Microsoft\Windows NT\CurrentVersion\AppCompatFlags\Compatibility Assistant\Store? Just manipulating the elevation/no elevation behavior can also be done by placing an external manifest. Let's say you started the .exe before you place the manifest and UAC requested elevation, now that info will be cached. If you now place the external manifest and start again, you'll get an error (740/NT: 0xC000042CL). Remove the cached entry ... works.Ink
Recently I am seeing __COMPAT_LAYER=DetectorsMessageBoxErrors for .NET Core 3.1 WPF applications. I never setup any compatibilty settings for the application. What does DetectorsMessageBoxErrors come from and what does it mean?Metallurgy
There are more options than the ones listed here. I just searched on my system for the __COMPAT_LAYER string, and found one file containing the two lines: __APPCOMPAT_MANIFEST= and __COMPAT_LAYER=VistaSetup DetectorsAppHealth DetectorsShimLog Installer.Crust
I'm not surprised that there are more, but those aren't options that can be picked on the Compatibility properties tab.Jagged

© 2022 - 2024 — McMap. All rights reserved.