visual-studio Windows 7下调试需要管理员权限的程序

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

Debug a program that needs administrator rights under Windows 7

visual-studiodebuggingvisual-studio-2008windows-7

提问by brian

I'm running Visual Studio 2008 on Windows 7 64-bit. I'm logged in as administrator, and I'm running it as administrator, but the program I'm working on fails with access denied when I call a restricted API. If run the program from Explorer with "Run as Administrator" it works.

我在 Windows 7 64 位上运行 Visual Studio 2008。我以管理员身份登录,并以管理员身份运行它,但是当我调用受限 API 时,我正在处理的程序失败并拒绝访问。如果使用“以管理员身份运行”从资源管理器运行该程序,则它可以工作。

I was under the belief that Visual Studio 2008 debugs programs with whatever rights Visual Studio 2008 itself is running with. As it stands I can't debug my application due to this, and I'm at a loss as to what's going on.

我相信 Visual Studio 2008 使用 Visual Studio 2008 本身运行的任何权限来调试程序。就目前而言,由于这个原因,我无法调试我的应用程序,而且我对正在发生的事情感到茫然。

采纳答案by brian

I found the answer. It turns out that it is, in fact, a manifest issue: by default if you have a manifest you need to set the appropriate administrator privilege.

我找到了答案。事实证明,这实际上是一个清单问题:默认情况下,如果您有一个清单,则需要设置适当的管理员权限。

The default is asInvoker, but that doesn't work if you need elevated privileges; you instead have to set it to requireAdministratorin the manifest properties.

默认为asInvoker,但如果您需要提升权限,这将不起作用;您必须requireAdministrator在清单属性中将其设置为。

回答by bluish

This works for Visual Studio 2012.

这适用于 Visual Studio 2012。

  • Create a manifest file: right click on the project and select "Add New Item", select "Application Manifest File". This will add a file named app.manifest to the project.
  • Edit manifest file: set attribute levelfor tag requestedExecutionLevelto requireAdministrator.
  • 创建清单文件:右键单击项目并选择“添加新项目”,选择“应用程序清单文件”。这会将名为 app.manifest 的文件添加到项目中。
  • 编辑清单文件:设置属性level为标记requestedExecutionLevelrequireAdministrator

Now your program will always require admin privileges, wherever it is executed. If launched by Visual Studio debugger, it will prompt you for restarting Visual Studio as administrator, if needed.

现在您的程序将始终需要管理员权限,无论它在哪里执行。如果由 Visual Studio 调试器启动,它会提示您在需要时以管理员身份重新启动 Visual Studio。

回答by Aoi Karasu

It's best to run Visual Studio 2008 as administrator. Moreover, it's good to change your application's manifest settings to require administrator privileges. You will be prompted by the system to elevate to system administrator each time you start your application, and when running under Visual Studio 2008 the IDE will offer you the elevation before start of debugging.

最好以管理员身份运行 Visual Studio 2008。此外,最好将应用程序的清单设置更改为需要管理员权限。每次启动应用程序时,系统都会提示您提升为系统管理员,并且在 Visual Studio 2008 下运行时,IDE 将在开始调试之前为您提供提升。

To change the setting, open project properties and go to Configuration PropertiesLinkerManifest fileUAC Execution Level.

要更改设置,请打开项目属性并转到Configuration PropertiesLinkerManifest fileUAC Execution Level

回答by ZippyV

Run Visual Studio 2008 as administrator.

以管理员身份运行 Visual Studio 2008。

回答by granadaCoder

I had a case where I put in the

我有一个案例,我把

    <requestedExecutionLevel level="requireAdministrator" uiAccess="false" />

settings (described by another answer at this post)........and did not work.

设置(在这篇文章的另一个答案中描述)........并且没有用。

I found another tip (seen below)...and unchecking that option allowed my code to work.

我发现了另一个提示(见下文)......取消选中该选项允许我的代码工作。

Try going into the project properties, and under the "debug" tab, uncheck "enable the Visual Studio hosting process" and see if that fixes your problem.

尝试进入项目属性,然后在“调试”选项卡下,取消选中“启用 Visual Studio 托管进程”,看看是否能解决您的问题。

(from https://social.msdn.microsoft.com/Forums/vstudio/en-US/b5c4da93-5d64-442e-af28-df6d10765538/debug-as-administrator?forum=vbgeneral)

(来自https://social.msdn.microsoft.com/Forums/vstudio/en-US/b5c4da93-5d64-442e-af28-df6d10765538/debug-as-administrator?forum=vbgeneral

So I needed the app.manifest settings AND the "uncheck".

所以我需要 app.manifest 设置和“取消选中”。

回答by Kate Gregory

My testing with Windows Vista and Visual Studio 2005 showed that when you use a host process, the behavior under the debugger depends only on how you launched Visual Studio and not on the manifest (or lack of manifest) of your application. When Visual Studio is launched elevated, the behavior depends only on the use (or not) of a host process, not on the manifest. This is probably still true with Windows 7 and Visual Studio 2008.

我对 Windows Vista 和 Visual Studio 2005 的测试表明,当您使用主机进程时,调试器下的行为仅取决于您启动 Visual Studio 的方式,而不取决于您的应用程序的清单(或缺少清单)。提升启动 Visual Studio 时,行为仅取决于主机进程的使用(或不使用),而不取决于清单。这可能仍然适用于 Windows 7 和 Visual Studio 2008。

Also, have you tried launching the application outside Visual Studio and using Attach to Process?

另外,您是否尝试过在 Visual Studio 之外启动应用程序并使用Attach to Process