Getting locale information using the GetLocaleInfoEx Windows API Function
- John
- Sep 2
- 3 min read
Updated: Sep 3
This post is one of a series providing implementation examples of Windows API Functions, Types, Enums and Consts using VBA. The code in this post can be used as-is, however, if you regularly (or even just occasionally) work with Windows API declarations in VBA, you may want to see the posts Automatically add Windows API declaration(s) and Using 'F1' to view Windows API web pages which explain some of the functionality that can be added to the VBE by VBE_Extras.
This post demonstrates how to use the GetLocaleInfoEx Windows API Function to get locale-related information. GetLocaleInfoEx uses a familiar pattern with Windows API Functions that "return" a String. You call it once passing in arguments being a pointer to a buffer and a numeric being the size of the buffer (in this case, the buffer is zero-length and so the numeric is 0). As the buffer cannot, then, be large enough, the Function returns the size of buffer required. The Function is then called a second time with the buffer at the correct size and then the Function populates the buffer.
The other arguments passed to GetLocaleInfoEx allow you to specify:
The locale for which you want to get information (lpLocaleName) ... this can be either a pointer to a locale name (such as "en-GB" for Great Britain or "de-DE" for Germany) or, as in the example code below, can be one of the LOCALE_NAME* Constants ... see LOCALE_NAME* Constants for details
The type of information you want to get (LCType) ... there are around 170 options covering dates, countries, languages, currencies, number formats and many, many more ... see Locale Information Constants for details
Part of the difficulty of working with GetLocaleInfoEx is that, at both of the pages linked to above, the Constants do not provide their associated value which is essential for VBA (where each Constants is defined as a Const with an explicit value) ... take a look at the pages and you will see what I mean ... the name of the Const is present, it's value is not. However, VBE_Extras does know the numeric value for each Const and can add this to your code ... see Automatically add Windows API declaration(s).
To support this, the GetLastError Windows API Function is also used to get an error code if the call of GetLocaleInfoEx is not successful. You can use Err.LastDllError instead ... but the point of this post is to demonstrate using the Windows API!
So here is an example. The VBA Function TryGetLocaleInfo encapsulates the Windows API calls .... it tries to get the locale information required returning True if it is successful or False otherwise. If successful, the locale information is 'returned' using the outsInfo parameter. If unsuccessful, an error code is 'returned' using the 'outlErrCode' parameter. See the code comments for further details.
And here is some example code showing how you might use TryGetLocaleInfo.
The TestTryGetLocaleInfo Sub sends to the Immediate window the "localised country name" using LOCALE_SLOCALIZEDCOUNTRYNAME and the "short date formatting string" using LOCALE_SSHORTDATE for the locale selected in the "Region" dialog in the Control Panel (because LOCALE_NAME_USER_DEFAULT is being used via the LocaleNameOption Enum).
For me, these are "United Kingdom" and "dd/MM/yyyy" ... if I use the Control Panel to change the "Region" to "English (United States)" then "United States" and "M/d/yyyy" are sent to the Immediate window instead.
You can change LOCALE_NAME_USER_DEFAULT to something else (using the other LocaleNameOption Enum members).
And you can change the LOCALE_* Consts to something else ... a few other examples Consts are included in the above code or you can add any one of the other 180+ values from the Locale Information Constants page if you can work out the numeric value to use ... or, simpler, use VBE_Extras to add the Consts with their values ... like this ...




Comments