detect if windows session is locked
Posted: Tue Mar 09, 2010 8:29 pm
Hi,
How can I detect if a windows session is locked?
thanks!
How can I detect if a windows session is locked?
thanks!
http://www.purebasic.com
https://www.purebasic.fr/english/
Code: Select all
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
SFSxOI's code crashes on Win 7 (illegal memory access). Anyone know how to fix it, to detect when the PC is locked? Thanks!SFSxOI wrote:real quick from memory for OpenInputDesktop, it looks something like this [snip]
Code: Select all
Repeat
Debug OpenInputDesktop_(0,0,0)
Sleep_(250)
ForEver
Code: Select all
GetDesktopWindow_() ;Return Root Desktop(Main Desktop Handle)
OpenDesktop_(@"Default", 0, #False, 0) ;Return Thread Desktop Handle
threadID = GetCurrentThreadId_()
GetThreadDesktop_(threadID) ;Return Thread Desktop Handle
Code: Select all
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
Code: Select all
Macro IsLocked()
Bool(GetForegroundWindow_() = #Null)
EndMacro
Code: Select all
EnableExplicit
Procedure.i IsLocked()
Protected handle.i
Protected *ProcessIdToSessionId
Protected *WTSQuerySessionInformation
Protected *WTSFreeMemory
Protected sid.i
Protected bytes.i
handle = LoadLibrary_("Wtsapi32.dll")
*ProcessIdToSessionId = GetProcAddress_(GetModuleHandle_("kernel32.dll"),?ProcessIdToSessionId)
*WTSQuerySessionInformation = GetProcAddress_(handle,?WTSQuerySessionInformation)
*WTSFreeMemory = GetProcAddress_(handle,?WTSFreeMemory)
If *ProcessIdToSessionId And *WTSQuerySessionInformation And *WTSFreeMemory
If CallFunctionFast(*ProcessIdToSessionId,GetCurrentProcessId_(),@sid)
If CallFunctionFast(*WTSQuerySessionInformation,#Null,sid,25,@handle,@bytes)
bytes = PeekL(handle + $10)
CallFunctionFast(*WTSFreeMemory,handle)
If OSVersion() = #PB_OS_Windows_7 Or OSVersion() = #PB_OS_Windows_Server_2008_R2
ProcedureReturn bytes
Else
ProcedureReturn Bool(bytes = #False)
EndIf
EndIf
EndIf
Else
handle = OpenDesktop_("Default",#Null,#False,#DESKTOP_SWITCHDESKTOP)
If handle
bytes = SwitchDesktop_(handle)
CloseDesktop_(handle)
ProcedureReturn Bool(bytes = #Null)
EndIf
EndIf
ProcedureReturn #False
ProcessIdToSessionId:
!db 'ProcessIdToSessionId',0x0
WTSQuerySessionInformation:
!db 'WTSQuerySessionInformationW',0x0
WTSFreeMemory:
!db 'WTSFreeMemory',0x0
EndProcedure
Debug IsLocked()
End
Code: Select all
CompilerIf Defined(PB_Compiler_Backend,#PB_Constant)
CompilerIf #PB_Compiler_Backend=#PB_Backend_C
DataSection
ProcessIdToSessionId:
Data.b 0
WTSQuerySessionInformation:
Data.b 0
WTSFreeMemory:
Data.b 0
EndDataSection
CompilerEndIf
CompilerEndIf
Code: Select all
DataSection
ProcessIdToSessionId:
Data.a 'P','r','o','c','e','s','s','I','d','T','o','S','e','s','s','i','o','n','I','d',0
WTSQuerySessionInformation:
Data.a 'W','T','S','Q','u','e','r','y','S','e','s','s','i','o','n','I','n','f','o','r','m','a','t','i','o','n','W',0
WTSFreeMemory:
Data.a 'W','T','S','F','r','e','e','M','e','m','o','r','y',0
EndDataSection