Tuesday, February 7, 2017

Run Qt application as administrator

If you are trying to run your Qt application with administrator right on Windows you need to do some steps:

1. In your .pro file remove the default manifest generated by Qt because you will get duplicate entry error in linker so do this:
CONFIG -= embed_manifest_exe
2. Create a resource file(.rc) and add it to the .pro file:
win32
{
    RC_FILE = App.rc
}
3. Prepare your resource file:
#include <windows.h>

ID_ICON ICON DISCARDABLE "AppIcon.ico"
CREATEPROCESS_MANIFEST_RESOURCE_ID RT_MANIFEST "App.exe.manifest"
4. Prepare your manifest file (App.exe.manifest):

<?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="my.App" type="win32" />
  <description>My application description </description>
  <dependency />
  <!-- Identify the application security requirements. -->
  <trustInfo xmlns="urn:schemas-microsoft-com:asm.v3">
    <security>
      <requestedPrivileges>
        <requestedExecutionLevel
          level="requireAdministrator"
          uiAccess="false"/>
        </requestedPrivileges>
       </security>
   </trustInfo>
   <!-- padding to four-byte multiple file size including the byte order mark -->
  <!-- padding 123 -->
</assembly>

Done. Qt 5.7, Windows 7, MSVC2015.

No comments:

Post a Comment