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
With this in mind, achieving this in a programmatic way basically means having a little script that will add the registry value for us.
Code: Select all
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:
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:
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
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:
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.