RE: Convert LCID to _locale_t
I have an LCID and am looking to call _vscwprintf_l with that particular
locale. Is there any way of converting an LCID to a _locale_t?
I haven't been able to find a function which does what I want, so I think I
successfully built my own. Though I've tested with various LCIDs, please
reply if you see any problems...
HRESULT LcidToCrtLocale(
int nCategory,
LCID lcidLocale,
_locale_t * ptLocale)
{
DWORD dwCchSize;
DWORD dwCchLanguageSize;
DWORD dwCchCountrySize;
LPSTR pszLanguageCountryName = NULL;
// Validate parameters
HRESULT hr = (ptLocale == NULL) ? E_POINTER : S_OK;
// Get the language buffer size
if (SUCCEEDED(hr))
{
dwCchLanguageSize = GetLocaleInfoA(
lcidLocale,
LOCALE_SENGLANGUAGE,
NULL,
0);
if (dwCchLanguageSize == 0)
{
hr = HRESULT_FROM_WIN32(GetLastError());
}
}
// Get the country buffer size
if (SUCCEEDED(hr))
{
dwCchCountrySize = GetLocaleInfoA(
lcidLocale,
LOCALE_SENGCOUNTRY,
NULL,
0);
if (dwCchCountrySize == 0)
{
hr = HRESULT_FROM_WIN32(GetLastError());
}
}
// Allocate a buffer for the full name
if (SUCCEEDED(hr))
{
pszLanguageCountryName = (LPSTR)HeapAlloc(
GetProcessHeap(),
0,
(dwCchLanguageSize + dwCchCountrySize) * sizeof(CHAR));
if (pszLanguageCountryName == NULL)
{
hr = HRESULT_FROM_WIN32(GetLastError());
}
}
// Copy the language name
if (SUCCEEDED(hr))
{
dwCchSize = GetLocaleInfoA(
lcidLocale,
LOCALE_SENGLANGUAGE,
pszLanguageCountryName,
dwCchLanguageSize);
if (dwCchLanguageSize != dwCchSize)
{
hr = HRESULT_FROM_WIN32(GetLastError());
}
}
// Add the underscore separator
if (SUCCEEDED(hr))
{
pszLanguageCountryName[dwCchLanguageSize - 1] = '_';
}
// Copy the country name
if (SUCCEEDED(hr))
{
dwCchSize = GetLocaleInfoA(
lcidLocale,
LOCALE_SENGCOUNTRY,
pszLanguageCountryName + dwCchLanguageSize,
dwCchCountrySize);
if (dwCchCountrySize != dwCchSize)
{
hr = HRESULT_FROM_WIN32(GetLastError());
}
}
// Create the locale
if (SUCCEEDED(hr))
{
*ptLocale = _create_locale(nCategory, pszLanguageCountryName);
if (*ptLocale == NULL)
{
hr = E_FAIL;
}
}
// Cleanup
if (pszLanguageCountryName != NULL)
{
HeapFree(GetProcessHeap(), 0, pszLanguageCountryName);
pszLanguageCountryName = NULL;
}
return hr;
}
Usage:
_locale_t tLocale;
HRESULT hr = LcidToCrtLocale(LC_NUMERIC, lcidLocale, &tLocale);
...._vscwprintf_l...
_free_locale(tLocale);
Cheers