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

9

8

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

Posted 2013-06-07T13:19:20.940

Reputation: 223

2

Note to mods: No migration to SO is required here.

– Karan – 2013-06-07T17:28:06.260

Answers

15

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

Karan

Posted 2013-06-07T13:19:20.940

Reputation: 51 857

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 – Rafael Vidal – 2013-06-07T21:43:43.093

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? – Karan – 2013-06-07T21:45:30.447

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 – Rafael Vidal – 2013-06-07T22:04:39.667

1

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.

– Karan – 2013-06-07T22:19:26.720

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 – Rafael Vidal – 2013-06-13T00:26:39.713

You're welcome, and it worked fine for me from a non-admin cmd prompt. – Karan – 2013-06-13T17:05:04.360

I guess I will need your help again Karan...it seems my application stopped to work after adding that manifest on it... – Rafael Vidal – 2013-06-14T07:23:05.433

A little more detail would help. "stopped to work" how exactly? Any error messages? Does the app create any logs? – Karan – 2013-06-14T14:38:23.563

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. – Rafael Vidal – 2013-06-29T03:19:21.947

Good to know! :) – Karan – 2013-06-29T03:21:57.333

2

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

MDMoore313

Posted 2013-06-07T13:19:20.940

Reputation: 4 874

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" – Rafael Vidal – 2013-06-07T14:40:21.953

@RafaelVidal does the SFX launch your application? If so, then your app will have admin rights. If not, I will update my answer. – MDMoore313 – 2013-06-07T14:44:16.567

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 – 2013-06-07T14:45:34.323

@RafaelVidal is there a reason you don't want WinRAR to launch the exe, and just have the user run the SFX itself? it seems like that would accomplish the same goal and solve your admin issue. If you're still focused on flagging your program to run as admin, you would have better chances of an answer on stack overflow (flag your question for migration) – MDMoore313 – 2013-06-07T15:33:09.147

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" – Rafael Vidal – 2013-06-07T16:20:48.710

How do I flag this topic to be moved for migration? – Rafael Vidal – 2013-06-07T16:22:03.763

I've flagged it for you @RafaelVidal. Be sure to edit your question to reflect that you want to program the exe to be run as admin. – MDMoore313 – 2013-06-07T16:24:45.793

@MDMoore313: No programming and thus no migration required, see my answer. – Karan – 2013-06-07T17:13:41.347