2025-11-28 00:35:46 +09:00

175 lines
5.2 KiB
C++

// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
// ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
// THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
// PARTICULAR PURPOSE.
//
// Copyright (c) Microsoft Corporation. All rights reserved.
#include "windows.h"
#include "stdio.h"
// All of the LCTYPES new to Windows Vista
LCTYPE NewTypes[] =
{
LOCALE_SNAME,
LOCALE_SDURATION,
LOCALE_SKEYBOARDSTOINSTALL,
LOCALE_SSHORTESTDAYNAME1,
LOCALE_SSHORTESTDAYNAME2,
LOCALE_SSHORTESTDAYNAME3,
LOCALE_SSHORTESTDAYNAME4,
LOCALE_SSHORTESTDAYNAME5,
LOCALE_SSHORTESTDAYNAME6,
LOCALE_SSHORTESTDAYNAME7,
LOCALE_SISO639LANGNAME2,
LOCALE_SISO3166CTRYNAME2,
LOCALE_SNAN,
LOCALE_SPOSINFINITY,
LOCALE_SNEGINFINITY,
LOCALE_SSCRIPTS
};
// Strings so we can print out the LCTYPES
LPCWSTR NewTypeNames[] =
{
L"LOCALE_SNAME",
L"LOCALE_SDURATION",
L"LOCALE_SKEYBOARDSTOINSTALL",
L"LOCALE_SSHORTESTDAYNAME1",
L"LOCALE_SSHORTESTDAYNAME2",
L"LOCALE_SSHORTESTDAYNAME3",
L"LOCALE_SSHORTESTDAYNAME4",
L"LOCALE_SSHORTESTDAYNAME5",
L"LOCALE_SSHORTESTDAYNAME6",
L"LOCALE_SSHORTESTDAYNAME7",
L"LOCALE_SISO639LANGNAME2",
L"LOCALE_SISO3166CTRYNAME2",
L"LOCALE_SNAN",
L"LOCALE_SPOSINFINITY",
L"LOCALE_SNEGINFINITY",
L"LOCALE_SSCRIPTS"
};
// Callback for EnumSystemLocalesEx()
#define BUFFER_SIZE 255
BOOL CALLBACK MyFuncLocaleEx(LPWSTR pStr, DWORD dwFlags, LPARAM lparam)
{
WCHAR** argv = (WCHAR**)lparam;
WCHAR wcBuffer[BUFFER_SIZE];
int iResult;
int i;
// If specific locales were specified on the command line check for a match
if (*argv && argv[1])
{
// Check each argument to see if our locale matches
for (i = 1; argv[i] != 0; i++)
{
// Using invariant, check if the name matches the input. CompareStringEx
// is probably overkill, but we want to demonstrate that named API.
if (CompareStringEx(LOCALE_NAME_INVARIANT,
LINGUISTIC_IGNORECASE,
argv[i],
-1,
pStr,
-1,
NULL,
NULL,
0) == CSTR_EQUAL)
{
break;
}
}
// If no match then stop and don't output this one
if (argv[i] == 0)
return TRUE;
}
// Print out the locale name we found
iResult = GetLocaleInfoEx(pStr, LOCALE_SENGLANGUAGE, wcBuffer, BUFFER_SIZE);
// If it succeeds, print it out
if (iResult > 0)
wprintf(L"Locale %s (%s)\n", pStr, wcBuffer);
else
wprintf(L"Locale %s had error %d\n", pStr, GetLastError());
// If this is the system locale, let us know. CompareStringEx
// is probably overkill, but we want to demonstrate that named API.
iResult = GetSystemDefaultLocaleName(wcBuffer, BUFFER_SIZE);
if (iResult > 0)
{
if (CompareStringEx(LOCALE_NAME_INVARIANT,
LINGUISTIC_IGNORECASE,
wcBuffer,
-1,
pStr,
-1,
NULL,
NULL,
0) == CSTR_EQUAL)
{
wprintf(L"Locale %s is the system locale!\n", wcBuffer);
}
}
else
{
wprintf(L"Error %d getting system locale\n", GetLastError());
}
// Get its LCID
LCID lcid = LocaleNameToLCID(pStr, NULL);
if (lcid != 0)
wprintf(L"LCID for %s is %x\n", pStr, lcid);
else
wprintf(L"Error %d getting LCID\n", GetLastError());
// Get today's date
iResult = GetDateFormatEx(pStr, DATE_LONGDATE, NULL, NULL, wcBuffer, BUFFER_SIZE, NULL);
if (iResult > 0)
wprintf(L"Date: %s\n", wcBuffer);
else
wprintf(L"Error %d getting today's date for %s\n", GetLastError(), pStr);
// Loop through all of the new LCTYPES and do GetLocaleInfoEx on them
for (i = 0; i < sizeof(NewTypes) / sizeof(NewTypes[0]); i++)
{
// Get this LCTYPE result for this locale
iResult = GetLocaleInfoEx(pStr, NewTypes[i], wcBuffer, BUFFER_SIZE);
// If it succeeds, print it out
if (iResult > 0)
{
wprintf(L" %s has value %s\n", NewTypeNames[i], wcBuffer);
}
else
{
wprintf(L" %s had error %d\n", NewTypeNames[i], GetLastError());
}
}
return (TRUE);
}
int __cdecl wmain(int argc, wchar_t* argv[])
{
// Enumerate all the locales and report on them
EnumSystemLocalesEx( MyFuncLocaleEx, LOCALE_ALL, (LPARAM)argv, NULL);
// See which of the input locales are valid (if any)
if (*argv && argv[1])
{
// Check each argument to see if our locale matches
for (int i = 1; argv[i] != 0; i++)
{
// See if this is a valid locale name
if (!IsValidLocaleName(argv[i]))
{
wprintf(L"%s is not a valid locale name\n", argv[i]);
}
}
}
}