It is currently Thu Oct 18, 2018 9:15 pm

All times are UTC + 1 hour




Post new topic Reply to topic  [ 6 posts ] 
Author Message
 Post subject: detect if windows session is locked
PostPosted: Tue Mar 09, 2010 8:29 pm 
Offline
User
User

Joined: Mon Aug 22, 2005 2:31 pm
Posts: 65
Hi,

How can I detect if a windows session is locked?

thanks!


Top
 Profile  
Reply with quote  
 Post subject: Re: detect if windows session is locked
PostPosted: Thu May 27, 2010 10:22 pm 
Offline
Addict
Addict

Joined: Sat Dec 31, 2005 5:24 pm
Posts: 2970
Location: Where ya would never look.....
A widnows session can indeed be detected for lock status. The WTSRegisterSessionNotification() API in wtsapi32.dll can be used. Take a look at http://msdn.microsoft.com/en-us/library/Aa383841 There is even visual basic 8 code which you should be able to convert to PureBasic.

Another more crude way is just for lock status for the desktop which is available via OpenInputDesktop() API in user32.dll, but if memory serves me correctly its very sensitive to account privilages. Basically and real quick from memory for OpenInputDesktop, it looks something like this:

Code:
Procedure MachineLocked()
lockstat.i

Lib_LockStat = LoadLibrary_("user32.dll")
*Func_OpenInputDesktop = GetProcAddress_(Lib_LockStat, "OpenInputDesktop")

hDesktopAccess.i = CallFunctionFast(*Func_OpenInputDesktop, dwFlags, fInherit, dwDesiredAccess, @lockstat)

FreeLibrary_(Lib_LockStat)

If lockstat < 1   
  ProcedureReturn #True
  Else
  ProcedureReturn #False
EndIf

EndProcedure


I've got something for WTSRegisterSessionNotification() too, somewhere and can't find it right now, but it was just the code from the MSDN link above converted anyway and its not hard to convert. Is this what you were lookin for? Hope it helps.

_________________
The advantage of a 64 bit operating system over a 32 bit operating system comes down to only being twice the headache.


Top
 Profile  
Reply with quote  
 Post subject: Re: detect if windows session is locked
PostPosted: Fri May 28, 2010 11:36 am 
Offline
User
User

Joined: Tue Apr 13, 2010 8:02 am
Posts: 54
I read in another thread that GetForegroundWindow_() returns 0 if the workstation is locked, and yep, works fine here with XP and 7.


Top
 Profile  
Reply with quote  
 Post subject: Re: detect if windows session is locked
PostPosted: Sat Feb 10, 2018 2:46 am 
Offline
Addict
Addict

Joined: Mon Feb 16, 2015 2:49 pm
Posts: 1566
SFSxOI wrote:
real quick from memory for OpenInputDesktop, it looks something like this [snip]

SFSxOI's code crashes on Win 7 (illegal memory access). Anyone know how to fix it, to detect when the PC is locked? Thanks!

Also, why does this code return a different handle to the desktop all the time, when there's only one desktop in use?

Code:
Repeat
  Debug OpenInputDesktop_(0,0,0)
  Sleep_(250)
ForEver


Top
 Profile  
Reply with quote  
 Post subject: Re: detect if windows session is locked
PostPosted: Sat Feb 10, 2018 4:40 am 
Offline
Addict
Addict

Joined: Sun Apr 12, 2009 6:27 am
Posts: 3095
Hi Dude
Code:
GetDesktopWindow_()                    ;Return Root Desktop(Main Desktop Handle) 
OpenDesktop_(@"Default", 0, #False, 0) ;Return Thread Desktop Handle
threadID = GetCurrentThreadId_()
GetThreadDesktop_(threadID)            ;Return Thread Desktop Handle



Try
Code:
Repeat
  thHwnd = OpenDesktop_(@"Default", 0, #False, #DESKTOP_SWITCHDESKTOP)
  If thHwnd
    SetThreadDesktop_(thHwnd)
    result = SwitchDesktop_(thHwnd)
    If result = 0
      Debug "Locked"
      CloseDesktop_(thHwnd)
    Else
      CloseDesktop_(thHwnd)
      Debug "Not Locked"
    EndIf
    Delay(500)
  EndIf
ForEver

_________________
Egypt my love


Top
 Profile  
Reply with quote  
 Post subject: Re: detect if windows session is locked
PostPosted: Sun Mar 11, 2018 12:40 pm 
Offline
Addict
Addict

Joined: Mon Feb 16, 2015 2:49 pm
Posts: 1566
Thank you, Rashad -- works great! 8)

Note: I had to remove SetThreadDesktop_(thHwnd) because it was making OpenWindow() and MessageRequesters() be shown on another (unseen) desktop, because I need to check the lock state every second (for a timer tool).

Second note: Your code also returns "locked" when a UAC prompt is open, but I guess there's no way to differentiate between that and a real PC lock?


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 6 posts ] 

All times are UTC + 1 hour


Who is online

Users browsing this forum: No registered users and 4 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  

 


Powered by phpBB © 2008 phpBB Group
subSilver+ theme by Canver Software, sponsor Sanal Modifiye