MENSAGEM AO BLOQUEAR A MAQUINA

JONATHANSTECKER 25/03/2011 10:10:55
#369124
Bom dia Senhores,

Estou tentando disparar um método em meu sistema, até aqui tudo certo.
Porém esse método só pode ser disparado se máquina do usuário estiver bloqueada.
O bloqueio seria usando WINDOWS + L, ou pelo CTRL + ALT + DEL.

Exemplo, em um timer verifico se a minha máquina está bloqueada. Caso esteja, executa o método.

Estou quebrando a cabeça aqui...
PEGUDO 25/03/2011 12:56:38
#369155
Resposta escolhida
[txt-size=6]Cara, eu acho que consegui alguma coisa na internet.
Mas não testei aqui com máquinas desbloqueadas, não.
Faz o teste aí em alguma máquina que esteja bloqueada e vê se te serve.

Primeiro, copia este código e cola em uma Classe:[/txt-size]

[txt-color=#0000f0]Public Class[/txt-color] WorkStation
#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
#End Region

#Region [Ô]WorkStationReader Events[Ô]
Event locked(ByVal ivarreturn As Object)
#End Region

#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) = [Ô]Erro ao abrir o Desktop: [Ô] & Err.LastDllError
ivarreturn(1) = False
WorkStationISLocked = ivarreturn
RaiseEvent locked(ivarreturn)
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) = [Ô]Bloqueado : [Ô] [ô]& Err.LastDllError
ivarreturn(1) = True
WorkStationISLocked = ivarreturn
RaiseEvent locked(ivarreturn)
GoTo CleanUpProc
Else
ivarreturn(0) = [Ô]Erro inesperado: [Ô] & Err.LastDllError
ivarreturn(1) = False
WorkStationISLocked = ivarreturn
GoTo CleanUpProc
End If
Else
ivarreturn(0) = [Ô]Desbloqueado : [Ô]
ivarreturn(1) = False
WorkStationISLocked = ivarreturn
RaiseEvent locked(ivarreturn)
GoTo CleanUpProc
End If
End If
Exit Function

CleanUpProc:

p_lngHwnd = CloseDesktop(p_lngHwnd)
End Function
#End Region
[txt-color=#0000f0]End Class[/txt-color]


[txt-size=6]Agora, faça uma nova instância dela, em um botão por exemplo, e teste:[/txt-size]

[txt-color=#0000f0]Private Sub[/txt-color] Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
[txt-color=#0000f0]Dim[/txt-color] Esta [txt-color=#0000f0]As New[/txt-color] WorkStation
[txt-color=#0000f0]Dim[/txt-color] estaounao = Esta.WorkStationISLocked

[txt-color=#0000f0]For Each[/txt-color] Ponto [txt-color=#0000f0]In[/txt-color] estaounao
[txt-color=#0000f0]Try
Select Case[/txt-color] Ponto
[txt-color=#0000f0]Case True[/txt-color]
MsgBox([txt-color=#e80000][Ô]PC Bloqueado[Ô][/txt-color])
[txt-color=#0000f0]Case False[/txt-color]
MsgBox([txt-color=#e80000][Ô]PC Desbloqueado[Ô][/txt-color])
[txt-color=#0000f0]End Select
Exit For
Catch[/txt-color] ex [txt-color=#0000f0]As Exception
End Try
Next
End Sub[/txt-color]

Espero que funcione.
JONATHANSTECKER 25/03/2011 13:34:37
#369160
PEGUDO, agradeço a ajuda.
Porém consegui de uma forma mais prática....
Estou usando os eventos do sistema.
Veja a facilidade.

    [ô][ô][ô] <summary>
[ô][ô][ô] Construtor da classe
[ô][ô][ô] </summary>
Public Sub New()
InitializeComponent()
AddHandler Microsoft.Win32.SystemEvents.SessionSwitch, AddressOf SystemEvents_Sessionswitch
End Sub

Private Sub SystemEvents_Sessionswitch(ByVal sender As Object, ByVal e As Microsoft.Win32.SessionSwitchEventArgs)
Select Case e.Reason
Case Microsoft.Win32.SessionSwitchReason.SessionLock
MessageBox.Show([Ô]Windows bloqueado as: [Ô] & Date.Now)
Case Microsoft.Win32.SessionSwitchReason.SessionUnlock
MessageBox.Show([Ô]Windows desbloqueado as: [Ô] & Date.Now)
End Select
End Sub
Tópico encerrado , respostas não são mais permitidas