在 Windows 7 中以编程方式设置时间

发布于 2024-09-28 09:13:51 字数 374 浏览 2 评论 0原文

我正在将一个应用程序从 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

著墨染雨君画夕 2024-10-05 09:13:51

不知道为什么它不适合你。以下代码将时间设置为今天的 UTC 下午 4:12。 (为我工作)

public class Program 
{
    public struct SystemTime
    {
        public ushort Year;
        public ushort Month;
        public ushort DayOfWeek;
        public ushort Day;
        public ushort Hour;
        public ushort Minute;
        public ushort Second;
        public ushort Millisecond;
    };

    [DllImport("kernel32.dll", EntryPoint = "SetSystemTime", SetLastError = true)]
    public extern static bool Win32SetSystemTime(ref SystemTime st);

    public static void Main(string[] args)
    {
        SystemTime st = new SystemTime
        {
            Year = 2010, Month = 10, Day = 18, Hour = 16, Minute = 12, DayOfWeek = 1
        };
    }
}

根据文档

调用进程必须具有SE_SYSTEMTIME_NAME权限。默认情况下禁用此权限。 SetSystemTime 函数在更改系统时间之前启用 SE_SYSTEMTIME_NAME 权限,并在返回之前禁用该权限。有关详细信息,请参阅以特殊权限运行。

所以看来这不应该是一个问题。

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)

public class Program 
{
    public struct SystemTime
    {
        public ushort Year;
        public ushort Month;
        public ushort DayOfWeek;
        public ushort Day;
        public ushort Hour;
        public ushort Minute;
        public ushort Second;
        public ushort Millisecond;
    };

    [DllImport("kernel32.dll", EntryPoint = "SetSystemTime", SetLastError = true)]
    public extern static bool Win32SetSystemTime(ref SystemTime st);

    public static void Main(string[] args)
    {
        SystemTime st = new SystemTime
        {
            Year = 2010, Month = 10, Day = 18, Hour = 16, Minute = 12, DayOfWeek = 1
        };
    }
}

According to the docs:

The calling process must have the SE_SYSTEMTIME_NAME privilege. This privilege is disabled by default. The SetSystemTime function enables the SE_SYSTEMTIME_NAME privilege before changing the system time and disables the privilege before returning. For more information, see Running with Special Privileges.

So seems like that shouldn't be an issue.

你好,陌生人 2024-10-05 09:13:51

好吧,如果最坏的情况发生,总会有

System.Diagnostics.Process.Start("CMD", "/C TIME 19:58");  // set time to 7:58PM

Well, if worst comes to worst, there is always

System.Diagnostics.Process.Start("CMD", "/C TIME 19:58");  // set time to 7:58PM
万劫不复 2024-10-05 09:13:51

您的应用程序需要提升才能更改时间(因为更改时间可能会导致活动日志等不真实),但不能更改时区。使用 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文