Get Windows display Language

youki 1,021 Reputation points
2023-09-25T09:27:58.9566667+00:00

I tried several things and according to following post, it should work but I can't get the Windows display language.

https://stackoverflow.com/questions/52849233/getuserpreferreduilanguages-never-returns-more-than-two-languages

Has anything changed in Windows 11?
I selected German (Luxemburg) but I can't get the value which should be 4103 according to the language id list:

https://renenyffenegger.ch/notes/Windows/development/Internationalization/language

My settings (I newly logged in and restartet the pc several times):
User's image

My code (also tried GetUserPreferredUILanguages):
User's image

User's image

auto a = GetUserDefaultUILanguage();    
auto b = GetSystemDefaultUILanguage();    
auto c = GetThreadUILanguage();    
auto d = GetUserDefaultLangID();    
auto e = GetSystemDefaultLangID();    
auto f = GetUserDefaultLCID();    
auto g = GetSystemDefaultLCID();     
ULONG pulNumLanguages = 0;    
ULONG pcchLanguagesBuffer = 80;    
wchar_t pwszLanguagesBuffer[80];    GetSystemPreferredUILanguages(MUI_MACHINE_LANGUAGE_SETTINGS, &pulNumLanguages, pwszLanguagesBuffer, &pcchLanguagesBuffer);     
auto h = pwszLanguagesBuffer + 6;
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,818 questions
C++
C++
A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.
3,682 questions
Windows 11
Windows 11
A Microsoft operating system designed for productivity, creativity, and ease of use.
9,393 questions
{count} votes

1 answer

Sort by: Most helpful
  1. youki 1,021 Reputation points
    2023-09-25T18:48:40.6066667+00:00

    Oh man, it took forever, so I might as well post it as it suits me.

    // Returns only the first null terminated string (selected Windows display language).
    std::basic_string<TCHAR> GetWindowsDisplayLanguageFromRegistry()
    {
        LPCTSTR subKey = _T("Control Panel\\International\\User Profile\\");
        LPCTSTR valueName = _T("Languages");
        DWORD bufferSize = 0;
    
        // Check buffer size.
        if (RegGetValue(HKEY_CURRENT_USER, subKey, valueName, RRF_RT_REG_MULTI_SZ, nullptr, nullptr, &bufferSize) == ERROR_SUCCESS)
        {
            //  Manage memory.
            std::vector<TCHAR> value(bufferSize / sizeof(TCHAR));
    
            // Get registry key value.
            if (RegGetValue(HKEY_CURRENT_USER, subKey, valueName, RRF_RT_REG_MULTI_SZ, nullptr, &value[0], &bufferSize) == ERROR_SUCCESS)
            {
                // Ensure null-termination.
                value[bufferSize / sizeof(TCHAR)] = _T('\0');
    
                std::basic_string<TCHAR> firstString(value.data());
    
                return firstString;
            }
        }
    
        return std::basic_string<TCHAR>();
    }
    
    1 person found this answer helpful.
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.