kn0w0n3
kn0w0n3

Reputation: 189

How to run application as administrator in Qt

How can I start my Qt application as administrator or get a prompt when admin privilege is needed? I have tried everything in the posts that I could find about it and most of them are a few years old. Nothing has worked and everyone seems to have a different spin on it. I have downloaded the windows SDK to get mt.exe, created the manifest files, the RC file and every other methods including right click and set the application to start in administrator mode. Nothing has worked. I need it because a task in my program requires administrator privilege. QFile::errorString() says access denied. Does anyone know how to make this work?

Upvotes: 1

Views: 6579

Answers (4)

Vadym
Vadym

Reputation: 197

Works for me (add in your_project.pro):

win32 {
  CONFIG += embed_manifest_exe
  QMAKE_LFLAGS_WINDOWS += /MANIFESTUAC:"level='requireAdministrator'"
}

Upvotes: 2

vivi
vivi

Reputation: 334

Easiest way is config at project Run configuration, adding this env variable:

__COMPAT_LAYER=RUNASINVOKER

then cleanup and rebuild and run again the proj, it will work.

Upvotes: 0

kn0w0n3
kn0w0n3

Reputation: 189

Okay so I found the answer to my question. I had found some solutions before, but they were not working. I found the reason why they were not working. The reason was very stupid: It was because the folder that my files were in had a space between the first and second word like (my app) which causes an error about not being able to find the location.It should be (myapp) with no spaces. The error about location not found was only happening when trying to add the adim privileges. Otherwise everything worked fine and there were no errors. So here is the solution and make sure there are no spaces in folder names.

1) Create the rc file :Open notepad and paste the following text inside then save the file as yourappname.rc This creates the rc file. Put the file in the folder that has all of your sourcecode and pro file.

#include <windows.h>
CREATEPROCESS_MANIFEST_RESOURCE_ID RT_MANIFEST "yourappname.exe.manifest"

2) Create the manifest file with notepad, paste the following text, and save it as yourappname.exe.manifest and put it in the same location as above.

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0">
  <assemblyIdentity version="2.0.0.0" processorArchitecture="X86"
  name="yourappname.yourappname" type="win32" />
  <description>A discription of your app</description>
  <dependency />
  <!-- Identify the application security requirements. -->
  <trustInfo xmlns="urn:schemas-microsoft-com:asm.v2">
    <security>
      <requestedPrivileges>
        <requestedExecutionLevel
          level="requireAdministrator"
          uiAccess="false"/>
        </requestedPrivileges>
       </security>
   </trustInfo>
</assembly>

3) In your pro file put the following text:

win32 {
    RC_FILE = yourappname.rc
}

That's it. Now your application should start with administrator privileges.

There are similar post that describe this process too. Just make sure there are no spaces in your folder names or it won't work. Also you MUST run qt with administrator privileges for it to work. Right click Qt and run as administrator before trying to compile.

Upvotes: 4

mrg95
mrg95

Reputation: 2418

To get the require admin prompt to display, I simply added

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

to my qmake file. I use this in my projects all the time.

Upvotes: 1

Related Questions