65.9K
CodeProject is changing. Read more.
Home

Detecting Windows/Workstation Locked / Unlocked in .NET

starIconstarIcon
emptyStarIcon
starIcon
emptyStarIconemptyStarIcon

2.30/5 (19 votes)

Apr 12, 2006

2 min read

viewsIcon

79507

downloadIcon

2052

This Code Detects Windows/Workstation Locked / Unlocked in .NET

Sample Image - DetectWindowslockunlock.jpg

Introduction

This Code Shows How to detect Windows / Workstation locked & unlocked in .NET

Public Class WorkStationReader

#Region "API Calls"

Private Const DESKTOP_CREATEMENU As Int32 = &H4&

Private Const DESKTOP_CREATEWINDOW As Int32 = &H2&

Private Const DESKTOP_ENUMERATE As Int32 = &H40&

Private Const DESKTOP_HOOKCONTROL As Int32 = &H8&

Private Const DESKTOP_READOBJECTS As Int32 = &H1&

Private Const DESKTOP_SWITCHDESKTOP As Int32 = &H100&

Private Const DESKTOP_WRITEOBJECTS As Int32 = &H80&

Private Const GENERIC_WRITE As Int32 = &H40000000

Private Const HWND_BROADCAST As Int32 = &HFFFF&

Private Const WM_HOTKEY As Int32 = &H312

Private Const MOD_ALT As Int32 = &H1

Private Const MOD_CONTROL As Int32 = &H2

Private Const VK_DELETE As Int32 = &H2E

Private Const UOI_NAME As Int32 = 2

Private Declare Function OpenDesktop Lib "user32" Alias "OpenDesktopA" (ByVal lpszDesktop As String, ByVal dwFlags As Int32, ByVal fInherit As Boolean, ByVal dwDesiredAccess As Int32) As Int32

Private Declare Function CloseDesktop Lib "user32" (ByVal hDesktop As Int32) As Int32

Private Declare Function SwitchDesktop Lib "user32" (ByVal hDesktop As Int32) As Int32

#End Region

#Region "WorkStationReader Global Variables"

Dim p_lngHwnd As Int32

Dim p_lngRtn As Int32

Dim p_lngErr As Int32

Dim l_lkwkst As Int32

#End Region

#Region "WorkStationReader Events"

Event locked(ByVal ivarreturn As Object)

#End Region

#Region "WorkStationReader Functions"

Function WorkStationISLocked() As Object

Dim ivarreturn(2) As Object

p_lngHwnd = OpenDesktop("Default", 0, False, DESKTOP_SWITCHDESKTOP)

If p_lngHwnd = 0 Then

ivarreturn(0) = "Error with OpenDesktop: " & Err.LastDllError

ivarreturn(1) = False

WorkStationISLocked = ivarreturn

RaiseEvent locked(ivarreturn)

Exit Function

Else

p_lngRtn = SwitchDesktop(hDesktop:=p_lngHwnd)

p_lngErr = Err.LastDllError

If p_lngRtn = 0 Then

If p_lngErr = 0 Then

'ivarreturn(0) = "Desktop is locked: " & Err.LastDllError

ivarreturn(0) = "Locked : " '& Err.LastDllError

ivarreturn(1) = True

WorkStationISLocked = ivarreturn

RaiseEvent locked(ivarreturn)

GoTo CleanUpProc

Else

ivarreturn(0) = "Error with SwitchDesktop: " & Err.LastDllError

ivarreturn(1) = False

WorkStationISLocked = ivarreturn

GoTo CleanUpProc

End If

Else

'ivarreturn(0) = "Not locked!"

ivarreturn(0) = "Unlocked : "

ivarreturn(1) = False

WorkStationISLocked = ivarreturn

RaiseEvent locked(ivarreturn)

GoTo CleanUpProc

End If

End If

Exit Function

CleanUpProc:

p_lngHwnd = CloseDesktop(p_lngHwnd)

End Function

#End Region

End Class