Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Detect if VPN Active?

Status
Not open for further replies.

jebenson

Technical User
Feb 4, 2002
2,956
US
Does anybody know of a way to detect if a VPN is active on a particular PC. I have an app that I would like to run on users' laptops when they connect to the LAN via VPN, but not run whan they connect to the LAN normally.

If its of any importance, users connect via broadband (DSL or cable), not dialup.

Thanks

I used to rock and roll every night and party every day. Then it was every other day. Now I'm lucky if I can find 30 minutes a week in which to get funky. - Homer Simpson
 
This works only for the MS vpn connections, doesn't seem to work for the cisco ones. And aoart from that; it does not check a certain vpn, just if any vpn is active:


Code:
Public Const RAS_MaxEntryName As Long = 256
Public Const RAS_MaxDeviceName As Long = 128
Public Const RAS_MaxDeviceType As Long = 16
Public Const RASCS_Connected As Long = &H2000

Public Type RASCONN
    dwSize As Long
    hrasconn As Long
    szEntryname(RAS_MaxEntryName) As Byte
    szDeviceType(RAS_MaxDeviceType) As Byte
    szDeviceName(RAS_MaxDeviceName) As Byte
End Type

Public Type RASCONNSTATUS
    dwSize As Long
    rasconnstate As Long
    dwError As Long
    szDeviceType(RAS_MaxDeviceType) As Byte
    szDeviceName(RAS_MaxDeviceName) As Byte
End Type

Public Declare Function RasEnumConnections Lib "rasapi32.dll" Alias "RasEnumConnectionsA" (ByRef lprasconna As RASCONN, ByRef lpdword As Long, ByRef lpdword As Long) As Long
Public Declare Function RasGetConnectStatus Lib "rasapi32.dll" Alias "RasGetConnectStatusA" (ByVal hrasconn As Long, ByRef lprasconnstatusa As RASCONNSTATUS) As Long

Public Function VPNConnectionActive() As Boolean
'//---------------------------------------------------------------------------------------
'// Procedure : VPNConnectionActive
'// Datum/Tijd: 30-06-2004 / 15:09
'// Auteur    : Rick Langevoort
'// Doel      : Controleren of er een actieve VPN connectie is.
'// Revisie   :
'//---------------------------------------------------------------------------------------
    Dim lngConns As Long, lngConn As Long
    Dim aRAS(255) As RASCONN, rasState As RASCONNSTATUS
    
    On Error GoTo ErrHandler

    aRAS(0).dwSize = 412
    If RasEnumConnections(aRAS(0), 421888, lngConns) = 0 Then
        '//Check to see if there are any VPN connections in those:
        For lngConn = 0 To lngConns - 1
            If StrComp(Left(StrConv(aRAS(lngConn).szDeviceType, vbUnicode), InStr(StrConv(aRAS(lngConn).szDeviceType, vbUnicode), Chr(0)) - 1), "vpn", vbTextCompare) = 0 Then
                '//If so, check if one of them is connected:
                rasState.dwSize = 160
                
                If RasGetConnectStatus(aRAS(lngConn).hrasconn, rasState) = 0 Then
                    If rasState.rasconnstate = RASCS_Connected Then Exit For
                End If
            End If
        Next lngConn
    End If
    
    VPNConnectionActive = lngConn < lngConns
    
    Exit Function

ErrHandler:
    MsgBox "Error in libmain.VPNConnectionActive: " & vbCrLf & vbCrLf & err.Description & " (" & CStr(err.Number) & ")", vbCritical, MSG_HEADER
End Function

Greetings,
Rick
 
LazyMe - thanks for your response. I'll check it out and let you know how it works for me.

-JEB

I used to rock and roll every night and party every day. Then it was every other day. Now I'm lucky if I can find 30 minutes a week in which to get funky. - Homer Simpson
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top