G
Guest
I am trying to capture an event when the screensaver starts. I have code
working on Windows XP etc when the Password/Welcome Screen is enabled
[Workstation is Locked]. Using the Fast User Switching Notifications.
However I am unable to detect the screensaver starting when the screensaver
password option is disabled. When using the WndProc Overrides I am unable to
capture the screensaver event unless the application is active [Our software
often sits in the Windows system tray].
Thank you in advance for any help that is given. I am attempting to use a
Hook to capture the WM_GETMESSAGE Hook using VB.NET my code is as follows:
'--------------------------------------------------------
Imports System.Windows.Forms
Imports System.Runtime.InteropServices
Public Class Screensaver
Dim hp As New HookProc(AddressOf KeyboardProc)
Public Const WH_GETMESSAGE = 3
Public Const HC_ACTION = 0
Private m_ScreensaverHook As IntPtr
Private Declare Auto Function UnhookWindowsHookEx Lib "user32" Alias
"UnhookWindowsHookEx" (ByVal hHook As IntPtr) As Integer
Private Declare Auto Function SetWindowsHookEx Lib "user32" (ByVal
idHook As Integer, ByVal lpfn As HookProc, ByVal hMod As Long, ByVal
dwThreadId As Integer) As IntPtr
Private Declare Auto Function CallNextHookEx Lib "user32" (ByVal curHook
As IntPtr, ByVal nCode As Integer, ByVal wParam As Integer, ByVal lParam As
Integer)
Private Delegate Function HookProc(ByVal code As Integer, ByVal wParam
As Integer, ByVal lParam As IntPtr) As IntPtr
' Events
Public Event Screensaver(ByVal m As Message)
Public Sub SetHook()
Dim hInstance As Long =
System.Runtime.InteropServices.Marshal.GetHINSTANCE(System.Reflection.Assembly.GetExecutingAssembly.GetModules()(0)).ToInt32()
Dim hThreadId As Integer = AppDomain.GetCurrentThreadId()
'Dim hThreadId As Integer = 0
m_ScreensaverHook = SetWindowsHookEx(WH_GETMESSAGE, hp, hInstance,
hThreadId)
End Sub
Public Sub Unhook()
If m_ScreensaverHook.ToInt32 <> 0 Then
UnhookWindowsHookEx(m_ScreensaverHook)
End Sub
Private Function KeyboardProc(ByVal nCode As Integer, ByVal wParam As
Integer, ByVal lParam As IntPtr) As IntPtr
If (nCode < HC_ACTION) Then
Return CallNextHookEx(m_ScreensaverHook, nCode, wParam,
lParam.ToInt32)
ElseIf (nCode = HC_ACTION) Then
Dim m As Message
m = CType(Marshal.PtrToStructure(lParam, GetType(Message)),
Message)
Debug.WriteLine("Screensaver")
RaiseEvent Screensaver(m)
End If
End Function
End Class
working on Windows XP etc when the Password/Welcome Screen is enabled
[Workstation is Locked]. Using the Fast User Switching Notifications.
However I am unable to detect the screensaver starting when the screensaver
password option is disabled. When using the WndProc Overrides I am unable to
capture the screensaver event unless the application is active [Our software
often sits in the Windows system tray].
Thank you in advance for any help that is given. I am attempting to use a
Hook to capture the WM_GETMESSAGE Hook using VB.NET my code is as follows:
'--------------------------------------------------------
Imports System.Windows.Forms
Imports System.Runtime.InteropServices
Public Class Screensaver
Dim hp As New HookProc(AddressOf KeyboardProc)
Public Const WH_GETMESSAGE = 3
Public Const HC_ACTION = 0
Private m_ScreensaverHook As IntPtr
Private Declare Auto Function UnhookWindowsHookEx Lib "user32" Alias
"UnhookWindowsHookEx" (ByVal hHook As IntPtr) As Integer
Private Declare Auto Function SetWindowsHookEx Lib "user32" (ByVal
idHook As Integer, ByVal lpfn As HookProc, ByVal hMod As Long, ByVal
dwThreadId As Integer) As IntPtr
Private Declare Auto Function CallNextHookEx Lib "user32" (ByVal curHook
As IntPtr, ByVal nCode As Integer, ByVal wParam As Integer, ByVal lParam As
Integer)
Private Delegate Function HookProc(ByVal code As Integer, ByVal wParam
As Integer, ByVal lParam As IntPtr) As IntPtr
' Events
Public Event Screensaver(ByVal m As Message)
Public Sub SetHook()
Dim hInstance As Long =
System.Runtime.InteropServices.Marshal.GetHINSTANCE(System.Reflection.Assembly.GetExecutingAssembly.GetModules()(0)).ToInt32()
Dim hThreadId As Integer = AppDomain.GetCurrentThreadId()
'Dim hThreadId As Integer = 0
m_ScreensaverHook = SetWindowsHookEx(WH_GETMESSAGE, hp, hInstance,
hThreadId)
End Sub
Public Sub Unhook()
If m_ScreensaverHook.ToInt32 <> 0 Then
UnhookWindowsHookEx(m_ScreensaverHook)
End Sub
Private Function KeyboardProc(ByVal nCode As Integer, ByVal wParam As
Integer, ByVal lParam As IntPtr) As IntPtr
If (nCode < HC_ACTION) Then
Return CallNextHookEx(m_ScreensaverHook, nCode, wParam,
lParam.ToInt32)
ElseIf (nCode = HC_ACTION) Then
Dim m As Message
m = CType(Marshal.PtrToStructure(lParam, GetType(Message)),
Message)
Debug.WriteLine("Screensaver")
RaiseEvent Screensaver(m)
End If
End Function
End Class