Option Explicit
Public Const DESKTOP_CREATEMENU = &H4&
Public Const DESKTOP_CREATEWINDOW = &H2&
Public Const DESKTOP_ENUMERATE = &H40&
Public Const DESKTOP_HOOKCONTROL = &H8&
Public Const DESKTOP_JOURNALPLAYBACK = &H20&
Public Const DESKTOP_JOURNALRECORD = &H10&
Public Const DESKTOP_READOBJECTS = &H1&
Public Const DESKTOP_WRITEOBJECTS = &H80&
Public Const DESKTOP_SWITCHDESKTOP = &H100&
Public Const SPI_SCREENSAVERRUNNING = 114
Public Declare Function SystemParametersInfoSS _
Lib "user32" Alias "SystemParametersInfoA" _
(ByVal uAction As Long, ByVal uParam As Long, _
ByRef lpvParam As Any, ByVal fuWinIni As Long) As Long
Public Declare Function SwitchDesktop _
Lib "user32" _
(ByVal hDesktop As Long) As Boolean
Public Declare Function OpenDesktop _
Lib "user32" Alias "OpenDesktopA" _
(ByVal lpszDesktop As String, _
ByVal dwFlags As Long, _
ByVal fInherit As Boolean, _
ByVal dwDesiredAccess As Long) As Long
Public Function SSRunning() As Boolean
Dim lRetVal As Long
Dim lRunning As Long
On Error Resume Next
lRetVal = SystemParametersInfoSS(SPI_SCREENSAVERRUNNING, 0&, lRunning, 0&)
lRetVal = SystemParametersInfoSS(SPI_SCREENSAVERRUNNING, lRunning, 0&, 0&)
If lRunning <> 0 Then
SSRunning = True
End If
End Function
Public Function DesktopLocked() As Boolean
Dim lngHWnd As Long
Dim lngRtn As Long
Dim lngErr As Long
DesktopLocked = False
lngHWnd = OpenDesktop("Default", 0, False, DESKTOP_SWITCHDESKTOP)
If lngHWnd = 0 Then
Debug.Print "Error with OpenDesktop: " & Err.LastDllError
Else
lngRtn = SwitchDesktop(hDesktop:=lngHWnd)
lngErr = Err.LastDllError
If lngRtn = 0 Then
If lngErr = 0 Then
Debug.Print "Desktop is locked: "
DesktopLocked = True
Else
Debug.Print "Error with SwitchDesktop: " & Err.LastDllError
End If
End If
End If
End Function