This is to produce a country list. This is the exact same list that is shown in the dropdown under Region & Language on the Location Tab.
I will include the VB6 code first, then the the VB.NET code that the VS2008 VB to VB.NET wizard made. Ultimatly I'd like to convert it to C# but so far I've not been able to. You will see in the code listed that the VB6 to VB.NET conversion produced some upgrade warnings. Since I've not done alot of Win32 API calls I'm really not sure how to make this work in C#.
The wizard did include some HELP links but for some reason I can not get them to work.
VB6
modCountryList.bas
Option Explicit
Public LCID As Long
Private Const GEO_FRIENDLYNAME As Long = &H8
Public Const GEOCLASS_NATION As Long = 16
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
With frmCountryList.List1
.AddItem GetGeoFriendlyName(geoid, LCID)
End With
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
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
frmCountryList.frm
Private Sub Form_Load()
LCID = GetUserDefaultLCID()
Call EnumSystemGeoID(GEOCLASS_NATION, 0&, AddressOf EnumGeoInfoProc)
End Sub
VB.NET
modCountryList.vb
Option Strict Off
Option Explicit On
Module modCountryList
Public LCID As Integer
Private Const GEO_FRIENDLYNAME As Integer = &H8
Public Const GEOCLASS_NATION As Integer = 16
Public Declare Function GetUserDefaultLCID Lib "Kernel32" () As Integer
Private Declare Function GetGeoInfo Lib "Kernel32" Alias "GetGeoInfoA"(ByVal geoid As Integer, ByVal GeoType As Integer, ByRef lpGeoData As Any, ByVal cchData As Integer, ByVal langid As Integer) As Integer
Public Declare Function EnumSystemGeoID Lib "Kernel32" (ByVal geoclass As Integer, ByVal ParentGeoId As Integer, ByVal lpGeoEnumProc As Integer) As Integer
Private Declare Function lstrlenW Lib "Kernel32" (ByVal lpString As Integer) As Integer
Public Function EnumGeoInfoProc(ByVal geoid As Integer) As Integer
With frmCountryList.List1
.Items.Add(GetGeoFriendlyName(geoid, LCID))
End With
EnumGeoInfoProc = 1
End Function
Private Function GetGeoFriendlyName(ByRef geoclass As Integer, ByRef LCID As Integer) As String
Dim lpGeoData As String
Dim cchData As Integer
Dim nRequired As Integer
lpGeoData = ""
cchData = 0
nRequired = GetGeoInfo(geoclass, GEO_FRIENDLYNAME, lpGeoData, cchData, LCID)
If (nRequired > 0) Then
lpGeoData = Space(nRequired)
cchData = nRequired
Call GetGeoInfo(geoclass, GEO_FRIENDLYNAME, lpGeoData, cchData, LCID)
GetGeoFriendlyName = TrimNull(lpGeoData)
End If
End Function
Private Function TrimNull(ByRef startstr As String) As String
TrimNull = Left(startstr, lstrlenW(StrPtr(startstr)))
End Function
End Module
frmCountryList.vb
Option Strict Off
Option Explicit On
Friend Class frmCountryList
Inherits System.Windows.Forms.Form
Private Sub frmCountryList_Load(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles MyBase.Load
LCID = GetUserDefaultLCID()
Call EnumSystemGeoID(GEOCLASS_NATION, 0, AddressOf EnumGeoInfoProc)
End Sub
End Class