MarthinusSwart.com

Win32 Language Information Conversion

Converting C++ Win32 code into C#.NET code could be a daunting task. I recently had to convert C++ code to C# that had to access the local machine's language settings.

The code snippet below is the original C++ code

LCID lcid;
if (lcid == -1)
  lcid = ::GetUserDefaultLCID();
if((BYTE)lcid == LANG_ENGLISH)
  return S_FALSE;

In C# you can invoke the GetUserDefaultLCID Win32 API function but that will not be the best way of doing it. C# has a great class called CultureInfo in the System.Globalization assembly that provides all the functionality needed when you need to create multi cultural software. The C# equivalent of the same function would be as follows

CultureInfo cultureInfo;
cultureInfo = CultureInfo.CurrentCulture;
if (cultureInfo.TwoLetterISOLanguageName.Equals("en"))
  return false;

If the LCID is still needed for some reason e.g. COM interop, the LCID can still be retrieved from the CultureInfo. Below is an example

CultureInfo cultureInfo;
cultureInfo = CultureInfo.CurrentCulture;
//if (cultureInfo.TwoLetterISOLanguageName.Equals("en"))
if (cultureInfo.LCID == LANG_ENGLISH)
  return false;

The LANG_ENGLISH constant should just have the same integer value than the original Win32 constant.