如何在 Windows 7 上调用具有提升权限的 Win32 API
我们刚刚发现,调用 Win32 SetDateTime
函数的代码需要在 Windows 7 中以提升模式运行,即即使以管理员身份登录,仍然必须选择运行 Set Date 代码:管理员致电更改日期。
我可以在代码中做些什么来始终在提升的权限下调用此函数吗?
We have just discovered that code that calls the Win32 SetDateTime
function needs to run in elevated mode in Windows 7, i.e. even when logged in as an administrator, one still has to choose to run the Set Date code as Administrator for the call to change the date.
Is there anything I can do in code to always call this function under elevated privileges?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用 ShellExecute API 调用来启动具有提升权限的可执行文件。
但是,如果计算机上启用了 UAC(用户访问控制),用户仍会收到 UAC 提示,要求提升运行权限。
因此,我认为您无法避免提升权限的提示,但至少您的用户不必以管理员身份手动运行该程序。
如果您需要有关如何以管理员身份启动可执行文件的帮助,请告诉我。
You can use the ShellExecute API call to launch an executable with elevated privileges.
However, if UAC (user access control) is enabled on the machine, the user will still get the UAC prompt asking for permissions to run elevated.
So I don't think you can avoid the prompt for elevation permission, but at least your user doesn't have to manually run the program as an admin.
Please let me know if you need help in how to launch an executable as an admin.
这不是安全的工作原理。改变时钟是一个非常的侵入性操作,它有很多副作用。 Windows 或任何其他操作系统中没有任何机制可以让您以有限的权限启动进程,然后任意绕过这些限制并突然获得管理员权限。如果可能的话,以有限的权限运行程序是没有任何意义的。
如果您想做这样的事情,那么您必须以更高的权限运行您的程序。在 Vista 和 Win7 上,需要您作为服务或计划任务运行。这需要管理员才能安装。 UAC 提供了一种获得常规程序管理员权限的方法,您必须在程序中包含清单,以便用户收到有关您的权限提升的通知。谷歌“需要管理员”,首先受到打击。
This is just not how security works. Changing the clock is a very intrusive operation, it has a very large number of side effects. There is no mechanism in Windows, or any other operating system for that matter, where you could start a process with limited privileges and then just arbitrarily bypass these limitations and suddenly gain administrator rights. There wouldn't be any point whatsoever to running programs with limited privileges if that was possible.
If you want to do something like this then you'll have to run your program with elevated rights. On Vista and Win7 that requires you to run as a service or a scheduled task. Which require an administrator to get installed. UAC provides a way gain admin rights for regular programs, you have to include a manifest in your program so the user is notified about your privilege elevation. Google 'requireadministrator', take the first hit.
正如其他人所说,您需要生成一个新进程来获取提升的权限,这将导致出现 UAC 对话框。
如果这是您需要在无人值守的情况下运行的东西,您可以尝试作为服务运行。这将为您提供所需的高级背景。
Like others have said, you need to spawn a new process to get elevated permissions, which will result in a UAC dialog.
If this is something you need to run unattended you could try running as a service. That would give you the elevated context you need.