1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
Imports System.Timers 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 Dim MyTimer As New Timer() Public Sub New() MyTimer.Enabled = True MyTimer.Interval = 500 AddHandler MyTimer.Elapsed, New ElapsedEventHandler(AddressOf WorkStationISLocked) MyTimer.Start() End Sub #End Region #Region "WorkStationReader Events" Event locked(ByVal ivarreturn As Object) Event unlocked(ByVal ivarreturn As Object) #End Region Dim LastStateUnlocked As Boolean = True #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 If LastStateUnlocked Then RaiseEvent locked(ivarreturn) LastStateUnlocked = False End If 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 If LastStateUnlocked Then RaiseEvent locked(ivarreturn) LastStateUnlocked = False End If 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 If Not LastStateUnlocked Then RaiseEvent unlocked(ivarreturn) LastStateUnlocked = True End If GoTo CleanUpProc End If End If Exit Function CleanUpProc: p_lngHwnd = CloseDesktop(p_lngHwnd) End Function #End Region End Class Public Class Form1 WithEvents NewStation As New WorkStationReader() Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load End Sub Private Sub NewStation_locked(ivarreturn As Object) Handles NewStation.locked Debug.WriteLine("Locked - " & Now) End Sub Private Sub NewStation_unlocked(ivarreturn As Object) Handles NewStation.unlocked Debug.WriteLine("unLocked - " & Now) End Sub End Class |
C#
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
public class WorkStationReader { private const Int32 DESKTOP_CREATEMENU = 0x4; private const Int32 DESKTOP_CREATEWINDOW = 0x2; private const Int32 DESKTOP_ENUMERATE = 0x40; private const Int32 DESKTOP_HOOKCONTROL = 0x8; private const Int32 DESKTOP_READOBJECTS = 0x1; private const Int32 DESKTOP_SWITCHDESKTOP = 0x100; private const Int32 DESKTOP_WRITEOBJECTS = 0x80; private const Int32 GENERIC_WRITE = 0x40000000; private const Int32 HWND_BROADCAST = 0xFFFF; private const Int32 WM_HOTKEY = 0x312; private const Int32 MOD_ALT = 0x1; private const Int32 MOD_CONTROL = 0x2; private const Int32 VK_DELETE = 0x2E; private const Int32 UOI_NAME = 2; [System.Runtime.InteropServices.DllImport("user32")] private static extern Int32 OpenDesktop(string lpszDesktop, Int32 dwFlags, bool fInherit, Int32 dwDesiredAccess); [System.Runtime.InteropServices.DllImport("user32")] private static extern Int32 CloseDesktop(Int32 hDesktop); [System.Runtime.InteropServices.DllImport("user32")] private static extern Int32 SwitchDesktop(Int32 hDesktop); private Int32 p_lngHwnd; private Int32 p_lngRtn; private Int32 p_lngErr; private Int32 l_lkwkst; private Timer MyTimer = new Timer(); public WorkStationReader() { MyTimer.Enabled = true; MyTimer.Interval = 500; MyTimer.Elapsed += new ElapsedEventHandler(WorkStationISLocked); MyTimer.Start(); } public event lockedEventHandler locked; public delegate void lockedEventHandler(object ivarreturn); public event unlockedEventHandler unlocked; public delegate void unlockedEventHandler(object ivarreturn); private bool LastStateUnlocked = true; private void WorkStationISLocked(Object source, System.Timers.ElapsedEventArgs e) { object[] ivarreturn = new object[3]; p_lngHwnd = OpenDesktop("Default", 0, false, DESKTOP_SWITCHDESKTOP); if (p_lngHwnd == 0) { ivarreturn[0] = "Error with OpenDesktop: " + System.Runtime.InteropServices.Marshal.GetLastWin32Error(); ivarreturn[1] = false; if (LastStateUnlocked) { locked?.Invoke(ivarreturn); LastStateUnlocked = false; } return; } else { p_lngRtn = SwitchDesktop(hDesktop: p_lngHwnd); p_lngErr = System.Runtime.InteropServices.Marshal.GetLastWin32Error(); if (p_lngRtn == 0) { if (p_lngErr == 0) { // ivarreturn(0) = "Desktop is locked: " & Err.LastDllError ivarreturn[0] = "Locked : "; // & Err.LastDllError ivarreturn[1] = true; if (LastStateUnlocked) { locked?.Invoke(ivarreturn); LastStateUnlocked = false; } goto CleanUpProc; } else { ivarreturn[0] = "Error with SwitchDesktop: " + System.Runtime.InteropServices.Marshal.GetLastWin32Error(); ivarreturn[1] = false; goto CleanUpProc; } } else { // ivarreturn(0) = "Not locked!" ivarreturn[0] = "Unlocked : "; ivarreturn[1] = false; if (!LastStateUnlocked) { unlocked?.Invoke(ivarreturn); LastStateUnlocked = true; } goto CleanUpProc; } } return; CleanUpProc: ; p_lngHwnd = CloseDesktop(p_lngHwnd); return; } } public WorkStationReader MyStationLockWatcher = new WorkStationReader(); private void ThisAddIn_Startup(object sender, System.EventArgs e) { MyStationLockWatcher.locked += MyStationLockWatcher_locked; MyStationLockWatcher.unlocked += MyStationLockWatcher_unlocked; } private void MyStationLockWatcher_unlocked(object ivarreturn) { Debug.WriteLine("Unlocked"); } private void MyStationLockWatcher_locked(object ivarreturn) { Debug.WriteLine("Locked"); } |