Archive: Vista - Check Box to Run File As Administrator


Vista - Check Box to Run File As Administrator
This pertains to NSIS and Vista

I can use NSIS to make an .exe run with Admin privileges when I install and then launch.

However, if you right click the .exe (or file), the checkbox for "Run As Administrator" is not checked.

Thus, when you reboot, you cannot launch the .exe as a user, you need to be an administrator. This breaks my app.

Does anyone here have any ideas on how to make this work? Much thanks in advance.


http://stackoverflow.com/questions/2...-windows-vista

Stu


Vista - Check Box to Run File As Administrator and Set Compatibility mode
Thanks, Stu...this is part of the answer that I was looking for! I tested the suggested fix last night and it worked.

In case anyone else needs set the Compatibility mode as well, you can set it in the same registry subkey!

For those that write data to HKLM, I have verified that there does not need to be an entry in HKLM as this does not appear to do anything, it has to be in HKCU! I also do not see any problems with creating the subkey on non-Vista machines as everything appears to be working just fine on my XP machine with the registry subkey.

; Sets the Compatibility mode to "Windows XP (Service Pack 2)" and the Privilege level to "Run this program as an administrator"

WriteRegStr? HKCU "Software\Microsoft\Windows NT\CurrentVersion?\AppCompatFlags?\Layers" "$INSTDIR\HelloWorld.exe" "WINXPSP2 RUNASADMIN"

The following is for your uninstaller so that the registry subkeys are removed:

; Removes the subkey that sets the Compatibility mode to "Windows XP (Service Pack 2)" and the Privilege level to "Run this program as an administrator"

DeleteRegValue? HKCU "Software\Microsoft\Windows NT\CurrentVersion?\AppCompatFlags?\Layers" "$INSTDIR\HelloWorld.exe"

If you need to change the compatibility mode to something other than "Windows XP (Service Pack 2)", follow the steps below to find out what is being written to the registry and use that instead of "WINXPSP2"!

For the non-believers, before adding this code to your installer, you can test this by running regedit and verifying if the Software\Microsoft\Windows NT\CurrentVersion?\AppCompatFlags?\Layers key already exists in both the HKLM and HKCU. You will probably see the key under HKLM and no key under HKCU or at least that was what I experienced on my work and home laptops.

On a Vista machine, once you verify that the key \ subkeys are not there right click an executable and manually set the Compatibility mode and the Privilege level. Refresh the Registry Editor window and you will see that a subkey has been added for that application in HKCU! Delete the subkey in the Registry Editor window or manually uncheck the Compatibility mode and the Privilege level to remove the subkey. Add the code to your installer, compile and run the new installer. Once your program has finished installing you should notice that the Compatibility mode and the Privilege level are already set.