2009-03-02 11 views
7

DUPLICATO:How can I programmatically determine if my workstation is locked?Controllo per workstation cambiare blocco/sblocco con C#

Come posso rilevare (durante il runtime) quando un utente di Windows ha bloccato loro schermo (Windows + L) e sbloccato di nuovo. So che potrei monitorare globalmente l'input da tastiera, ma è possibile controllare tale cosa con le variabili di ambiente?

+0

Le risposte a questa domanda potrebbero fornire un punto di partenza per voi. http://stackoverflow.com/questions/44980 –

+0

Grazie a tutti. Sei stato molto utile come sempre :) –

risposta

3

È possibile ottenere questa notifica tramite un messaggio WM_WTSSESSION_CHANGE. È necessario notificare a Windows che si desidera ricevere questi messaggi tramite WTSRegisterSessionNotification e annullare la registrazione con WTSUnRegisterSessionNotification.

Questi post devono essere utili per un'implementazione C#.

http://pinvoke.net/default.aspx/wtsapi32.WTSRegisterSessionNotification

http://blogs.msdn.com/shawnfa/archive/2005/05/17/418891.aspx

http://bytes.com/groups/net-c/276963-trapping-when-workstation-locked

+0

Mi hai battuto di pochi secondi :) – shahkalpesh

+0

-1: La versione di codice gestito di questo è stata pubblicata prima che la versione Win32 fosse ... perché è stata contrassegnata come risposta corretta? – Powerlord

+0

Difficilmente una ragione per un downvote tho, eh? –

-2

che assolutamente non c'è bisogno WM_WTSSESSION_CHANGE Basta usare le API WTTS interne.

2

È possibile utilizzare ComponentDispatcher come un modo alternativo per ottenere quegli eventi.

Ecco un esempio di classe per completarlo.

public class Win32Session 
{ 
    private const int NOTIFY_FOR_THIS_SESSION = 0; 
    private const int WM_WTSSESSION_CHANGE = 0x2b1; 
    private const int WTS_SESSION_LOCK = 0x7; 
    private const int WTS_SESSION_UNLOCK = 0x8; 

    public event EventHandler MachineLocked; 
    public event EventHandler MachineUnlocked; 

    public Win32Session() 
    { 
     ComponentDispatcher.ThreadFilterMessage += ComponentDispatcher_ThreadFilterMessage; 
    } 

    void ComponentDispatcher_ThreadFilterMessage(ref MSG msg, ref bool handled) 
    { 
     if (msg.message == WM_WTSSESSION_CHANGE) 
     { 
      int value = msg.wParam.ToInt32(); 
      if (value == WTS_SESSION_LOCK) 
      { 
       OnMachineLocked(EventArgs.Empty); 
      } 
      else if (value == WTS_SESSION_UNLOCK) 
      { 
       OnMachineUnlocked(EventArgs.Empty); 
      } 
     } 
    } 

    protected virtual void OnMachineLocked(EventArgs e) 
    { 
     EventHandler temp = MachineLocked; 
     if (temp != null) 
     { 
      temp(this, e); 
     } 
    } 

    protected virtual void OnMachineUnlocked(EventArgs e) 
    { 
     EventHandler temp = MachineUnlocked; 
     if (temp != null) 
     { 
      temp(this, e); 
     } 
    } 
} 
Problemi correlati