473,473 Members | 2,282 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

GetLocaleInfoEx bombs out

PhilOfWalton
1,430 Recognized Expert Top Contributor
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
5 1744
ADezii
8,834 Recognized Expert Expert
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
1,430 Recognized Expert Top Contributor
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
8,834 Recognized Expert Expert
I would try experimenting with the Buffer Size, I allocated what I thought was the MAX.
Jul 18 '17 #4
PhilOfWalton
1,430 Recognized Expert Top Contributor
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
8,834 Recognized Expert Expert
Glad you got it all working, Phil.
Jul 18 '17 #6

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

Similar topics

4
by: Noah Coad [MVP .NET/C#] | last post by:
For no appearnt reason, the AutoComplete stopped showing me the members of a number of classes used in my project. It was working just fine, and as of yeasterday, AutoComplete stopped working. It...
3
by: Hasani | last post by:
I'm creating a .net db provider and I came across a weird problem w/ my data reader. The .net provider was created in managed c++ becuase the db api is also c++. I'm calling the code from a c#...
4
by: clintonG | last post by:
I'm using the intrinsic Request object wrong and need some help with this one. I wrote a class file for some XMLTextWriter tasks and in the class file I want to use a conditional to determine if...
3
by: John | last post by:
Hi I have a very simply app with a single web form with a single button on it and has the following code; Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)...
2
by: deergregoryd | last post by:
I've got some PHP and javascript working together to create a marginless image popup from another arbitrary image. This works perfectly in Firefox, but IE bombs and I'm not really knowledgable...
5
by: BenG | last post by:
Hi. I have a gridview control on a web form (asp.net 2.0) that's bound to a objectDataSource. The objectdatasource which is bound to a class I've written in the DAL to read and update the database....
4
by: LetMeDoIt | last post by:
Greetings, I'm using ASP to retrieve from MSSQL and I then populate a table. After several months of successfull retrieves, this same code now bombs out. It turns out that if I clear out from...
6
by: John | last post by:
Hi I have a vs 2003 win form app which runs fine in IDE but once it is deployed via setup it bombs on start-up screen and wants to send MS the error report. This happens on multiple pc including...
5
kadghar
by: kadghar | last post by:
Most of the times VBA is used with variables. Objects (such as worksheets, cells or databases) are only used when we read their properties (value, formula, font...) or we use a method (save,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.