Search code examples
vb6

Get current culture in VB6


How do I get current culture in VB6? I need to be able to determine where the application is running so that I can format dates appropriately.

For example:

if culture = US or CA or MX
   dateFormat = "MM/DD/YYYY"
else
   dateFormat = "DD/MM/YYYY"

Solution

  • The Windows API can provide culture information fairly easily:

    Option Explicit
    
    Private Const LOCALE_SENGCOUNTRY As Long = &H1002  'english name of country
    Private Const LOCALE_SABBREVCTRYNAME As Long = &H7 'abbreviated country name
    
    Private Declare Function GetSystemDefaultLCID Lib "kernel32" () As Long
    Private Declare Function GetLocaleInfoA Lib "kernel32" (ByVal Locale As Long, ByVal LCType As Long, ByVal lpLCData As String, ByVal cchData As Long) As Long
    
    Public Sub Test()
       Dim LCID As Long
       Dim Name As String
       
       'you could use the locale id; the US is 1033
       LCID = GetSystemDefaultLCID()
       
       'or convert the locale id to an abbreviation, such as USA
       Name = GetUserLocaleInfo(LCID, LOCALE_SABBREVCTRYNAME)
       
       'or convert the locale id to a name, such as United States
       Name = GetUserLocaleInfo(LCID, LOCALE_SENGCOUNTRY)
    End Sub
    
    Public Function GetUserLocaleInfo(ByVal dwLocaleID As Long, ByVal dwLCType As Long) As String
       Dim Data As String
       Dim r As Long
    
       r = GetLocaleInfoA(dwLocaleID, dwLCType, Data, Len(Data))
        
       If r Then
          Data = Space$(r)
          r = GetLocaleInfoA(dwLocaleID, dwLCType, Data, Len(Data))
         
          If r Then
             GetUserLocaleInfo = Left$(Data, r - 1)
          End If
       End If
    End Function