在 Windows 7 中以编程方式设置时间
我正在将一个应用程序从 Windows 2000(不要问)移植到 Windows 7,并且我需要复制允许用户从 GUI 设置时间的功能。以前,这是通过使用命令提示符直接调用“时间”来完成的,但 Windows 7 中的用户权限似乎有所改变。
经过一些研究,您似乎可以使用调用 < code>kernel32.dll方法Win32SetSystemTime
,但出现了相同的权限问题。阅读 MSDN 我认为我需要启用 SE_SYSTEMTIME_NAME
,但是无论我尝试什么,我似乎都无法使其工作。
有没有人有一些经过测试的 Windows 7 示例代码,以允许 API 调用 Win32SetSystemTime
?
I'm porting an application from Windows 2000 (don't ask) to Windows 7 and I need to replicate functionality that allows the user to set the time from a GUI. Previously this had been done with a call directly to 'time' using the command prompt, but it appears the user permissions have changed somewhat in Windows 7.
Having done some research, it appears that you can set the time using a call to the kernel32.dll
method Win32SetSystemTime
, but the same permissions issue arises. Reading MSDN I think I need to enable SE_SYSTEMTIME_NAME
, however no matter what I try I can't seem to make this work.
Does anyone have some tested example code for Windows 7 to allow an API call to Win32SetSystemTime
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
不知道为什么它不适合你。以下代码将时间设置为今天的 UTC 下午 4:12。 (为我工作)
根据文档:
所以看来这不应该是一个问题。
Not sure why it's not working for you. The following code sets the time to today's date at 4:12 PM UTC. (Worked for me)
According to the docs:
So seems like that shouldn't be an issue.
好吧,如果最坏的情况发生,总会有
Well, if worst comes to worst, there is always
您的应用程序需要提升才能更改时间(因为更改时间可能会导致活动日志等不真实),但不能更改时区。使用 requireAdministrator 将清单放在您的应用程序上,应用程序将会提升。 (要在制作清单之前对此进行测试,请右键单击您的 exe 并以管理员身份运行。这将提升该应用程序一次。提升与由恰好属于管理员组的人启动是不同的。它是关于选择使用你的权力。)
用户很可能不喜欢 UAC 提示,因此如果很少发生时间更改,请将其分成一个单独的 exe,在主应用程序上放置一个清单使用 asInvoker 和另一个使用 requireAdministrator 的时间转换器,并使用 ShellExecute 从主应用程序启动时间转换器。理想情况下,有一个按钮或菜单项来实现此操作,并在其上放置一个盾牌图标,以便 UAC 提示不会让用户感到惊讶。我拒绝了我没有预料到的 UAC 提示。
Your app needs to be elevated to change the time (since changing the time could result in activity logs etc being untrue) but not to change the time zone. Put a manifest on your application with requireAdministrator and the app will elevate. (To test this before making the manifest, right-click your exe and Run As Adminstrator. This will elevate the app just the one time. Elevating is a different thing from being launched by someone who happens to be in the Administrators group. It's about choosing to use your powers.)
Chances are the user won't like the UAC prompt, so if the time-changing is rare, split it out into a separate exe, put a manifest on the main app with asInvoker and another on the time-changer with requireAdministrator, and launch the time-changer from the main app with ShellExecute. Ideally have a button or menu item to make this happen and put a shield icon on it so that the UAC prompt doesn't surprise the user. I decline UAC prompts I wasn't expecting.