The RasGetConnectStatus function looks like this:
Declare Function RasGetConnectStatus _ Lib "rasapi32.dll" Alias "RasGetConnectStatusA" _ (ByVal hRasConn As Long, _ lpRasConnStatus As Any) As Long
The hRasConn parameter is a connections handle which you got from the RasDial or RasEnumConnections functions. The lpRasConnStatus is a RASCONNSTATUS structure which is defined in the Platform SDK as follows:
RASCONNSTATUSA { DWORD dwSize; 4 RASCONNSTATE rasconnstate; 4 DWORD dwError; 4 CHAR szDeviceType[ RAS_MaxDeviceType + 1 ]; 17 CHAR szDeviceName[ RAS_MaxDeviceName + 1 ]; 129/33 #if (WINVER >= 0x401) CHAR szPhoneNumber[ RAS_MaxPhoneNumber + 1 ];129 #End If }
The RASCONNSTATUS structure can be three possible different sizes, being 288 bytes for NT4 enhanced and windows 2000, 160 bytes for windows 95/98 and NT4, and 64 bytes for NT3.51.
Our VB friendly structure could be defined as follows:
' VB "friendly" RASCONNSTATUS structure Type VBRASCONNSTATUS lRasConnState As RASCONNSTATE dwError As Long sDeviceType As String sDeviceName As String sNTPhoneNumber As String End Type
Note: the phone number of the connection is only returned on Windows NT4 enhanced and windows 2000.
To use this from VB, the following function wraps it all up, returning a long value that should be 0 if it succeeds, and filling a VBRASCONNSTATUS structure with the appropiate information. If the function fails (non zero return value), use the VBRasErrorHandler function to get a descriptive string
You can call this function from VB like so: (note: you need to already have the handle to an existing connection, hRasConn)
Dim rtn as long
Dim myConnStatus as VBRASCONNSTATUS
rtn = VBRasGetConnectStatus(hRasConn, myConnStatus)
If rtn <> 0 then MsgBox "failed"
Function VBRasGetConnectStatus _ (hRasConn As Long, _ udtVBRasConnStatus As VBRASCONNSTATUS) As Long Dim i As Long, dwSize As Long Dim aVarLens As Variant Dim b() As Byte aVarLens = Array(288&, 160&, 64&) For i = 0 To 2 dwSize = aVarLens(i) ReDim b(dwSize - 1) CopyMemory b(0), dwSize, 4 rtn = RasGetConnectStatus(hRasConn, b(0)) If rtn <> 632 Then Exit For Next i VBRasGetConnectStatus = rtn If rtn <> 0 Then Exit Function With udtVBRasConnStatus CopyMemory .lRasConnState, b(4), 4 CopyMemory .dwError, b(8), 4 CopyByteToTrimmedString .sDeviceType, b(12), 17& If dwSize = 64& Then CopyByteToTrimmedString .sDeviceName, b(29), 33& ElseIf dwSize = 160& Then CopyByteToTrimmedString .sDeviceName, b(29), 129& Else CopyByteToTrimmedString .sDeviceName, b(29), 129& CopyByteToTrimmedString .sNTPhoneNumber, b(158), 129& End If End With 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, RASCONNSTATE, RasDial, RasEnumConnections