C++ 如何在 Visual Studio 2005 中使用 Manifest 文件以管理员权限运行应用程序?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15967949/
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 19:56:17  来源:igfitidea点击:

How to run application with Admin privileges using Manifest file in Visual Studio 2005?

c++windows-8windows-vistamanifestwindows-server

提问by Lipika

I need to create an application which needs to create files/folders in "C:\Program Files","Users[username]" and Sys32. Also the application needs to make some registry entry.

我需要创建一个应用程序,它需要在“C:\Program Files”、“Users[username]”和 Sys32 中创建文件/文件夹。应用程序还需要创建一些注册表项。

This application needs to work on Vista and higher. Also, on Windows Server 2003 and higher.

此应用程序需要在 Vista 及更高版本上运行。此外,在 Windows Server 2003 及更高版本上。

The above Operating Systems have the concept of User Account Control (UAC), where to access Program Files and writing in registry requires admin privileges.

上述操作系统具有用户帐户控制 (UAC) 的概念,在何处访问程序文件和写入注册表需要管理员权限。

I looked into many forums and found that using Microsoft SDK we can check whether the current user have admin privileges or not . But the function "CheckTokenMembership" fails for Vista and higher version of OS.

我查看了许多论坛,发现使用 Microsoft SDK 我们可以检查当前用户是否具有管理员权限。但是对于 Vista 和更高版本的操作系统,“CheckTokenMembership”功能失败。

I also found a solution where manifest file can be used to tell OS in advance that the current application requires admin privileges. This is done using "requestedExecutionLevel" tag.

我还找到了一个解决方案,可以使用清单文件提前告诉操作系统当前应用程序需要管理员权限。这是使用“requestedExecutionLevel”标签完成的。

I am using Visual Studio 2005 to create the application. When we create an application in Visual Studio a default manifest file is created. Can I change this manifest file to include "requestedExecutionLevel" tag, so that my application always runs with admin privileges?

我正在使用 Visual Studio 2005 创建应用程序。当我们在 Visual Studio 中创建应用程序时,会创建一个默认清单文件。我可以更改此清单文件以包含“requestedExecutionLevel”标签,以便我的应用程序始终以管理员权限运行吗?

Is there any other method through which my application runs with admin privileges without asking user (admin or standard) to run my application as "run as admin"??

有没有其他方法可以让我的应用程序以管理员权限运行而不要求用户(管理员或标准)以“以管理员身份运行”的身份运行我的应用程序?

Thanks!

谢谢!

回答by snowcrash09

You should find an option for this in project properties Linker -> Manifest File -> UAC Execution Level. Set this to requireAdminstrator.

您应该在项目属性 Linker -> Manifest File -> UAC Execution Level 中找到一个选项。将此设置为requireAdminstrator

This will cause the default generated manifest to include the requestedExecutionlevelthat you need, so that your users will be prompted automatically to elevate their privileges if they are not already elevated.

这将导致默认生成的清单包含requestedExecutionlevel您需要的清单,以便您的用户如果尚未提升权限,则会自动提示提升他们的权限。

screenshot of visual studio UAC options

Visual Studio UAC 选项的屏幕截图