C++:以管理员身份运行程序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19617955/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 23:00:30  来源:igfitidea点击:

C++: Run program as administrator

c++adminprivileges

提问by Forivin

Some programs are automatically asking for administrator rights when you run them. These programs are marked with a little shield in the bottom right corner:
enter image description hereNow I'm wondering how I could accomplish this in C++.
Is there a function for this or do I need some special compiler/linker commands for that?

某些程序在您运行时会自动要求管理员权限。这些程序在右下角标有一个小盾牌:
在此处输入图片说明现在我想知道如何在 C++ 中实现这一点。
有没有这个功能,或者我需要一些特殊的编译器/链接器命令吗?

回答by Rahul Tripathi

You must be probably looking for CreateProcessAsUseror CreateProcessWithLogonWfunction.

您可能正在寻找CreateProcessAsUserCreateProcessWithLogonW函数。

There is one more option like this:-

还有一个这样的选择:-

  • Go to the project's Property Pages dialog box.
  • Now open the Configuration Properties node.
  • Next you have to open the Linker node.
  • then select the Manifest File property.
  • Finally modify the Enable User Account Control (UAC), UAC Execution Level, and UAC Bypass UI Protection properties.
  • 转到项目的“属性页”对话框。
  • 现在打开配置属性节点。
  • 接下来,您必须打开链接器节点。
  • 然后选择清单文件属性。
  • 最后修改 Enable User Account Control (UAC)、UAC Execution Level 和 UAC Bypass UI Protection 属性。

Soemthing like this:

像这样的东西:

enter image description here

在此处输入图片说明

And as Syamcorrectly mentioned in the comments the solution could be to embed a manifest in the executable.

正如Syam在评论中正确提到的,解决方案可能是在可执行文件中嵌入清单。

回答by Mustakimur Rahman

Actually, this shield which means the application will need to run from administrator is set from the operating system if the application going to use any feature of that machine which needs permission from administrator.
You do not need to set anything from your app for that.
Hope the answer may help you.

实际上,如果应用程序要使用需要管理员许可的机器的任何功能,则此屏蔽意味着应用程序需要从管理员运行是从操作系统设置的。
您不需要为此从您的应用程序中设置任何内容。
希望答案可以帮到你。