简体   繁体   English

从 windows 注册表读取 Timezoneinfo (C++)

[英]Read Timezoneinfo from windows registry (C++)

I am trying to read the TIME_ZONE_INFORMATION struct from windows registry with the following code:我正在尝试使用以下代码从 windows 注册表中读取 TIME_ZONE_INFORMATION 结构:

void GetTimeZoneInfo(){
    TIME_ZONE_INFORMATION tz = {0}; 
    TIME_ZONE_INFORMATION tz_data={0};
    char *keyname="TZI";
    DWORD size = sizeof(tz_data);
    HKEY hk = NULL;
    char *zone_key = "SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Time Zones\\Central Standard Time";
    if ((RegOpenKeyExA(HKEY_LOCAL_MACHINE, zone_key, 0, KEY_READ, &hk) == ERROR_SUCCESS))
    {
        if(RegQueryValueExA(hk, keyname, NULL, NULL, (LPBYTE) &tz_data, &size) == ERROR_SUCCESS)
        
        {    /*Control enters here*/
             /*Read the data*/
             cout<<"Successful in retrieving the value"<<endl;
            tz.Bias = tz_data.Bias;
            tz.DaylightBias = tz_data.DaylightBias;
            tz.DaylightDate = tz_data.DaylightDate;
            tz.StandardBias = tz_data.StandardBias;
            tz.StandardDate = tz_data.StandardDate;
        }
        else{ cout<<"Failure in retrieving the value"<<endl;}
    }
    else { cout<<"RegOpenKey Failure!"<<endl;}
}

It sets all to zero value, but don't see any error on running RegOpenKeyExA and RegQueryValueExA.它将所有值设置为零,但在运行 RegOpenKeyExA 和 RegQueryValueExA 时看不到任何错误。 TIME_ZONE_INFORMATION is stored in registry as type: REG_BINARY TIME_ZONE_INFORMATION 作为类型存储在注册表中:REG_BINARY

Is this the right way to read from registry?这是从注册表读取的正确方法吗?

According to MS docs TZI key contains the following time zone information:根据MS docs TZI 密钥包含以下时区信息:

typedef struct _REG_TZI_FORMAT
{
    LONG Bias;
    LONG StandardBias;
    LONG DaylightBias;
    SYSTEMTIME StandardDate;
    SYSTEMTIME DaylightDate;
} REG_TZI_FORMAT;

You are reading to a wrong struct - TIME_ZONE_INFORMATION.您正在阅读错误的结构 - TIME_ZONE_INFORMATION。

A couple of things:有几件事:

  • In most cases you should be using the DYNAMIC_TIME_ZONE_INFORMATION structure, as it supports the full range of data that is in the registry.在大多数情况下,您应该使用DYNAMIC_TIME_ZONE_INFORMATION结构,因为它支持注册表中的全部数据。 Most of the Win32 time zone functions have alternative versions that work with the dynamic structures.大多数 Win32 时区函数都有可用于动态结构的替代版本。

  • You don't need to load the information from the registry yourself.您不需要自己从注册表中加载信息。 Instead, use EnumDynamicTimeZoneInformation .相反,请使用EnumDynamicTimeZoneInformation Enumerate them until the TimeZoneKeyName matches the one you are looking for.枚举它们,直到TimeZoneKeyName与您要查找的匹配。

  • As nevilad pointed out , the TZI entries in the registry data aligns with _REG_TZI_FORMAT , which is slightly different than TIME_ZONE_INFORMATION structure.正如nevilad 指出的那样,注册表数据中的TZI条目与_REG_TZI_FORMAT对齐,这与TIME_ZONE_INFORMATION结构略有不同。 (But again, you don't need to read from the registry yourself. ) (但同样,您不需要自己从注册表中读取。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM