Visual Basic Locale/Regionalization Routines

GetGeoInfo: Obtaining Country List by Enumerating GeoIDs
Posted:   Sunday August 15, 2004
Updated:   Monday December 26, 2011
Applies to:   VB4-32, VB5, VB6
Developed with:   VB6, Windows XP
OS restrictions:   Windows ME, Windows XP, Windows Server 2003
Author:   VBnet - Randy Birch


GetGeoInfo: Obtaining Country List by Enumerating GeoIDs
GetGeoInfo: Determine Geographic Country Info by GeoID

EnumSystemLocales: Enumerate Installed and Supported System Locales
GetLocaleInfo: Regional Locale Country Settings
Windows ME, Windows XP or Windows Server 2003.

EnumSystemGeoID returns a listing of the GeoIDs that are available on the system. By passing each retrieved GeoID to GetGeoInfo - specifically the GetGeoFriendlyName wrapper - we can obtain a pretty substantial listing of world countries.  My system returned 260.

Because this demo implements an enumeration routine a BAS module is required. The GEO APIs require Windows ME, XP or Server 2003.


 BAS Module Code
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.
'working var
Public LCID As Long

Private Const GEO_FRIENDLYNAME As Long = &H8

Public Const GEOCLASS_NATION As Long = 16 'only valid GeoClass value

Public Declare Function GetUserDefaultLCID Lib "Kernel32" () As Long

Private Declare Function GetGeoInfo Lib "Kernel32" _
   Alias "GetGeoInfoA" _
  (ByVal geoid As Long, _
   ByVal GeoType As Long, _
   lpGeoData As Any, _
   ByVal cchData As Long, _
   ByVal langid As Long) As Long
Public Declare Function EnumSystemGeoID Lib "Kernel32" _
  (ByVal geoclass As Long, _
   ByVal ParentGeoId As Long, _
   ByVal lpGeoEnumProc As Long) As Long
Private Declare Function lstrlenW Lib "Kernel32" _
  (ByVal lpString As Long) As Long

Public Function EnumGeoInfoProc(ByVal geoid As Long) As Long

  'add the data to the list
   With Form1.List1
     'if the GeoId returned from the Enum
     'matches the GeoId determined at Load
     'for the user, append a string to that
     'combo item and record the list index
     'of that item
      .AddItem GetGeoFriendlyName(geoid, LCID)
   End With
  'return 1 to continue enumeration
   EnumGeoInfoProc = 1
End Function

Private Function GetGeoFriendlyName(geoclass As Long, LCID As Long) As String

   Dim lpGeoData As String
   Dim cchData As Long
   Dim nRequired As Long
   lpGeoData = ""
   cchData = 0
  'call once with an empty string; the return 
  'value indicates the size of the buffer required    
   nRequired = GetGeoInfo(geoclass, GEO_FRIENDLYNAME, ByVal lpGeoData, cchData, LCID)
   If (nRequired > 0) Then
      lpGeoData = Space$(nRequired)
      cchData = nRequired
      Call GetGeoInfo(geoclass, GEO_FRIENDLYNAME, ByVal lpGeoData, cchData, LCID)
      GetGeoFriendlyName = TrimNull(lpGeoData)
   End If
End Function

Private Function TrimNull(startstr As String) As String

   TrimNull = Left$(startstr, lstrlenW(StrPtr(startstr)))
End Function

 Form Code
All that is required on the form is a listbox (List1) and two lines of code:

Option Explicit
Private Sub Form_Load()

   LCID = GetUserDefaultLCID()
   Call EnumSystemGeoID(GEOCLASS_NATION, 0&, AddressOf EnumGeoInfoProc)
End Sub


PayPal Link
Make payments with PayPal - it's fast, free and secure!


Copyright 1996-2011 VBnet and Randy Birch. All Rights Reserved.
Terms of Use  |  Your Privacy


Hit Counter