By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,647 Members | 1,444 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,647 IT Pros & Developers. It's quick & easy.

GetLocaleInfoEx bombs out

PhilOfWalton
Expert 100+
P: 1,430
I have been using GetLocaleInfo for some time (mainly to get country names) and this works perfectly.

I read somewhere, this has been replaces by GetLocaleInfoEx.

I can't get this to work and it crashes Access.

Sorry the code is a bit long winded
Expand|Select|Wrap|Line Numbers
  1. Option Compare Database
  2. Option Explicit
  3.  
  4. Public Const LOCALE_ALL = &H2                   ' 2 All information
  5. Public Const LOCALE_SYSTEM_DEFAULT& = &H800
  6. Public Const LOCALE_NAME_USER_DEFAULT = vbNullString
  7. Public Const LOCALE_SNATIVELANGNAME As Long = &H4 ' native name of language
  8. Public Const LOCALE_ICOUNTRY As Long = &H5
  9. Public Const LOCALE_SCOUNTRY As Long = &H6
  10. Public Const LOCALE_SABBREVCTRYNAME As Long = &H7
  11. Public Const LOCALE_SNATIVECTRYNAME As Long = &H8 ' native name of country
  12. Public Const LOCALE_USER_DEFAULT As Long = &H400
  13. Public Const LOCALE_SENGLANGUAGE As Long = &H1001 ' English name of language
  14. Public Const LOCALE_SENGCOUNTRY As Long = &H1002 ' English name of country
  15. Public Const LOCALE_ILANGUAGE As Long = &H1
  16. Public Const LOCALE_SLANGUAGE As Long = &H2
  17.  
  18. Const cMAXLEN = 255
  19. Private 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
  20.  
  21. Private Declare Function GetLocaleInfoEx Lib "kernel32" (ByVal LocaleNmae As String, ByVal LCType As Long, ByVal lpLCData As String, ByVal cchData As Long) As Long
  22. Private Declare Function GetLastError Lib "kernel32" () As Long
  23.  
  24. Public Function CountryName(Optional InfoRequired As Long) As String
  25.  
  26.     Dim lngLocale As Long
  27.     Dim strLCData As String, lngData As Long
  28.     Dim lngX As Long
  29.     Dim Lngi As Long
  30.  
  31.     strLCData = String$(cMAXLEN, 0)
  32.     lngData = cMAXLEN - 1
  33.     If InfoRequired = 2 Or Nz(InfoRequired) = 0 Then
  34.         For Lngi = 1 To 120
  35.             lngX = GetLocaleInfo(LOCALE_USER_DEFAULT, Lngi, strLCData, lngData)
  36.             If lngX <> 0 Then
  37.                 Debug.Print LOCALE_USER_DEFAULT & "   " & Lngi & "   " & Left$(strLCData, lngX - 1)
  38.             End If
  39.         Next Lngi
  40.     Else
  41.         lngX = GetLocaleInfo(LOCALE_USER_DEFAULT, InfoRequired, strLCData, lngData)
  42.         If lngX <> 0 Then
  43.             CountryName = Left$(strLCData, lngX - 1)
  44.             'Debug.Print LOCALE_USER_DEFAULT & "   " & Left$(strLCData, lngX - 1)
  45.         End If
  46.     End If
  47.  
  48. End Function
  49.  
  50. Public Function Example()
  51.  
  52.     Dim lpLCData As String
  53.     Dim bufferSize As Long
  54.     Dim RetVal
  55.     Dim lngX As Long
  56.     Dim Lngi As Long
  57.  
  58.     For Lngi = 1 To 120
  59.         'Get return buffer size.
  60.         RetVal = GetLocaleInfoEx(LOCALE_NAME_USER_DEFAULT, Lngi, lpLCData, 0)
  61.         'GetLocaleInfoEx returns 0 on failure.
  62.         If RetVal <> 0 Then
  63.             'Use that value to size your buffer...
  64.             lpLCData = String(RetVal, Chr$(0))
  65.             '...and pass the appropriate buffer/size parameters.
  66.             lngX = GetLocaleInfoEx(LOCALE_NAME_USER_DEFAULT, Lngi, lpLCData, RetVal)
  67.             If lngX <> 0 Then
  68.                 Debug.Print lpLCData
  69.             Else
  70.                 Debug.Print GetLastError
  71.             End If
  72.         Else
  73.             Debug.Print GetLastError
  74.         End If
  75.     Next Lngi
  76.  
  77. End Function
  78.  
The CountryName example works, but the Example tries to print something, then Access crashes.

Grateful for any observations

