启动时通过UAC授予c ++程序管理权限 [英] Granting c++ program administrative rights through UAC at startup

查看:409
本文介绍了启动时通过UAC授予c ++程序管理权限的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我已经进行了一些研究,但我想了解如何使他的程序在执行UAC之前向UAC寻求管理特权。这是否需要在ShellExecute()中使用 runas?我意识到这是我在编程过程中需要理解的东西,因为几乎我安装的每个程序都要求我这样做。因此,例如,如果我在启动时授予程序特权,则可以通过system()函数正确执行任何批处理文件或cmd语句?

So I've done some research, but I'm interested in understanding how one would make his program ask for administrative privileges from the UAC before its execution. Would this require one to use "runas" inside the ShellExecute()? I realize that this is something I will need to understand into my adventure of programming, as just about every program I have installed has asked me this. So for example, if I gave a program privileges at start up it would be able to execute any batch file or cmd statement through the system() function correct?

#include "stdafx.h"
#include <iostream>

using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
    system("notepad");
    return 0;
}


推荐答案

您可以在以下位置设置requireAdmin可执行文件清单文件,然后这将要求您将应用程序提升为管理员,用户可能必须提供其他凭据(例如运行方式)。
它将对您的应用程序图标在资源管理器中起到一些保护作用。

You can set the requireAdmin in the executables manifest file, and then this will request your application to be raised to admin, the user may have to supply additional credentials (like run as). It will put a little shield against your application icon in explorer.

在Visual Studio中

In Visual Studio


  • 选择项目的属性,

  • 然后打开配置属性

  • 然后打开链接器

  • 选择清单文件

  • 并修改 UAC执行级别以要求管理员

  • select properties on your project,
  • then open up "Configuration Properties"
  • then open up Linker
  • Select "Manifest File"
  • and modify "UAC Execution Level" to requireAdministrator

您可以通过其他方式执行此操作,但这是最简单的方法之一。
因此,在您的示例中,如果编写的程序的UAC执行级别设置为 requireAdministrator,则在这种情况下,您调用的程序也将提升为记事本。 (我认为可能有某种方法可以确保您没有被提升,但是我从未这样做过)

you can do this other ways but that's one of the easiest. So, in your example if the program you wrote had UAC execution level set to "requireAdministrator" then the program you call would then also be elevated in this case notepad. (I think that there may be some way to make sure that you are not elevated, but I have never done that)

这篇关于启动时通过UAC授予c ++程序管理权限的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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