Calcula el tiempo que las conexiones RAS están activas

Crea un Nuevo proyecto y añade un control Timer y un ListView en el formulario.

En Timer1_Timer() se llama a la rutina CheckRASConnections… cuando inicias una conexión RAS la función detecta la nueva conexión y comienza a calcular el tiempo de la conexión. Cuando la conexión termina el contador de tiempo se para mostrando el tiempo total conectado.

Usa la función CheckConnections para saber cuando comienza una nueva conexión o cuando termina… llama a esta función en la rutina del Timer

Este código va en el Formulario.

Option Explicit
Private Sub Form_Load()
    Timer1.Interval = 1000
End Sub
Sub Timer1_Timer()
    Dim x As Integer

    x = CheckConnections
    If x > 0 Then
        MsgBox x & " New Connection Started"
    ElseIf x < 0 Then
        MsgBox Abs(x) & " Connection Terminated"
    End If
End Sub

Y este otro en un módulo.

Option Explicit
Private Declare Function RasEnumConnections Lib "RasApi32.dll" Alias "RasEnumConnectionsA" _
                            (lpRasConn As Any, lpcb As Long, lpcConnections As Long) As Long

Public Const RAS_MAXENTRYNAME As Integer = 256
Public Const RAS_MAXDEVICETYPE As Integer = 16
Public Const RAS_MAXDEVICENAME As Integer = 128
Public Const RAS_RASCONNSIZE As Integer = 412
Public Const ERROR_SUCCESS = 0

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 Function ByteToString(ByteArray() As Byte) As String
    ' Convierte el array de Byte devuelta por el API en un String
    Dim i As Integer

    ByteToString = ""
    i = 0
    Do While ByteArray(i)  0
        ByteToString = ByteToString & Chr(ByteArray(i))
        i = i + 1
    Loop
End Function
Private Function toTime(ByVal x As Single) As String
    ' Convierte un Single en String formato Horario
    toTime = Format(x Mod 60, "00")
    toTime = ":" & toTime
    x = x  60
    toTime = Format(x Mod 60, "00") & toTime
    toTime = ":" & toTime
    x = x  60
    toTime = x & toTime
End Function
Sub CheckRASConnections()
    ' Comprueba las conexiones RAS
    Dim i As Long
    Dim RasConn(255) As RasConn
    Dim structSize As Long
    Dim ConnectionsCount As Long
    Dim ret As Long
    Static LastTime As Single
    Dim ElapsedTime As Single

    If LastTime = 0 Then LastTime = Timer
    'Fills the RasConn structure with the data of all the opened RAS connections
    RasConn(0).dwSize = RAS_RASCONNSIZE
    structSize = RAS_MAXENTRYNAME * RasConn(0).dwSize
    ret = RasEnumConnections(RasConn(0), structSize, ConnectionsCount)
    ElapsedTime = Timer - LastTime
    If ElapsedTime < 0 Then ElapsedTime = 0
    'Each call to the Sub recalculate the elapsed time for all the active or new RAS connections
    If ret = ERROR_SUCCESS Then
        For i = 0 To ConnectionsCount - 1
            On Error GoTo NewConnection
            'Update an existing list item connection
            Form1.ListView1.ListItems("K" & RasConn(i).hRasConn).Tag = _
                Form1.ListView1.ListItems("K" & RasConn(i).hRasConn).Tag + ElapsedTime
            Form1.ListView1.ListItems("K" & RasConn(i).hRasConn).Text = _
                ByteToString(RasConn(i).szEntryName) & "-" & _
                toTime(Form1.ListView1.ListItems("K" & RasConn(i).hRasConn).Tag)
            GoTo NextConnection
NewConnection:
            'Create a new list item connection
            Form1.ListView1.ListItems.Add , "K" & RasConn(i).hRasConn, _
                ByteToString(RasConn(i).szEntryName)
            Form1.ListView1.ListItems("K" & RasConn(i).hRasConn).Tag = 0
NextConnection:
        Next
    End If
    LastTime = Timer
End Sub
Function CheckConnections() As Integer
    'Tells you if a Connection as been started or terminated
    'Returns the number of new connections, if the number is greater than
    'zero it indicates the number of new connections started, if the number
    'is negatice it indicates the number of connections terminated, zero if
    'the number of RAS connections is the same
    Static ConnCount As Integer
    Dim RasConn(255) As RasConn
    Dim structSize As Long
    Dim ConnectionsCount As Long
    Dim ret As Long

    'Fills the RasConn structure with the data of all the opened RAS connections
    RasConn(0).dwSize = RAS_RASCONNSIZE
    structSize = RAS_MAXENTRYNAME * RasConn(0).dwSize
    ret = RasEnumConnections(RasConn(0), structSize, ConnectionsCount)
    CheckConnections = ConnectionsCount - ConnCount
    ConnCount = ConnectionsCount
End Function

Enviado por Andrea Tincani

Anuncios

Responder

Introduce tus datos o haz clic en un icono para iniciar sesión:

Logo de WordPress.com

Estás comentando usando tu cuenta de WordPress.com. Cerrar sesión /  Cambiar )

Google photo

Estás comentando usando tu cuenta de Google. Cerrar sesión /  Cambiar )

Imagen de Twitter

Estás comentando usando tu cuenta de Twitter. Cerrar sesión /  Cambiar )

Foto de Facebook

Estás comentando usando tu cuenta de Facebook. Cerrar sesión /  Cambiar )

Conectando a %s