Click here to Skip to main content
Click here to Skip to main content
Go to top

Detecting Windows/Workstation Locked / Unlocked in .NET

, 12 Apr 2006
Rate this:
Please Sign up or sign in to vote.
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

Share

About the Author

hitesh_tech
Web Developer
India India
No Biography provided

Comments and Discussions

 
QuestionTo read a remote workstation. PinmemberRoderick Mannino23-Apr-12 16:07 
QuestionPerfect in Win7 Pinmembersscully31425-Oct-11 3:17 
GeneralMy vote of 5 PinmemberHeaven20203-Nov-10 6:54 
Questioncan you please modify the same code for me to run in Access (VB6 code) Pinmembergold3422-Aug-10 19:18 
Answer.NET solution Pinmemberf3rland28-Oct-08 9:13 
Generalworks fine for me in xp sp2 Pinmembertodd_00115-May-08 14:42 
GeneralWorks fione for me in xp sp2 Pinmembermorphias0@yahoo.com16-Apr-08 2:37 
GeneralPROGRAMMING ASSISTANCE. PinmemberLaw Admin12-Apr-07 9:45 
GeneralIt doesn't works Pinmembernahuelgq11-Nov-06 8:31 
QuestionDoesnt work.. please help! PinmemberSebastianWalther5-Sep-06 6:13 
QuestionIs it possible to chack status of remote servers? Pinmembertordster16-Jun-06 1:49 
GeneralDoesn't detect Pinmembertlibert16-May-06 5:10 

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

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

| Advertise | Privacy | Mobile
Web04 | 2.8.140926.1 | Last Updated 12 Apr 2006
Article Copyright 2006 by hitesh_tech
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid