当用户已经是管理员时通过C ++激活管理员[以管理员身份运行] [英] Activating Administrator via C++ when users are already administrator [Run as administrator]

查看:249
本文介绍了当用户已经是管理员时通过C ++激活管理员[以管理员身份运行]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我注意到,在Windows 7,有时,即使当你是一个管理员,你不能做很多事情,可能是某种错误,我的应用程序,我检查一个用户是否是管理员,然后启动程序,因为我的程序在受保护的默认文件夹中创建文件,如根文件夹(C:),如果您不是Windows 7上的管理员,您只能在那里创建文件夹。

Well I noticed that on Windows 7, sometimes even when you are an administrator, you can't do a lot of things, probably it's some sort of bug, my application I check if an user is administrator before start the program because my program creates file in folders that are protected default like the root folder ( C: ), and if you aren't an administrator on Windows 7, you can only create folders there.

所以,如果我右键点击我的应用程序,然后去以管理员身份运行,它只是工作正常。
有没有办法让我的应用程序作为管理员自动运行?我想要能够像下面这样一行代码: ActivateAdministrator(); 完全可用于代码,因为我更改属性,创建文件与 ifstream

So if I right click in my application and go "Run as Administrator", it just works fine. Is there a way to make my application run as administrator automatically? I would like to be able to make a line of code like: ActivateAdministrator(); and be available for the code completely, because I change attributes, create files with ifstream.

推荐答案

您可以向您的可执行文件添加清单 - http://msdn.microsoft.com/en-us/library/bb756929.aspx

You could add a manifest to your executable - http://msdn.microsoft.com/en-us/library/bb756929.aspx

如果用户在UAC已打开并且不是管理员的系统上运行,则会显示包含 requestedExecutionLevel
的清单level =requireAdministrator
将生成管理员密码的提示,然后您的应用程序可以使用管理权限运行。 (需要管理员权限意味着不正确的密码或没有密码将阻止其完全运行)

If the user is running on a system with the UAC switched on, and are not an administrator, a manifest which contains requestedExecutionLevel level="requireAdministrator" will produce a prompt for the Administrator password before your application can run with administrative privileges. (requiring administrator privileges means that an incorrect password or no password will stop it from running altogether)

如果他们是UAC打开的管理员,那么相同的清单将导致是/否提示询问您的应用程序是否应该被授予管理权限。

If they are an administrator with the UAC switched on, then that same manifest will cause a Yes/No prompt to ask whether your application should be granted administrative privileges.

当然,真正的问题是,无论您的应用程序正在做什么,被检查。

Of course, the real issue is that whatever your application is doing which requires administrative privileges needs to be examined.

大多数情况下,正常用户级应用程序不需要此权限。这是一个应用程序设计问题真的 - 你的应用程序做什么需要管理员权限?是真的是必要的吗?例如如果您要修改文件,那么为什么这些文件在文件系统上的受保护区域中,而不是在用户的个人资料空间中?

Most of the time the privilege is simply not required for normal user-level applications. This is an application design issue really - what is your application doing which requires admin privileges? is it really necessary? e.g. If you're modifying files, then why are those files in a protected area on the file system instead of in the user's profile space?

这篇关于当用户已经是管理员时通过C ++激活管理员[以管理员身份运行]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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