如何使用windows api更改时区设置 [英] How to change time zone settings using windows api
问题描述
我需要在我的应用程序中通过API更改DST和时区。
我修改&复制SetTimeZoneInformation使用的例子,并运行它:
http://msdn.microsoft.com/en-us/library/windows/desktop/ms724944(v = vs.85).aspx
我假设我的时间设置应反映在托盘或控制面板中的时间设置。
此代码修改了HKLM \SYSTEM \\ \\CurrentControlSet\Control\TimeZoneInformation键内容。
HKLM \SOFTWARE\Microsoft\Windows NT\CurrentVersion\Time Zones\my time zone线程没有任何更改。
有什么方法可以使用C ++中的API修改我的时区吗?
添加了(从应该在这里编辑的答案):
实际上,我的目标是让我的应用程序在Win7下工作。现有功能使用手动DST支持。因此,我需要以某种方式同步此手动DST和GMT偏移设置与Win7的。正如我所知,SetTimeZoneInformation函数只是尝试类似于根据我的输入从现有的时区设置时区。如果没有时区等于我的输入,Win7使用默认值,例如UTC-12,并通知用户当前时区不被识别。
,我应该用什么方法来实现?
在链接页面中查看以下内容:
应用程序必须具有
SE_TIME_ZONE_NAME
权限为这个
函数成功。默认情况下禁用此权限。使用
AdjustTokenPrivileges
函数在调用
SetTimeZoneInformation
之前启用该特权,然后禁用在
SetTimeZoneInformation
调用后的特权。有关详细信息,请参阅使用
特殊权限运行
加上,发送 WM_SETTINGCHANGE
作为广播消息通知托盘(SNA)关于更改。
我知道示例代码尝试启用权限,但它没有错误检查,然后调用 SetTimeZoneInformation
反正...
I need to change DST and time zone via API in my application. I modified & copied example of "SetTimeZoneInformation" usage by the end of the following link and have run it: http://msdn.microsoft.com/en-us/library/windows/desktop/ms724944(v=vs.85).aspx
I am assuming that my time settings should reflect on my time settings in tray or control panel. But nothing happens.
Following thing also should be noticed.
This code modifies "HKLM\SYSTEM\CurrentControlSet\Control\TimeZoneInformation" key content. And there are no changes in "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Time Zones\my time zone" thread. But this is what I am looking for!
Is there any way to modify "my time zone" using API in C++?
Added (from the answer that should have been edited in here):
So, actually, my goal is to make my application to work under Win7. Existing functionality uses MANUAL DST support. Thus, I need somehow synchronize this manual DST and GMT offset settings with Win7 ones. As I understood, SetTimeZoneInformation function just tries something like to find time zone from the existing set of time zones according to my input. If there is no time zone equal my input, Win7 uses default one, UTC-12 for instance, and notifies user that "current time zone is not recognized".
So, what approach should I use to realize that?
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 theAdjustTokenPrivileges
function to enable the privilege before callingSetTimeZoneInformation
, and then to disable the privilege after theSetTimeZoneInformation
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 ...
这篇关于如何使用windows api更改时区设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!