How to change time zone settings using windows api

限于喜欢 提交于 2019-12-05 18:58:49

Assuming your code is running with the required privileges and SetTimeZoneInformation() did not return FALSE for another reason, then it did change the system's time zone.

However, the clock applet in the notification area does not know about that change, because you forgot to advertise it in the first place. Quoth the Remarks section of the documentation:

To inform Explorer that the time zone has changed, send the WM_SETTINGCHANGE message.

So, you should broadcast a WM_SETTINGCHANGE message to all the top-levels windows when your application changes the system's time zone. Something like:

SendMessageTimeout(HWND_BROADCAST, WM_SETTINGCHANGE, NULL, NULL,
    SMTO_NORMAL, aShortTimeoutInMilliseconds, NULL);

Did you check the return codes for error? See the following from the linked page:

An application must have the SE_TIME_ZONE_NAME privilege for this function to succeed. This privilege is disabled by default. Use the AdjustTokenPrivileges function to enable the privilege before calling SetTimeZoneInformation, and then to disable the privilege after the SetTimeZoneInformation call. For more information, see Running with Special Privileges.

plus, send WM_SETTINGCHANGE as a broadcast message to notify the "tray" (SNA) about the change.

I know the sample code tries to enable the privilege, but it does no error checking and goes forth to call SetTimeZoneInformation anyway ...

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!