How do I configure my application to run as administrator automatically?
Solution 1
What you need to do is embed an application manifest into the EXE.
-
Save the following as a text file called App.exe.manifest:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0"> <trustInfo xmlns="urn:schemas-microsoft-com:asm.v3"> <security> <requestedPrivileges> <requestedExecutionLevel level="requireAdministrator" uiAccess="false"/> </requestedPrivileges> </security> </trustInfo> </assembly>
Download the Windows SDK.
-
Inside you'll find mt.exe i.e. the Manifest Tool. Invoke it as follows:
mt.exe -manifest "App.exe.manifest" -outputresource:"App.exe";#1
If there are no errors you're done. You can delete the manifest file and distribute just the EXE. Running it should make it automatically request admin access every time.
Solution 2
When creating your archive, Request Administrative Access.
Rafael Vidal
Updated on September 18, 2022Comments
-
Rafael Vidal over 1 year
I have created a patch file with an executable inside of it using Winrar SFX feature.
After executing the SFX file and the extraction ends up, my executable file will never run because it doesn't have admin privileges. I am wondering HOW I can grant administrator access to that file by some programming way (like a batch file).I know that I can right click it, go to "Properties", choose "Compatibility" tab and then tick the box "Execute as Administrator".
The problem is that the users who will download that patch doesn't know it (and my exe only runs when you right click it and choose "Open as administrator", otherway it will never open nor display the UAC popup).
I have tried some ways, like the "Elevator Runner (Elevate me)", etc, but I'm actually looking for something simpler than.
I just need to make the exe always run as administrator.-
Karan almost 11 yearsNote to mods: No migration to SO is required here.
-
-
Rafael Vidal almost 11 yearsMDMoore313 thanks for your time, but I guess I didn't explained myself well. I don't need to run SFX as admin. I need to run a specific file as admin ("my_app.exe"), which is located inside the SFX file. The point is that double clicking that file, won't make it run. The only way to run it is right clicking it and choose "Run as Administrator"
-
MDMoore313 almost 11 years@RafaelVidal does the SFX launch your application? If so, then your app will have admin rights. If not, I will update my answer.
-
Rafael Vidal almost 11 yearsNo, it doesn't launch the application. The user has to do it manually, so he may tick the "Run as Adminsitrator" box on the "Compatibility" tab or right-click "Run as Administrator" context menu
-
Rafael Vidal almost 11 yearsThe SFX do launches the file, but the users may run that file by themselves later on and then there will be no SFX to run it for then. That's why the exe need to be flagged as "run as admin"
-
Karan almost 11 years@MDMoore313: No programming and thus no migration required, see my answer.
-
Rafael Vidal almost 11 yearsI'll test it. By the way, is it possible to make this "Manifest" a bit better? Like doing it executing as Administrator without requesting Admin Access? Thanks
-
Karan almost 11 yearsMeaning you want it to silently elevate and gain admin access? Unless you turn off UAC that's not possible, otherwise what would be the point of UAC and don't you think every malware would be doing this?
-
Rafael Vidal almost 11 yearsI understand your point, but I know that there are ways...not easy for sure, but there are, like the "Elevate Me" program...Anyway, making the exe run as admin is enough for now. Really thank you in advance
-
Karan almost 11 yearsAll such programs to bypass the elevation prompt use the well-known Task Scheduler trick which requires admin access. Since you're looking to do this on any arbitrary PC where your SFX is executed, you can't silently create the scheduled task without the standard user's consent. In fact, if any program did this on my PC (silently create a scheduled task to bypass UAC), I would instantly classify it as malware and delete it.
-
Rafael Vidal almost 11 yearsPretty good Karan, worked perfectly. I would only suggest you to add to the answer the need of running CMD as ADMIN, otherwise it will not work...Thank you again
-
Karan almost 11 yearsYou're welcome, and it worked fine for me from a non-admin cmd prompt.
-
Rafael Vidal almost 11 yearsI guess I will need your help again Karan...it seems my application stopped to work after adding that manifest on it...
-
Karan almost 11 yearsA little more detail would help. "stopped to work" how exactly? Any error messages? Does the app create any logs?
-
Rafael Vidal almost 11 yearsI'm sorry Karan, it did work perfectly...my problem was another library...that library was conflicting with my app. Thank you for your perfect answer.
-
Karan almost 11 yearsGood to know! :)