简体   繁体   English

如何使我的应用程序自动以管理员权限运行?

[英]How do I get my application to run with administrator rights automatically?

I've made a console application that is supposed to update some registry entries so that I can access 32 bit COM components from a 64 bit application. 我制作了一个控制台应用程序,该应用程序应该更新一些注册表项,以便可以从64位应用程序访问32位COM组件。 If I have admin rights, it works great, but I can't seem to get the application run with admin rights out of the box. 如果我拥有管理员权限,那么它会很好用,但是我似乎无法开箱即用管理员权限运行该应用程序。

This is what I've done. 这就是我所做的。

  1. Create a Windows Console Application. 创建一个Windows控制台应用程序。
  2. Add my code. 添加我的代码。
  3. Right click on my project and select Properties. 右键单击我的项目,然后选择属性。
  4. Navigate to Configuration Properties > Linker > Manifest File > UAC Execution Level and set to requireAdministrator (/level='requireAdministrator') . 导航到“ Configuration Properties > Linker > Manifest File > UAC Execution Level并将其设置为requireAdministrator (/level='requireAdministrator')

在此处输入图片说明

It took a lot to figure out this because all of the info on the web is for Visual Studio 2010 or earlier which required manually creating an XML manifest file and conflicts with the auto generated one that this creates. 需要花很多时间才能弄清这一点,因为Web上的所有信息都是针对Visual Studio 2010或更早版本的,这需要手动创建XML清单文件,并且与由此创建的自动生成的文件相冲突。

However, this doesn't seem to be enough to get it to run as an admin. 但是,这似乎不足以让它以管理员身份运行。 It is a real PITA that this information isn't made easily findable. 不能轻易找到此信息是一个真正的PITA。 Is there some other step that I am missing? 我还缺少其他步骤吗? Something like a signing process? 有点像签名过程吗?

Turns out it is how this mini application is run. 事实证明,这个迷你应用程序是如何运行的。

From my main programme, using ShellExecute() or ShellExecuteEx() with the "runas" verb will allow running of this executable with administrator privileges without popping up a UAC dialog. 在我的主程序中,将ShellExecute()ShellExecuteEx()"runas"动词一起使用将允许以管理员权限运行此可执行文件,而不会弹出UAC对话框。

Running this from the command line however, will result in this mini app being executed in the user's security context, which is what I was doing. 但是,从命令行运行此命令将导致此微型应用程序在用户的安全上下文中执行,这就是我正在做的事情。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM