visual valid returns net method example documentacion description comment comentarios c# windows

valid - summary returns c#



¿Cómo puedo determinar mediante programación si mi estación de trabajo está bloqueada? (7)

A continuación se muestra el código de 100% de trabajo para encontrar si la PC está bloqueada o no.

Antes de usar esto, use el espacio de nombres System.Runtime.InteropServices .

[DllImport("user32", EntryPoint = "OpenDesktopA", CharSet = CharSet.Ansi,SetLastError = true, ExactSpelling = true)] private static extern Int32 OpenDesktop(string lpszDesktop, Int32 dwFlags, bool fInherit, Int32 dwDesiredAccess); [DllImport("user32", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)] private static extern Int32 CloseDesktop(Int32 hDesktop); [DllImport("user32", CharSet = CharSet.Ansi,SetLastError = true,ExactSpelling = true)] private static extern Int32 SwitchDesktop(Int32 hDesktop); public static bool IsWorkstationLocked() { const int DESKTOP_SWITCHDESKTOP = 256; int hwnd = -1; int rtn = -1; hwnd = OpenDesktop("Default", 0, false, DESKTOP_SWITCHDESKTOP); if (hwnd != 0) { rtn = SwitchDesktop(hwnd); if (rtn == 0) { // Locked CloseDesktop(hwnd); return true; } else { // Not locked CloseDesktop(hwnd); } } else { // Error: "Could not access the desktop..." } return false; }

Estoy escribiendo algunas herramientas de productividad / métricas para ayudarme a controlar mi enfoque durante todo el día. Recientemente, me he dado cuenta de que tiendo a perder el rumbo más de lo habitual y siento la necesidad de levantarme e ir a caminar / beber / etc. y me preocupa que esté "perdiendo" demasiado tiempo.

Como siempre bloqueo mi computadora cuando voy a algún lado, y la desbloqueo tan pronto como regrese (incluso si estoy leyendo en mi escritorio, etc.), me preguntaba cómo puedo determinar, en código, durante cuánto tiempo la máquina está bloqueado.

Estoy escribiendo esto en C # si eso ayuda, pero estoy abierto a otras ideas.

Me gusta la idea de servicio de Windows (y la he aceptado) por simplicidad y limpieza, pero desafortunadamente no creo que me funcione en este caso particular. Quería ejecutar esto en mi estación de trabajo en el trabajo en lugar de en casa (o además de la casa, supongo), pero está bloqueado por cortesía del Departamento de Defensa. Esa es en parte la razón por la que estoy haciendo lo mío, en realidad.

Lo escribiré de todos modos y veré si funciona. ¡Gracias a todos!


Crearía un servicio de Windows (un tipo de proyecto visual studio 2005) que maneja el evento OnSessionChange como se muestra a continuación:

protected override void OnSessionChange(SessionChangeDescription changeDescription) { if (changeDescription.Reason == SessionChangeReason.SessionLock) { //I left my desk } else if (changeDescription.Reason == SessionChangeReason.SessionUnlock) { //I returned to my desk } }

Qué y cómo registrar la actividad en ese punto depende de usted, pero un Servicio de Windows proporciona acceso rápido y fácil a eventos de Windows como inicio, apagado, inicio de sesión / salida, junto con los eventos de bloqueo y desbloqueo.


La solución a continuación usa la API de Win32. Se llama a OnSessionLock cuando la estación de trabajo está bloqueada, y se llama a OnSessionUnlock cuando está desbloqueado.

[DllImport("wtsapi32.dll")] private static extern bool WTSRegisterSessionNotification(IntPtr hWnd, int dwFlags); [DllImport("wtsapi32.dll")] private static extern bool WTSUnRegisterSessionNotification(IntPtr hWnd); private const int NotifyForThisSession = 0; // This session only private const int SessionChangeMessage = 0x02B1; private const int SessionLockParam = 0x7; private const int SessionUnlockParam = 0x8; protected override void WndProc(ref Message m) { // check for session change notifications if (m.Msg == SessionChangeMessage) { if (m.WParam.ToInt32() == SessionLockParam) OnSessionLock(); // Do something when locked else if (m.WParam.ToInt32() == SessionUnlockParam) OnSessionUnlock(); // Do something when unlocked } base.WndProc(ref m); return; } void OnSessionLock() { Debug.WriteLine("Locked..."); } void OnSessionUnlock() { Debug.WriteLine("Unlocked..."); } private void Form1Load(object sender, EventArgs e) { WTSRegisterSessionNotification(this.Handle, NotifyForThisSession); } // and then when we are done, we should unregister for the notification // WTSUnRegisterSessionNotification(this.Handle);


No había encontrado esto antes, pero desde cualquier aplicación puede conectar un SessionSwitchEventHandler. Obviamente, su aplicación deberá estar ejecutándose, pero siempre que sea:

Microsoft.Win32.SystemEvents.SessionSwitch += new Microsoft.Win32.SessionSwitchEventHandler(SystemEvents_SessionSwitch); void SystemEvents_SessionSwitch(object sender, Microsoft.Win32.SessionSwitchEventArgs e) { if (e.Reason == SessionSwitchReason.SessionLock) { //I left my desk } else if (e.Reason == SessionSwitchReason.SessionUnlock) { //I returned to my desk } }


Sé que esta es una vieja pregunta, pero he encontrado un método para obtener el estado de bloqueo para una sesión determinada.

Encontré mi respuesta here pero estaba en C ++, así que traduje todo lo que pude a C # para obtener el estado de bloqueo.

Así que aquí va:

static class SessionInfo { private const Int32 FALSE = 0; private static readonly IntPtr WTS_CURRENT_SERVER = IntPtr.Zero; private const Int32 WTS_SESSIONSTATE_LOCK = 0; private const Int32 WTS_SESSIONSTATE_UNLOCK = 1; private static bool _is_win7 = false; static SessionInfo() { var os_version = Environment.OSVersion; _is_win7 = (os_version.Platform == PlatformID.Win32NT && os_version.Version.Major == 6 && os_version.Version.Minor == 1); } [DllImport("wtsapi32.dll")] private static extern Int32 WTSQuerySessionInformation( IntPtr hServer, [MarshalAs(UnmanagedType.U4)] UInt32 SessionId, [MarshalAs(UnmanagedType.U4)] WTS_INFO_CLASS WTSInfoClass, out IntPtr ppBuffer, [MarshalAs(UnmanagedType.U4)] out UInt32 pBytesReturned ); [DllImport("wtsapi32.dll")] private static extern void WTSFreeMemoryEx( WTS_TYPE_CLASS WTSTypeClass, IntPtr pMemory, UInt32 NumberOfEntries ); private enum WTS_INFO_CLASS { WTSInitialProgram = 0, WTSApplicationName = 1, WTSWorkingDirectory = 2, WTSOEMId = 3, WTSSessionId = 4, WTSUserName = 5, WTSWinStationName = 6, WTSDomainName = 7, WTSConnectState = 8, WTSClientBuildNumber = 9, WTSClientName = 10, WTSClientDirectory = 11, WTSClientProductId = 12, WTSClientHardwareId = 13, WTSClientAddress = 14, WTSClientDisplay = 15, WTSClientProtocolType = 16, WTSIdleTime = 17, WTSLogonTime = 18, WTSIncomingBytes = 19, WTSOutgoingBytes = 20, WTSIncomingFrames = 21, WTSOutgoingFrames = 22, WTSClientInfo = 23, WTSSessionInfo = 24, WTSSessionInfoEx = 25, WTSConfigInfo = 26, WTSValidationInfo = 27, WTSSessionAddressV4 = 28, WTSIsRemoteSession = 29 } private enum WTS_TYPE_CLASS { WTSTypeProcessInfoLevel0, WTSTypeProcessInfoLevel1, WTSTypeSessionInfoLevel1 } public enum WTS_CONNECTSTATE_CLASS { WTSActive, WTSConnected, WTSConnectQuery, WTSShadow, WTSDisconnected, WTSIdle, WTSListen, WTSReset, WTSDown, WTSInit } public enum LockState { Unknown, Locked, Unlocked } [StructLayout(LayoutKind.Sequential)] private struct WTSINFOEX { public UInt32 Level; public UInt32 Reserved; /* I have observed the Data field is pushed down by 4 bytes so i have added this field as padding. */ public WTSINFOEX_LEVEL Data; } [StructLayout(LayoutKind.Sequential)] private struct WTSINFOEX_LEVEL { public WTSINFOEX_LEVEL1 WTSInfoExLevel1; } [StructLayout(LayoutKind.Sequential)] private struct WTSINFOEX_LEVEL1 { public UInt32 SessionId; public WTS_CONNECTSTATE_CLASS SessionState; public Int32 SessionFlags; /* I can''t figure out what the rest of the struct should look like but as i don''t need anything past the SessionFlags i''m not going to. */ } public static LockState GetSessionLockState(UInt32 session_id) { IntPtr ppBuffer; UInt32 pBytesReturned; Int32 result = WTSQuerySessionInformation( WTS_CURRENT_SERVER, session_id, WTS_INFO_CLASS.WTSSessionInfoEx, out ppBuffer, out pBytesReturned ); if (result == FALSE) return LockState.Unknown; var session_info_ex = Marshal.PtrToStructure<WTSINFOEX>(ppBuffer); if (session_info_ex.Level != 1) return LockState.Unknown; var lock_state = session_info_ex.Data.WTSInfoExLevel1.SessionFlags; WTSFreeMemoryEx(WTS_TYPE_CLASS.WTSTypeSessionInfoLevel1, ppBuffer, pBytesReturned); if (_is_win7) { /* Ref: https://msdn.microsoft.com/en-us/library/windows/desktop/ee621019(v=vs.85).aspx * Windows Server 2008 R2 and Windows 7: Due to a code defect, the usage of the WTS_SESSIONSTATE_LOCK * and WTS_SESSIONSTATE_UNLOCK flags is reversed. That is, WTS_SESSIONSTATE_LOCK indicates that the * session is unlocked, and WTS_SESSIONSTATE_UNLOCK indicates the session is locked. * */ switch (lock_state) { case WTS_SESSIONSTATE_LOCK: return LockState.Unlocked; case WTS_SESSIONSTATE_UNLOCK: return LockState.Locked; default: return LockState.Unknown; } } else { switch (lock_state) { case WTS_SESSIONSTATE_LOCK: return LockState.Locked; case WTS_SESSIONSTATE_UNLOCK: return LockState.Unlocked; default: return LockState.Unknown; } } } }

Nota: El código anterior fue extraído de un proyecto mucho más grande, así que si perdí un poco, lo siento. No tengo tiempo para probar el código anterior pero planeo volver en una semana o dos para verificar todo. Solo lo publiqué ahora porque no quería olvidarlo.


Si está interesado en escribir un servicio de Windows para "encontrar" estos eventos, Topshelf (la biblioteca / marco que hace que escribir servicios de Windows sea mucho más fácil) tiene un gancho.

public interface IMyServiceContract { void Start(); void Stop(); void SessionChanged(Topshelf.SessionChangedArguments args); } public class MyService : IMyServiceContract { public void Start() { } public void Stop() { } public void SessionChanged(SessionChangedArguments e) { Console.WriteLine(e.ReasonCode); } }

y ahora el código para conectar el servicio de la parte superior a la interfaz / concreto arriba

Todo lo que se muestra a continuación es la configuración "típica" del topshelf ... a excepción de 2 líneas que marqué como

/ * ESTA ES LA LÍNEA MÁGICA * /

Esos son los que hacen que el método SessionChanged se active.

Probé esto con Windows 10 x64. Cerré y abrí mi máquina y obtuve el resultado deseado.

IMyServiceContract myServiceObject = new MyService(); /* container.Resolve<IMyServiceContract>(); */ HostFactory.Run(x => { x.Service<IMyServiceContract>(s => { s.ConstructUsing(name => myServiceObject); s.WhenStarted(sw => sw.Start()); s.WhenStopped(sw => sw.Stop()); s.WhenSessionChanged((csm, hc, chg) => csm.SessionChanged(chg)); /* THIS IS MAGIC LINE */ }); x.EnableSessionChanged(); /* THIS IS MAGIC LINE */ /* use command line variables for the below commented out properties */ /* x.RunAsLocalService(); x.SetDescription("My Description"); x.SetDisplayName("My Display Name"); x.SetServiceName("My Service Name"); x.SetInstanceName("My Instance"); */ x.StartManually(); // Start the service manually. This allows the identity to be tweaked before the service actually starts /* the below map to the "Recover" tab on the properties of the Windows Service in Control Panel */ x.EnableServiceRecovery(r => { r.OnCrashOnly(); r.RestartService(1); ////first r.RestartService(1); ////second r.RestartService(1); ////subsequents r.SetResetPeriod(0); }); x.DependsOnEventLog(); // Windows Event Log x.UseLog4Net(); x.EnableShutdown(); x.OnException(ex => { /* Log the exception */ /* not seen, I have a log4net logger here */ }); });

Mis paquetes.config para proporcionar sugerencias sobre las versiones:

<package id="log4net" version="2.0.5" targetFramework="net45" /> <package id="Topshelf" version="4.0.3" targetFramework="net461" /> <package id="Topshelf.Log4Net" version="4.0.3" targetFramework="net461" />


NOTA : Esta no es una respuesta, sino una (contribución) a la respuesta de , porque mi reputación no me permite comentar hasta ahora.

En caso de que alguien haya intentado con el código de la respuesta de Timothy Carter y no haya funcionado de inmediato en un servicio de Windows, hay una propiedad que debe establecerse como true en el constructor del servicio. Solo agregue la línea en el constructor:

CanHandleSessionChangeEvent = true;

Y asegúrese de no establecer esta propiedad después de que se haya iniciado el servicio, de lo contrario se InvalidOperationException una InvalidOperationException .