Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
指定された年の和暦元号のアルファベット頭文字をレジストリから取得する
BOOL GetEraAlphabet(const LPSYSTEMTIME lpSystemTime, LPWSTR lpszEraAlphabet, DWORD dwEraAlphabetSize)
{
BOOL bRet = FALSE;
WCHAR szEra[256] = { 0 };
if (GetDateFormatEx(L"ja-JP", DATE_USE_ALT_CALENDAR, lpSystemTime, L"gg", szEra, _countof(szEra), NULL) > 0)
{
HKEY hKey;
if (RegOpenKeyExW(HKEY_LOCAL_MACHINE, L"SYSTEM\\CurrentControlSet\\Control\\Nls\\Calendars\\Japanese\\Eras", 0, KEY_READ, &hKey) == ERROR_SUCCESS)
{
DWORD dwValues;
if (RegQueryInfoKeyW(hKey, 0, 0, 0, 0, 0, 0, &dwValues, 0, 0, 0, 0) == ERROR_SUCCESS)
{
for (DWORD i = 0; i < dwValues; ++i)
{
WCHAR szValueName[1024] = { 0 };
DWORD dwValueNameSize = _countof(szValueName);
WCHAR szData[1024];
DWORD dwDataByteSize = sizeof(szData);
if (RegEnumValueW(hKey, i, szValueName, &dwValueNameSize, 0, 0, (LPBYTE)szData, &dwDataByteSize) == ERROR_SUCCESS)
{
if (wcsncmp(szData, szEra, lstrlenW(szEra)) == 0)
{
LPWSTR context = 0;
LPWSTR p = wcstok_s(szData, L"_", &context);
for (int i = 0; i < 3 && p; ++i)
{
p = wcstok_s(0, L"_", &context);
}
if (p)
{
lstrcpynW(lpszEraAlphabet, p, dwEraAlphabetSize);
bRet = TRUE;
}
break;
}
}
}
}
RegCloseKey(hKey);
}
}
return bRet;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment