PlatformVB

RasEnumConnections

The RasEnumConnections returns an array of RASCONN structures.  In VB we can declare the function as follows:

Declare Function RasEnumConnections _
      Lib "rasapi32.dll" Alias "RasEnumConnectionsA" _
         (lpRasconn As Any, _
          lpcB As Long, _
          lpcConnections As Long) As Long

Note: the lpRasconn parameter is usually an array of RASCONN structures, but I have declared it "As Any" so as we can use an array of bytes.

RASCONN structure

From the platform SDK, the RASCONN structure is defined as:

typedef struct _RASCONN {
  DWORD     dwSize;
  HRASCONN  hrasconn;
  TCHAR     szEntryName[RAS_MaxEntryName + 1];

#if (WINVER >= 0x400)
  TCHAR    szDeviceType[ RAS_MaxDeviceType + 1 ];
  TCHAR    szDeviceName[ RAS_MaxDeviceName + 1 ];
#End If
#if (WINVER >= 0x401)
  TCHAR    szPhonebook [ MAX_PATH ];
  DWORD    dwSubEntry;
#End If
#if (WINVER >= 0x500)
  GUID     guidEntry;
#End If
} RASCONN ;

This gives us four possible sized structures being : 692, 676, 412,  or 32 bytes long.

Our VB friendly type declaration for this structure would be as follows:

' VB "friendly" RASCONN structure

Type VBRASCONN
   hRasConn As Long
   sEntryName As String
   sDeviceType As String
   sDeviceName As String
   sPhonebook  As String
   lngSubEntry As Long
   guidEntry(15) As Byte
End Type

 

Enumerating connections

The following code shows how to enumerate connections and populate an array of VBRASCONN structures.  The function returns a long equal to the number of connections.

You could call this function like so:

Dim nConnections as Long
Dim myConnections() as VBRASCONN
nConnections = VBRasEnumConnections(myConnections)

 

Function VBRasEnumConnections(aVBRasConns() As VBRASCONN) As Long

   Dim rtn As Long
   Dim b() As Byte
   Dim aLens As Variant, dwSize As Long
   Dim lpcB As Long, lpConns As Long
   Dim i As Long
   
   ReDim b(3)
   aLens = Array(692&, 676&, 412&, 32&)

   For i = 0 To 3
      dwSize = aLens(i)
      CopyMemory b(0), dwSize, 4
      lpcB = 4
      rtn = RasEnumConnections(b(0), lpcB, lpConns)
      If rtn <> 632 And rtn <> 610 Then Exit For
   Next i
   
   VBRasEnumConnections = lpConns
   If lpConns = 0 Then Exit Function
   
   lpcB = dwSize * lpConns
   ReDim b(lpcB - 1)
   CopyMemory b(0), dwSize, 4
   rtn = RasEnumConnections(b(0), lpcB, lpConns)
   
   ' now copy the bytes to the aVBRasConns array
   ReDim aVBRasConns(lpConns - 1)
   For i = 0 To lpConns - 1
      With aVBRasConns(i)
         CopyMemory .hRasConn, b(i * dwSize + 4), 4
         If dwSize = 32& Then
            CopyByteToTrimmedString .sEntryName, b(i * dwSize + 8), 21&
         Else
            CopyByteToTrimmedString .sEntryName, b(i * dwSize + 8), 257&
            CopyByteToTrimmedString .sDeviceType, b(i * dwSize + 265), 17&
            CopyByteToTrimmedString .sDeviceName, b(i * dwSize + 282), 129&
            If dwSize > 412& Then
              CopyByteToTrimmedString .sPhonebook, b(i * dwSize + 411), 260&
              CopyMemory .lngSubEntry, b(i * dwSize + 672), 4
              If dwSize > 676& Then
                CopyMemory .guidEntry(0), b(i * dwSize + 676), 16
              End If
            End If
         End If
      End With
   Next i
End Function
' helper function
Sub CopyByteToTrimmedString(strToCopyTo As String, _
                              bPos As Byte, lngMaxLen As Long)
   Dim strTemp As String, lngLen As Long
   strTemp = String(lngMaxLen + 1, 0)
   CopyMemory ByVal strTemp, bPos, lngMaxLen
   lngLen = InStr(strTemp, Chr$(0)) - 1
   strToCopyTo = Left$(strTemp, lngLen)
End Sub

 

 

See Also: Contents, Introduction