Click here to Skip to main content
Licence 
First Posted 12 Apr 2006
Views 35,988
Bookmarked 20 times

Detecting Windows/Workstation Locked / Unlocked in .NET

By | 12 Apr 2006 | Article
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

About the Author

hitesh_tech

Web Developer

India India

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
QuestionTo read a remote workstation. PinmemberRoderick Mannino16:07 23 Apr '12  
QuestionPerfect in Win7 Pinmembersscully3143:17 25 Oct '11  
GeneralMy vote of 5 PinmemberHeaven20206:54 3 Nov '10  
Questioncan you please modify the same code for me to run in Access (VB6 code) Pinmembergold3419:18 22 Aug '10  
Answer.NET solution PinPopularmemberf3rland9:13 28 Oct '08  
Generalworks fine for me in xp sp2 Pinmembertodd_00114:42 15 May '08  
GeneralWorks fione for me in xp sp2 Pinmembermorphias0@yahoo.com2:37 16 Apr '08  
GeneralPROGRAMMING ASSISTANCE. PinmemberLaw Admin9:45 12 Apr '07  
GeneralIt doesn't works Pinmembernahuelgq8:31 11 Nov '06  
QuestionDoesnt work.. please help! PinmemberSebastianWalther6:13 5 Sep '06  
QuestionIs it possible to chack status of remote servers? Pinmembertordster1:49 16 Jun '06  
GeneralDoesn't detect Pinmembertlibert5:10 16 May '06  

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.

Permalink | Advertise | Privacy | Mobile
Web04 | 2.5.120517.1 | Last Updated 12 Apr 2006
Article Copyright 2006 by hitesh_tech
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid