Catalin
Posts: 6506
Joined: Wed Jun 13, 2018 7:49 am

Always run EXE as administrator / in compatibility mode

Mon Jan 20, 2020 11:36 am

Hello guys,

In the past, we've been asked how an EXE (e.g. your application) can always be run as administrator (or maybe in compatibility mode), similar to right clicking your EXE --> "Properties" --> "Compatibility" tab --> "Run this program as an administrator" option (or maybe "Run this program in compatibility mode for:" option).

Unfortunately, Advanced Installer does not have predefined support for such a task, but due to the requests on this subject in the past, I have decided to create a step-by-step How-To which hopefully will be of help for you when trying to achieve this.

As mentioned above, Advanced Installer does not have predefined support for this task and, unless you are the creator of the application and are willing to modify the code of your application to achieve this (which will probably consume quite some time), this can be quite confusing. As previously discussed, the Windows OS have predefined support for this. In order to achieve this manually, you can proceed as it follows:

- right click on your .EXE file --> "Properties" --> "Compatibility" tab --> "Run this program as an administrator" ("Run this program in compatibility mode for:")

However, the question now is: How can we achieve this in a programmatic way?

Well, first of all, it is important to understand the fact that pretty much any change you bring to your Windows system, it will most probably be reflected in the registries. The above options are no exception of this rule.

You can see the changes (for instance, if you manually check one of the options above) by opening the Registry Editor (regedit.exe) and navigating to the following keys:

Code: Select all

"HKCU:\Software\Microsoft\Windows NT\CurrentVersion\AppCompatFlags\Layers"
or

Code: Select all

"HKLM:\Software\Microsoft\Windows NT\CurrentVersion\AppCompatFlags\Layers"
With this in mind, achieving this in a programmatic way basically means having a little script that will add the registry value for us.
RunAsAdminRegReflection.png
RunAsAdminRegReflection.png (127.33KiB)Viewed 35341 times

As it can be seen in the screenshot, the "RUNASADMIN" value is added for the "Run this program as an administrator" option.

To achieve this in a programmatic way, we can either use a script (e.g. PowerShell, VBS) or a .DLL/.EXE (C#). For instance, here is how a PowerShell script that does that would look:

Code: Select all

# the registry key that stores the option
$runAsAdminReg = "HKCU:\Software\Microsoft\Windows NT\CurrentVersion\AppCompatFlags\Layers"

# the path to our .EXE application
$name = "C:\Users\Catalin\Desktop\sample.exe"

# the registry value for our option
$value = "RUNASADMIN"

# the cmdlet that adds the value
New-ItemProperty -Path $runAsAdminReg -Name $name -Value $value -PropertyType string -Force | Out-Null
Using the above script, we can also force the program to run in compatibility mode. All we have to do is to change the value that is added to the registries. The $value variable could take one of the following values:
  • WIN95
  • WIN98
  • WIN4SP5
  • WIN2000
  • WINXPSP2
  • WINXPSP3
  • VISTARTM
  • VISTASP1
  • VISTASP2
  • WIN7RTM
  • WINSRV03SP1
  • WINSRV08SP1
  • WIN8RTM
Now the question would be how can we implement this in Advanced Installer. Well, this can be added as a "Run PowerShell Inline script" custom action, with sequence (simply press the "Add custom action with sequence" button which is placed to the right side of the custom action's name)

The custom action should be scheduled after the "Add Resources" action group, with its execution time set to "When the system is being modified (deferred)"

If you are not sure whether your setup will be installed on a 32-bit machine or a 64-bit machine, you could set the "Platform" to "Auto".

If you do not want to use a hard-coded path in your script, you can make use of the installer properties.

For instance, if the .EXE is added in the "Files and Folders" page, under the "Application Folder" folder, your script can look like this:
FinalCA.png
FinalCA.png (169.96KiB)Viewed 35341 times

Basically, what we are doing above is taking the path that is stored in the "APPDIR" property and then add the name of our .EXE to the path.

Important: In order for the script to successfully execute, the setup must be run with administrative privileges, because writing to the registries require administrative privileges.

Hope this helps.

Best regards,
Catalin
Catalin Gheorghe - Advanced Installer Team
Follow us: Twitter - Facebook - YouTube

clint
Posts: 135
Joined: Fri Jun 19, 2009 9:46 am
Location: Germany

Re: Always run EXE as administrator / in compatibility mode

Mon Jan 20, 2020 1:46 pm

Excellent!
Thank you!

clint

Catalin
Posts: 6506
Joined: Wed Jun 13, 2018 7:49 am

Re: Always run EXE as administrator / in compatibility mode

Mon Jan 20, 2020 2:54 pm

You are always welcome, Clint!

I am really glad this was of help for you.

To be fully honest with you, I did not expect to receive feedback so soon on this. :D

Best regards,
Catalin
Catalin Gheorghe - Advanced Installer Team
Follow us: Twitter - Facebook - YouTube

Return to “Sample Projects”