Skip to content

Instantly share code, notes, and snippets.

@kenjinote
Created February 10, 2019 05:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kenjinote/07fd7cbe39fda83d1a9d7180ca0981b6 to your computer and use it in GitHub Desktop.
Save kenjinote/07fd7cbe39fda83d1a9d7180ca0981b6 to your computer and use it in GitHub Desktop.
指定された年の和暦元号のアルファベット頭文字をレジストリから取得する
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