PlatformVB

RASGetCountryInfo

The RASGetCountryInfo function retrieves the country code and name of a country.  You need to specify the countryID .  The function also returns the countryID of the next country as stored in the registry.  

From the platform SDK, the function is passed a RASCOUNTRYINFO structure which looks like this:

typedef struct RASCTRYINFO {
  DWORD   dwSize;
  DWORD   dwCountryID;
  DWORD   dwNextCountryID;
  DWORD   dwCountryCode;
  DWORD   dwCountryNameOffset;
} RASCTRYINFO

Note: the above structure actually stores a pointer to the Country Name, not the string itself.  In VB we usually deal with variable length strings not pointers to strings.  So our VB type, declarations and wrapper function might look something like this:

Type VBRASCTRYINFO
    CountryCode As Long
    CountryID As Long
    CountryName As String
    NextCountryID As Long
End Type

Declare Function RasGetCountryInfo _
   Lib "rasapi32.dll" Alias "RasGetCountryInfoA" _
   (lpRasCtryInfo As Any, lpdwSize As Long) _
As Long
        
Public Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" _
        (Destination As Any, Source As Any, ByVal Length As Long)
'_______________________________________________________________

Function VBRasGetCountryInfo _
      (clsCountryInfo As VBRASCTRYINFO) As Long
   
   Dim b(511) As Byte, lpSize As Long, rtn As Long
   Dim lPos As Long, strTemp As String, lngLen As Long
   b(0) = 20
   CopyMemory b(4), clsCountryInfo.CountryID, 4
   lpSize = 512
   
   rtn = RasGetCountryInfo(b(0), lpSize)
   
   VBRasGetCountryInfo = rtn
   If rtn <> 0 Then Exit Function
   
   CopyMemory clsCountryInfo.NextCountryID, b(8), 4
   CopyMemory clsCountryInfo.CountryCode, b(12), 4
   
   CopyMemory lPos, b(16), 4
   lngLen = lpSize - lPos - 2
   
   If lngLen > 0 Then
      strTemp = String(lngLen, 0)
      CopyMemory ByVal strTemp, b(lPos), lngLen
   End If
   
   clsCountryInfo.CountryName = strTemp
End Function

 

The VBRasGetCountryInfo function above returns a long.  The return value is 0 for a successful operation. If the return value is not zero use the VBRasErrorHandler function to return an error description. 

You can call the above function like so:

Dim MyCountry As VBRASCTRYINFO
MyCountry.CountryID = 61 
rtn = VBRasGetCountryInfo(MyCountry)

Note: You have to specify the Country ID.  61 is Australia,  1 is USA.

You can also enumerate all the countries, by starting with CountryID =1, then calling the VBRasGetCountryInfo again, substituting the CountryID with the previously returned NextCountryID.

This sample shows how to call on the above function to enumerate all countries and print their code and name to the Debug window in VB.

 

Dim lngID As Long, rtn As Long
Dim MyCountry As VBRASCTRYINFO
lngID = 1

Do
   MyCountry.CountryID = lngID
   rtn = VBRasGetCountryInfo(MyCountry)

   With MyCountry
        Debug.Print .CountryCode, .CountryName
    End With

    If rtn <> 0 Then Exit Do
    lngID = MyCountry.NextCountryID
Loop While lngID <> 0

 

 

See Also: Contents, Introduction, RasErrorHandler.