Run the Qt application as an administrator on Windows

Is there a way to run the Qt application as an administrator? I have an auto-updater for my application. To replace the files in the Program Files folder, you must have administrator rights, so this requires administrator rights.

+4
source share
2 answers

Running an application with administrator privileges has little to do with Qt. There are two approaches.

"Simple" is to manually configure the application to run with administrator rights. You can do this by right-clicking on the executable file. Then, on the "Compatibility" tab, you can select "Run this application as administrator" in the "Privilege Level" section.

However, if you automatically want to achieve the same, you will have to implement the manifest in your application. What you are looking for is to set the requestedExecutionLevel to requireAdministrator . A little more information can be found on MSDN or this entry on Wikipedia at UAC .

For your application, as built into Qt Creator, this means that you need to embed the manifest by including a link in the 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.

+10
source

From an article regarding Mr. @ Bart:

Application manifest

First we need to prepare the application manifest file. This is below for an 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> 

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

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

Automatic manifest injection

Manually executing the mt command after each compilation is a tedious task. How about persuading qmake to do this for us? After studying the documents, it looks as follows:

 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 attach the manifest file, which is located in the project root directory and named after the project goal (i.e. MyApp.manifest). For all of this to add a manifest, now go and provide version information.

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

0
source

Source: https://habr.com/ru/post/1414935/


All Articles