How do I configure my application to run as administrator automatically?

27,062

Solution 1

What you need to do is embed an application manifest into the EXE.

  1. 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>
    
  2. Download the Windows SDK.

  3. 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
    
  4. 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.

Source

Solution 2

When creating your archive, Request Administrative Access.enter image description here

Share:
27,062
Rafael Vidal
Author by

Rafael Vidal

Updated on September 18, 2022

Comments

  • Rafael Vidal
    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.

  • Rafael Vidal
    Rafael Vidal almost 11 years
    MDMoore313 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
    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
    Rafael Vidal almost 11 years
    No, 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
    Rafael Vidal almost 11 years
    The 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
    Karan almost 11 years
    @MDMoore313: No programming and thus no migration required, see my answer.
  • Rafael Vidal
    Rafael Vidal almost 11 years
    I'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
    Karan almost 11 years
    Meaning 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
    Rafael Vidal almost 11 years
    I 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
    Karan almost 11 years
    All 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
    Rafael Vidal almost 11 years
    Pretty 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
    Karan almost 11 years
    You're welcome, and it worked fine for me from a non-admin cmd prompt.
  • Rafael Vidal
    Rafael Vidal almost 11 years
    I guess I will need your help again Karan...it seems my application stopped to work after adding that manifest on it...
  • Karan
    Karan almost 11 years
    A little more detail would help. "stopped to work" how exactly? Any error messages? Does the app create any logs?
  • Rafael Vidal
    Rafael Vidal almost 11 years
    I'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
    Karan almost 11 years
    Good to know! :)