Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RUNASADMIN in Registry doesnt seem to work in Windows 7

Tags:

c#

uac

windows-7

For a while now the installer for my program has used the below code to make my app run with administer privileges. But it seems to have no effect under Windows 7. In Vista it worked beautifully. If I right click the shortcut and tell it to run as Administer, the program will start fine. But by using the below, code it should be made to run the program that way all the time. It doesn't anymore. Does anyone know if Win 7 still uses this key? UAC is also on by the way.

Registry.SetValue(@"HKEY_CURRENT_USER\Software\Microsoft\Windows NT\
CurrentVersion\AppCompatFlags\Layers", "C:\App\app.exe", "RUNASADMIN");

Thanks.

like image 421
JimDel Avatar asked Nov 12 '09 23:11

JimDel


5 Answers

I have an answer/workaround for this question.

First off, I disagree (respectfully) with the comment that using the AppCompatFlags is not a "proper way to configure your application and installer." Modifying this section of the registry is simply mirroring using the Windows GUI to change the Privilege Level of the executable. I find this method easier to implement than adding a manifest file. If the user wants or needs to change the Privilege Level to not Run as Administrator, they can do that easily with the GUI.

Anyway, I had this same problem of trying to set the Privilege Level of the executable to Run as administrator. We know that we can set it with the the GUI:

  • Right-click on the shortcut or .EXE file and select Properties
  • Click on the Compatibility tab
  • (At this point you can set the Privilege Level for just you or for all users; I prefer doing it for all users)
  • Click the button, Change settings for all users
  • A new Properties window is open with a tab titled "Compatibility for all users"
  • Under Privilege Level check on "Run this program as an administrator," click OK a couple of times to save the changes.

When the changes are saved, you will find the setting in the registry:

[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\AppCompatFlags\Layers]
"C:\\Program Files (x86)\\My Program\\My Program.exe"="RUNASADMIN"

When I set the .exe to run as administrator using the GUI in this way, it always works.

However, whenever I tried to change the registry directly without going through the GUI, the program just won't run as administrator. The registry shows that I made the change and when I look at the Privilege Level for the executable, Run as administrator is checked as on.

I tried several different ways to make the .exe run as an administrator by just changing the registry:

  • Manually edited the registry with regedit
  • Imported the changes from a .reg file
  • Used the command line tool reg.exe to change the registry
  • Used the now defunct Wise Script tool
  • Used AutoIT Scripting

All these methods did the same thing. The registry was changed and the GUI showed the that program should run as an administrator, but the program never runs as an administrator.

The fix that for this problem that I stumbled across is to go ahead and change both the HKCU key and the HKLM key with the setting.

[HKEY_CURRENT_USER\Software\Microsoft\Windows NT\CurrentVersion\AppCompatFlags\Layers]
"C:\\Program Files (x86)\\My Program\\My Program.exe"="RUNASADMIN"

[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\AppCompatFlags\Layers]
"C:\\Program Files (x86)\\My Program\\My Program.exe"="RUNASADMIN"

If you change both of these registry sections, then the .exe will run as an administrator. More importantly, if a different user logs in to the the PC, the program will run as an administrator. This is in spite of the registry change not being made HKCU section for the subsequent user.

I don't know what is going on here, but it is working.

like image 50
Doug Green Avatar answered Oct 11 '22 18:10

Doug Green


I am using Windows 7 and I can see such keys. However, I don't think that's a proper way to configure your application and installer.

My recommendation is that you distribute a manifest file along with your application (app.exe). The manifest file can be even embedded in the executable easily if you are using Visual Studio 2008. Then in the manifest file you can require administrator rights.

http://msdn.microsoft.com/en-us/library/bb756929.aspx

http://blogs.msdn.com/shawnfa/archive/2006/04/06/568563.aspx

http://channel9.msdn.com/posts/jmazner/How-To-Tell-Vistas-UAC-What-Privelege-Level-Your-App-Requires/

like image 43
Lex Li Avatar answered Oct 11 '22 17:10

Lex Li


in win7 , RUNASADMIN IS PLACED IN KEY : when HKLM\\SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\AppCompatFlags\\Layers using install shield 5.1 , the values are copied to the appcpmctflgsin wow6432node and exe actualy falis to run as admin.

like image 30
uss Avatar answered Oct 11 '22 18:10

uss


This answer by RobeN works "for an exe file I didn't create," which you expressed as an interest in your comment on Lex Li's answer. It uses your original registry idea.

Two possibly relevant differences:

  1. With a 32-bit OS, I need not worry about the Wow6432Node (a concern mentioned by "uss")
  2. By using HKLM instead of HKCU, I need not worry about which user is executing the application (a concern mentioned by Leo).
like image 28
Pete Avatar answered Oct 11 '22 16:10

Pete


I used CMD for adding this entry into Registry using following command:

reg ADD "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\AppCompatFlags\Layers" /t REG_SZ /f /v "C:\Program Files (x86)\MyApp\myapp.exe" /d RUNASADMIN

This works good in Win8Pro-32Bit but not works on 64Bit version!
I found that running this command on a 64Bit Win (runs through a 32-bit installer as final install stage), causes to creating entry on HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Windows NT\CurrentVersion\AppCompatFlags\Layers!
After some research (thank's to my friend Mr. H.Toosi), we found right solution.
JUST ADD /reg:64 AT END OF THE EARLIER COMMAND like this:

reg ADD "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\AppCompatFlags\Layers" /t REG_SZ /f /v "C:\Program Files (x86)\MyApp\myapp.exe" /d RUNASADMIN /reg:64

and everything is normal in both 32 and 64 bit OSes(Win 7 32Bit, Win 8 32Bit, Win 8.1 64Bit).

like image 32
S.M.Mousavi Avatar answered Oct 11 '22 16:10

S.M.Mousavi