English 中文(简体)
RUNASADMIN in Registry doesnt seem to work in 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_USERSoftwareMicrosoftWindows NT
CurrentVersionAppCompatFlagsLayers", "C:Appapp.exe", "RUNASADMIN");

Thanks.

最佳回答

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/

问题回答

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_MACHINESOFTWAREMicrosoftWindows NTCurrentVersionAppCompatFlagsLayers]
"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_USERSoftwareMicrosoftWindows NTCurrentVersionAppCompatFlagsLayers]
"C:\Program Files (x86)\My Program\My Program.exe"="RUNASADMIN"

[HKEY_LOCAL_MACHINESOFTWAREMicrosoftWindows NTCurrentVersionAppCompatFlagsLayers]
"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.

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.

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).

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

reg ADD "HKLMSOFTWAREMicrosoftWindows NTCurrentVersionAppCompatFlagsLayers" /t REG_SZ /f /v "C:Program Files (x86)MyAppmyapp.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_MACHINESOFTWAREWow6432NodeMicrosoftWindows NTCurrentVersionAppCompatFlagsLayers!
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 "HKLMSOFTWAREMicrosoftWindows NTCurrentVersionAppCompatFlagsLayers" /t REG_SZ /f /v "C:Program Files (x86)MyAppmyapp.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).





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签