Click here to Skip to main content
15,880,469 members
Articles / Programming Languages / Visual Basic
Article

Detecting Windows/Workstation Locked / Unlocked in .NET

Rate me:
Please Sign up or sign in to vote.
2.30/5 (19 votes)
12 Apr 20062 min read 78K   2K   23   12
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

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionTo read a remote workstation. Pin
Rod Mannino23-Apr-12 16:07
Rod Mannino23-Apr-12 16:07 
QuestionPerfect in Win7 Pin
sscully31425-Oct-11 3:17
sscully31425-Oct-11 3:17 
GeneralMy vote of 5 Pin
Global Analyser3-Nov-10 6:54
Global Analyser3-Nov-10 6:54 
Questioncan you please modify the same code for me to run in Access (VB6 code) Pin
gold3422-Aug-10 19:18
gold3422-Aug-10 19:18 
Answer.NET solution PinPopular
f3rland28-Oct-08 9:13
f3rland28-Oct-08 9:13 
Generalworks fine for me in xp sp2 Pin
todd.0101110115-May-08 14:42
todd.0101110115-May-08 14:42 
GeneralWorks fione for me in xp sp2 Pin
morphias0@yahoo.com16-Apr-08 2:37
morphias0@yahoo.com16-Apr-08 2:37 
GeneralPROGRAMMING ASSISTANCE. Pin
Law Admin12-Apr-07 9:45
Law Admin12-Apr-07 9:45 
GeneralIt doesn't works Pin
nahuelgq11-Nov-06 8:31
nahuelgq11-Nov-06 8:31 
QuestionDoesnt work.. please help! Pin
SebastianWalther5-Sep-06 6:13
SebastianWalther5-Sep-06 6:13 
QuestionIs it possible to chack status of remote servers? Pin
tordster16-Jun-06 1:49
tordster16-Jun-06 1:49 
GeneralDoesn't detect Pin
tlibert16-May-06 5:10
tlibert16-May-06 5:10 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.