Option Explicit
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Copyright ©1996-2011 VBnet/Randy Birch, All Rights Reserved.
' Some pages may also contain other copyrights by the author.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Distribution: You can freely use this code in your own
' applications, but you may not reproduce
' or publish this code on any web site,
' online service, or distribute as source
' on any media without express permission.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Private Const LOCALE_SSHORTDATE As Long = &H1F 'short date format string
Public Declare Function GetSystemDefaultLCID Lib "kernel32" () As Long
Public Declare Function GetLocaleInfo Lib "kernel32" _
Alias "GetLocaleInfoA" _
(ByVal Locale As Long, _
ByVal LCType As Long, _
ByVal lpLCData As String, _
ByVal cchData As Long) As Long
Private Sub Command1_Click()
MsgBox GetShortDateFormat
End Sub
Private Function GetShortDateFormat() As String
Dim LCID As Long
'get the locale for the user
LCID = GetSystemDefaultLCID()
If LCID <> 0 Then
'return the short date format
GetShortDateFormat = GetUserLocaleInfo(LCID, LOCALE_SSHORTDATE)
End If
End Function
Private Function GetUserLocaleInfo(ByVal dwLocaleID As Long, _
ByVal dwLCType As Long) As String
Dim sReturn As String
Dim r As Long
'call the function passing the Locale type
'variable to retrieve the required size of
'the string buffer needed
r = GetLocaleInfo(dwLocaleID, dwLCType, sReturn, Len(sReturn))
'if successful..
If r Then
'pad the buffer with r spaces
sReturn = Space$(r)
'and call again passing the buffer
r = GetLocaleInfo(dwLocaleID, dwLCType, sReturn, Len(sReturn))
'if successful (r > 0)
If r Then
'r holds the size of the string
'including the terminating null
GetUserLocaleInfo = Left$(sReturn, r - 1)
End If
End If
End Function |
While the GetSystemDefaultLCID function retrieves the system default
locale identifier, this is often inappropriate or insufficient in a networked
environment or under an operating system where multiple locales have
been installed. For example, it is possible for a network admin rolling
out a standard image to have the user's default locale set to one
differing from the base OS installation, and thus the system default
locale.
In this situation Windows' provides an alternate API you can use to
obtain the LCID for the current user ... GetUserDefaultLCID. Defined
identically to GetSystemDefaultLCID, GetUserDefaultLCID function
retrieves the user default–locale identifier and is therefore
the most appropriate API to use when it is the user's locale
you are interested in, rather than that of the system. |