Run the Qt application as administrator on Windows
Asked Answered
M

3

6

Is there a way I can run the Qt application as an administrator? I have an auto-updater for my application. It needs administrator privileges to replace the files in Program Files folder and hence it requires administrator privileges.

Ms answered 29/5, 2012 at 16:13 Comment(7)
You have written an application which needs administrator privileges to run? There is nothing Qt specific about running an application as administrator.Tardiff
@Tardiff Yes, I have an application which needs administrator privileges to run.Ms
You could either right-click the application and set it to run as administrator under Compatibility > Privilege Level. Or you can embed a manifest with the appropriate execution level. Qt Creator is your IDE? Or Visual Studio?Tardiff
@Tardiff I would like to do it programmatically. QtCreator is my IDE. I think that I need to use manifest file like you suggest.Ms
Yes. This will already help: qtcentre.org/threads/… The level you're looking for in your manifest is "requireAdministrator".Tardiff
@Tardiff Please post the last comment as the answer, I will accept it. Thanks!Ms
This 2016 post demonstrates how to do it in Qt Creator.Turnery
T
12

Running your application with administrator privileges does not have a whole lot to do with Qt. There are two approaches.

The "simple" one is to manually set your application to run with administrator privileges. You can do so by right-clicking on the executable. Then on the "Compatibilty" tab, you can choose to "Run this application as an administrator" under "Privilege level".

However, if you automatically want to achieve the same, you will have to embed a manifest into your application. What you're looking for is to set the requestedExecutionLevel to requireAdministrator. A bit more information can be found on MSDN or in this Wikipedia entry on UAC.

For your application as built in Qt Creator, it means you will need to embed the manifest by including a reference to it in a Resource (.rc) file. This resource file can then be added to your .pro file by specifying RC_FILE = myapp.rc. An informative blog post on this very issue is this one, as well as this post on the QtCentre forum.

Tardiff answered 29/5, 2012 at 20:41 Comment(0)
P
4

A very simple solution for this, if you're using MSVC toolkit, is to add the following into the project file:

QMAKE_LFLAGS_WINDOWS += "/MANIFESTUAC:\"level='requireAdministrator' uiAccess='false'\""

I am using Qt 5.12 msvc2017.

I've found this to be quite neat, as from what I see in generated Makefile, Qt is already adding some manifest related link flags, and this approach wouldn't interfere with already embedding manifest, as manually adding manifest from existing file.

Other manifest link options can be easily added. You can read the docs for VS compiler, and/or you can check what flags/options Visual Studio IDE has to offer in Project properties/Linker/Manifest File and then check Command Line section of the Linker to see how it adds them.

Not sure how would this be done in gcc or clang builds, it would probably require solutions provided by @H Aßdøµ, and @Bart.

Plutus answered 15/9, 2019 at 22:20 Comment(2)
I add that line where, exactly?Claret
@Claret you add it to the project file, it's qmake. I also edited the answer.Plutus
C
1

From the article that referred to Mr @Bart:

Application Manifest

First, we have to prepare an application manifest file. This one below is for application that does not require administrator rights:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0">
  <assemblyIdentity version="2.0.2.0" processorArchitecture="X86" type="win32"/>
  <trustInfo xmlns="urn:schemas-microsoft-com:asm.v3">
    <security>
      <requestedPrivileges>
        <requestedExecutionLevel level="asInvoker" uiAccess="false"/>
      </requestedPrivileges>
    </security>
  </trustInfo>
</assembly>

Second, we need the MT.exe tool from the Microsoft Windows SDK to embed this XML in our executable. To do it use the following command:

mt.exe –manifest MyApp.exe.manifest -outputresource:MyApp.exe;1

Automatic Manifest Embedding

Manually executing the mt command after each compilation is a tedious task. What about convincing qmake to do it for us? After studying the docs it looks like the following line should do the trick:

win32 {
    WINSDK_DIR = C:/Program Files (x86)/Microsoft SDKs/Windows/v7.0A
    WIN_PWD = $$replace(PWD, /, \\)
    OUT_PWD_WIN = $$replace(OUT_PWD, /, \\)
    QMAKE_POST_LINK = "$$WINSDK_DIR/bin/x64/mt.exe -manifest $$quote($$WIN_PWD\\$$basename(TARGET).manifest) -outputresource:$$quote($$OUT_PWD_WIN\\${DESTDIR_TARGET};1)"
}

The above code will automatically execute the mt.exe program from WINSDK_DIR and embed a manifest file that is located in the project root directory and named after project's target (ie. MyApp.manifest). That's all to adding a manifest, now let's move on and specify the version information.

Orginal post: http://blog.strixcode.com/2010/08/embedding-application-manifest-and.html

Carmellacarmelle answered 28/1, 2017 at 0:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.