如何在Qt中以管理员身份运行应用程序 [英] How to run application as administrator in Qt

查看:213
本文介绍了如何在Qt中以管理员身份运行应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何以管理员身份启动 Qt 应用程序或在需要管理员权限时获得提示?我已经尝试了我能找到的所有帖子中的所有内容,并且其中大多数已经有几年的历史了.没有任何效果,每个人似乎对此都有不同的看法.我已经下载了 Windows SDK 来获取 mt.exe,创建了清单文件、RC 文件和所有其他方法,包括右键单击并将应用程序设置为以管理员模式启动.没有任何效果.我需要它,因为我的程序中的任务需要管理员权限.QFile::errorString() 表示访问被拒绝.有谁知道如何使这项工作?

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?

推荐答案

好的,我找到了问题的答案.我之前找到了一些解决方案,但它们不起作用.我找到了他们不工作的原因.原因很愚蠢:这是因为我的文件所在的文件夹在第一个和第二个单词之间有一个空格,例如(我的应用程序)导致无法找到位置的错误.它应该是(myapp)与没空间了.关于未找到位置的错误仅在尝试添加 adim 权限时发生.否则一切正常,没有错误.所以这是解决方案,并确保文件夹名称中没有空格.

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) 创建 rc 文件:打开记事本并在里面粘贴以下文本然后将文件另存为 yourappname.rc 这将创建 rc文件.将文件放在包含所有源代码和专业版的文件夹中文件.

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) 用记事本创建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) 在您的 pro 文件中输入以下文本:

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.

也有类似的帖子描述了这个过程.只要确保有您的文件夹名称中没有空格,否则将无法使用.你也必须用 qt 运行管理员权限才能工作.右键单击 Qt 并运行为尝试编译之前管理员.

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.

这篇关于如何在Qt中以管理员身份运行应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