当应用程序需要管理员权限时,Windows 7 会弹出消息吗?
当我编写一个需要执行“可管理”操作的应用程序时会发生什么?
就像将文件写入 C:\Program Files
- 那么,当应用程序尝试执行此操作时,会发生什么?是否会弹出一条消息要求用户输入密码?会抛出异常吗?程序会终止并且计算机会爆炸吗?上述所有的? :)
旁注:Windows 7 用户默认以管理员身份登录吗?
What will happen when I write an application that needs to do something "adminable"?
Like writing files to C:\Program Files
- then, when the application tries to do this, what will happen? Will a message be popped up asking the user for a password? Will an exception be thrown? Will the program terminate and will the computer explode? All of the above? :)
On a side note: Are Windows 7 users logged by default as administrators?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
一般来说,Windows Vista 和 Windows 7 不会自动检测您需要管理员权限,也不会自动提升您的应用程序。 Microsoft 有一些关于正确请求提升的指南。没有清单的旧应用程序也可能会发现自己被虚拟化 - 也就是说,您对程序文件的写入可能会被重定向到用户配置文件目录下的目录。
请记住,您应该将特权操作保持在绝对最低限度。 UAC 的存在是为了阻止应用程序开发人员请求不必要的管理访问权限,以提高安全性。唯一会扰乱程序文件的程序是安装程序。
请注意,Windows 7/Vista 上的管理用户无法在不提升权限的情况下使用其权限 - 管理员令牌具有更高的完整性级别 比默认情况下运行的进程要多,因此无法访问。如果您尝试执行需要此类访问的操作,您将收到访问被拒绝的错误;底层 API 不会抛出任何类型的异常,它们通常只会失败并将
GetLastError()
设置为ERROR_ACCESS_DENIED
。当然,更高级别的 API 可能会选择将其转换为抛出的异常,或者暴力终止,尽管后者相当粗鲁,并且不太可能在任何内置 Windows API 中发生。In general, Windows Vista and Windows 7 will not automatically detect that you need admin rights, and will not elevate your application automatically. Microsoft has some guidelines for properly requesting elevation. Older applications without a manifest may find themselves virtualized as well - that is, your write to program files may be redirected to a directory under the user's profile directory.
Keep in mind that you should keep privileged operations to an absolute minimum. UAC exists to discourage application developers from requesting unnecessary administrative access, in order to improve security. The only programs that should be messing with program files are installers.
Note that Administrative users on Windows 7/Vista cannot use their rights without elevating - the Administrator token has a higher integrity level than processes run at by default, and as such cannot be accessed. If you attempt to perform an operation which requires such access, you will receive an access denied error; the underlying APIs won't throw any kind of exception, they'll just typically fail and set
GetLastError()
toERROR_ACCESS_DENIED
. Higher level APIs, of course, may choose to convert this to a thrown exception, or to terminate violently, although the latter is quite rude, and unlikely to occur in any built-in windows APIs.