Phil
Jul 16 '17 #1

✓ answered by ADezii

I would try experimenting with the Buffer Size, I allocated what I thought was the MAX.

Share this Question
Share on Google+
5 Replies


ADezii
Expert 5K+
P: 8,607
Try changing the manner in which your allocated your Buffer Size, I am referring to Code Line# 130.
Expand|Select|Wrap|Line Numbers
  1. Public Function Example()
  2. 10 Dim lpLCData As String
  3. 20 Dim bufferSize As Long
  4. 30 Dim RetVal
  5. 40 Dim lngX As Long
  6. 50 Dim Lngi As Long
  7. 60
  8. 70 For Lngi = 1 To 120
  9. 80  'Get return buffer size.
  10. 90   RetVal = GetLocaleInfoEx(LOCALE_NAME_USER_DEFAULT, Lngi, lpLCData, 0)
  11. 100  'GetLocaleInfoEx returns 0 on failure.
  12. 120  If RetVal <> 0 Then
  13. 130    lpLCData = String$(cMAXLEN, 0)
  14. 140
  15. 150    '...and pass the appropriate buffer/size parameters.
  16. 160    lngX = GetLocaleInfoEx(LOCALE_NAME_USER_DEFAULT, Lngi, lpLCData, RetVal)
  17. 170
  18. 180    If lngX <> 0 Then
  19. 190      Debug.Print lpLCData
  20. 200    Else
  21. 210      Debug.Print GetLastError
  22. 220    End If
  23. 230  Else
  24. 240    Debug.Print GetLastError
  25. 250  End If
  26. 260 Next Lngi
  27. End Function
Jul 17 '17 #2

PhilOfWalton
Expert 100+
P: 1,430
Thanks as ever for that. At least it doesn't bomb out.

The difference now is the first version (CounrtyName) prints out all the fields normally, and in the new version (Example) there is a space between each letter. Obviously I can code this to remove the space, but the question is "is this the way the data is stored or is it a glitch in the way the data is displayed?"

Again thanks

Phil
Jul 17 '17 #3

ADezii
Expert 5K+
P: 8,607
I would try experimenting with the Buffer Size, I allocated what I thought was the MAX.
Jul 18 '17 #4

PhilOfWalton
Expert 100+
P: 1,430
Thanks ADezii. That put me on the right track.

This is what works
Expand|Select|Wrap|Line Numbers
  1. Public Function CountryName(Optional InfoRequired As Long) As String
  2.  
  3.     Dim strLCData As String
  4.     Dim BufferSize As Long
  5.     Dim lngX As Long
  6.     Dim Lngi As Long
  7.     Dim LngStart As Long
  8.     Dim LngEnd As Long
  9.  
  10.     If InfoRequired = LOCALE_ALL Or Nz(InfoRequired) = 0 Then
  11.         LngStart = 1
  12.         LngEnd = 120
  13.     Else
  14.         LngStart = InfoRequired
  15.         LngEnd = InfoRequired
  16.     End If
  17.  
  18.     For Lngi = LngStart To LngEnd
  19.         'Get return buffer size.
  20.         BufferSize = GetLocaleInfoEx(LOCALE_NAME_USER_DEFAULT, Lngi, strLCData, 0)
  21.         'GetLocaleInfoEx returns 0 on failure.
  22.         If BufferSize <> 0 Then
  23.             strLCData = String$(cMAXLEN, 0)
  24.             '...and pass the appropriate buffer/size parameters.
  25.             lngX = GetLocaleInfoEx(LOCALE_NAME_USER_DEFAULT, Lngi, strLCData, BufferSize)
  26.             If lngX <> 0 Then
  27.                 ' There appear to be blanks (Chr$(0) betweeen each letter, so remove them
  28.                 strLCData = Replace(strLCData, Chr$(0), "")
  29.                 If LngStart = LngEnd Then                   ' single value required
  30.                     'Debug.Print Left(strLCData, BufferSize)
  31.                     CountryName = Left(strLCData, BufferSize)
  32.                 Else
  33.                     Debug.Print Lngi; "   "; Left(strLCData, BufferSize)
  34.                 End If
  35.             Else
  36.                 Debug.Print GetLastError
  37.             End If
  38.         Else
  39.             Debug.Print GetLastError
  40.         End If
  41.     Next Lngi
  42.  
  43. End Function
  44.  
Best wishes

Phil
Jul 18 '17 #5

ADezii
Expert 5K+
P: 8,607
Glad you got it all working, Phil.
Jul 18 '17 #6

Post your reply

Sign in to post your reply or Sign up for a free account.