Tips and tricks, news, links, downloads on Microsoft Access (original) (raw)

When TeamViewer running as service - connections established by System account, so we can't detect which user has TeamViewer session active. In that case we can use a following function, which looks through TeamViewer log file (logging must be enabled in TeamViewer options). Idea is based on that KB article Log file reading - Incoming connection

Public Function TeamViewerActive() As Boolean

Dim tvLog As String

tvLog = "C:\Program Files (x86)\TeamViewer\TeamViewer15_Logfile.log"

If Len(Dir(tvLog)) = 0 Then Exit Function

'Get user's process TeamViewer PID

Dim objServices As Object, objProcessSet As Object, Process As Object

Set objServices = GetObject("winmgmts:\\.\root\CIMV2")

Set objProcessSet = objServices.ExecQuery("SELECT * FROM Win32_Process WHERE Name = 'TeamViewer.exe'", , 48) '

Dim tvPID As Long

For Each Process In objProcessSet

Dim ProcessUserName As Variant

Call Process.GetOwner(ProcessUserName)

If ProcessUserName = GetWindowsUserName Then

tvPID = Process.ProcessID

Exit For

End If

Next

Set objProcessSet = Nothing

'no PID - no TeamViewer running

If tvPID = 0 Then Exit Function

Dim FSO As Object

Dim strLog As String, a, i As Long

Set FSO = CreateObject("scripting.FileSystemObject")

Dim TS As Object 'TextStream

Set TS = FSO.OpenTextFile(tvLog, 1) 'ForReading

strLog = TS.ReadAll

a = Split(strLog, vbNewLine)

TS.Close

'Get the lastest session

Dim lngLastStart As Long, strSessionID As String

For i = 0 To UBound(a)

If a(i) Like "* " & tvPID & " *" Then

If a(i) Like "* CParticipantManagerBase participant *" Then

lngLastStart = i

End If

End If

Next i

If lngLastStart > 0 Then

'Session was started

TeamViewerActive = True

'Get session ID

strSessionID = Mid(a(lngLastStart), InStr(1, a(lngLastStart), " (ID [") + 5)

strSessionID = Left(strSessionID, InStr(1, strSessionID, "]"))

'Check if that session already ended, if no - then user in TeamViewer session

For i = lngLastStart To UBound(a)

If a(i) Like "* CPersistentParticipantManager::RemoveParticipant:*" & Replace(strSessionID, "[", "[[]") & "*" Then

TeamViewerActive = False

End If

Next i

End If

End Function

Labels: Access, VB, VBA