Jump to content

Get Session State [Solved]


Recommended Posts

Hello,

I found the following VB6 code:

FORM Code

Option Explicit

      Private Declare Function WTSRegisterSessionNotification _
                      Lib "Wtsapi32" (ByVal hWnd As Long, _
                                      ByVal THISSESS As Long) As Long
      Private Declare Function WTSUnRegisterSessionNotification _
                      Lib "Wtsapi32" (ByVal hWnd As Long) As Long
       
      Private Const NOTIFY_FOR_ALL_SESSIONS As Long = 1
      Private Const NOTIFY_FOR_THIS_SESSION As Long = 0
       
      Private Sub Form_Load()
       
          HookWindow Me.hWnd
          Call WTSRegisterSessionNotification(Me.hWnd, NOTIFY_FOR_ALL_SESSIONS)

      End Sub

MODULE Code

Option Explicit

   Public Declare Function CallWindowProc _
                  Lib "user32" _
                  Alias "CallWindowProcA" (ByVal lpPrevWndFunc As Long, _
                                           ByVal hWnd As Long, _
                                           ByVal msg As Long, _
                                           ByVal wParam As Long, _
                                           ByVal lParam As Long) As Long

   Public Declare Function SetWindowLong _
                  Lib "user32" _
                  Alias "SetWindowLongA" (ByVal hWnd As Long, _
                                          ByVal nIndex As Long, _
                                          ByVal dwNewLong As Long) As Long

   Public Const WM_WTSSESSION_CHANGE As Long = &H2B1
   Public Const WTS_CONSOLE_CONNECT As Long = 1
   Public Const WTS_CONSOLE_DISCONNECT As Long = 2
   Public Const WTS_REMOTE_CONNECT As Long = 3
   Public Const WTS_REMOTE_DISCONNECT As Long = 4
   Public Const WTS_SESSION_LOGON As Long = 5
   Public Const WTS_SESSION_LOGOFF As Long = 6
   Public Const WTS_SESSION_LOCK As Long = 7
   Public Const WTS_SESSION_UNLOCK As Long = 8
   Public Const WTS_SESSION_REMOTE_CONTROL As Long = 9
   Public Const FORMAT_MESSAGE_FROM_SYSTEM = &H1000
   Public Const LANG_NEUTRAL = &H0
   Public Const GWL_WNDPROC As Long = (-4)
   Public Const WM_DESTROY = &H2
   Public lPrevProc As Long

   Public Sub HookWindow(ByVal lHandle As Long)

       If lPrevProc = 0 Then
           lPrevProc = SetWindowLong(lHandle, GWL_WNDPROC, AddressOf WindowProc)
       Else
           Call SetWindowLong(lHandle, GWL_WNDPROC, lPrevProc)
       End If
   End Sub

   Public Function WindowProc(ByVal hWnd As Long, _
                              ByVal iMsg As Long, _
                              ByVal wParam As Long, _
                              ByVal lParam As Long) As Long
       Select Case iMsg
           Case WM_WTSSESSION_CHANGE
               Select Case wParam
                   Case WTS_CONSOLE_CONNECT
                       MsgBox "A session was connected to the console session at" & Now

                   Case WTS_CONSOLE_DISCONNECT
                       MsgBox "A session was disconnected from the console session at" & Now

                   Case WTS_REMOTE_CONNECT
                       MsgBox "A session was connected to the remote session at" & Now

                   Case WTS_REMOTE_DISCONNECT
                       MsgBox "A session was disconnected from the remote session at" & Now

                   Case WTS_SESSION_LOGON
                       MsgBox "A user has logged on to the session at" & Now

                   Case WTS_SESSION_LOGOFF
                       MsgBox "A user has logged off the session at" & Now

                   Case WTS_SESSION_LOCK
                       MsgBox "A session has been locked at" & Now

                   Case WTS_SESSION_UNLOCK
                       MsgBox "A session has been unlocked at" & Now

                   Case WTS_SESSION_REMOTE_CONTROL
                       MsgBox "A session has changed its remote controlled status. To determine the status, call GetSystemMetrics and check the SM_REMOTECONTROL metric at" & Now

               End Select

           Case WM_DESTROY
               HookWindow Form1.hWnd
       End Select

       WindowProc = CallWindowProc(lPrevProc, hWnd, iMsg, wParam, lParam)

  End Function

I'm translating it in AutoIt but i really don't know how to translate this line: lPrevProc = SetWindowLong(lHandle, GWL_WNDPROC, AddressOf WindowProc)

My problem is AdressOf WindowsProc

Can someone help me ?

Edited by Jango
Link to comment
Share on other sites

i think, in AutoIt GUIRegisterMsg($WM_WTSSESSION_CHANGE, "WindowProc") should be enough :D hookig the windowproc is not required.

Edited by ProgAndy

*GERMAN* [note: you are not allowed to remove author / modified info from my UDFs]My UDFs:[_SetImageBinaryToCtrl] [_TaskDialog] [AutoItObject] [Animated GIF (GDI+)] [ClipPut for Image] [FreeImage] [GDI32 UDFs] [GDIPlus Progressbar] [Hotkey-Selector] [Multiline Inputbox] [MySQL without ODBC] [RichEdit UDFs] [SpeechAPI Example] [WinHTTP]UDFs included in AutoIt: FTP_Ex (as FTPEx), _WinAPI_SetLayeredWindowAttributes

Link to comment
Share on other sites

i think, in AutoIt GUIRegisterMsg($WM_WTSSESSION_CHANGE, "WindowProc") should be enough :D hookig the windowproc is not required.

Thank you ProgAndy anyway I'm a little bit lost ... also to convert WindowProc function ...

Link to comment
Share on other sites

It should be something like this: (still includes are missing, possibly some Constants are already declared in WindowsConstants)

This was just some search'n'replace (Public Const -> Global Const $; &H -> 0x, ...) :D

Global Const $WM_WTSSESSION_CHANGE = 0x2B1
   Global Const $WTS_CONSOLE_CONNECT = 1
   Global Const $WTS_CONSOLE_DISCONNECT = 2
   Global Const $WTS_REMOTE_CONNECT = 3
   Global Const $WTS_REMOTE_DISCONNECT = 4
   Global Const $WTS_SESSION_LOGON = 5
   Global Const $WTS_SESSION_LOGOFF = 6
   Global Const $WTS_SESSION_LOCK = 7
   Global Const $WTS_SESSION_UNLOCK = 8
   Global Const $WTS_SESSION_REMOTE_CONTROL = 9
   Global Const $FORMAT_MESSAGE_FROM_SYSTEM = 0x1000
   Global Const $LANG_NEUTRAL = 0x0
   Global Const $GWL_WNDPROC = (-4)
   Global Const $WM_DESTROY = 0x2

GUIRegisterMsg($WM_WTSSESSION_CHANGE, "MY_WM_WTSSESSION_CHANGE")

Func MY_WM_WTSSESSION_CHANGE($hWnd, $uMsg, $wParam, $lParam)
       Switch $uMsg
           Case $WM_WTSSESSION_CHANGE
               Switch $wParam
                   Case $WTS_CONSOLE_CONNECT
                       MsgBox(0, "WM_WTSESSION_CHANGE", "A session was connected to the console session at " & _Now() )

                   Case $WTS_CONSOLE_DISCONNECT
                       MsgBox(0, "WM_WTSESSION_CHANGE", "A session was disconnected from the console session at " & _Now() )

                   Case $WTS_REMOTE_CONNECT
                       MsgBox(0, "WM_WTSESSION_CHANGE", "A session was connected to the remote session at " & _Now() )

                   Case $WTS_REMOTE_DISCONNECT
                       MsgBox(0, "WM_WTSESSION_CHANGE", "A session was disconnected from the remote session at " & _Now() )

                   Case $WTS_SESSION_LOGON
                       MsgBox(0, "WM_WTSESSION_CHANGE", "A user has logged on to the session at " & _Now() )

                   Case $WTS_SESSION_LOGOFF
                       MsgBox(0, "WM_WTSESSION_CHANGE", "A user has logged off the session at " & _Now() )

                   Case $WTS_SESSION_LOCK
                       MsgBox(0, "WM_WTSESSION_CHANGE", "A session has been locked at " & _Now() )

                   Case $WTS_SESSION_UNLOCK
                       MsgBox(0, "WM_WTSESSION_CHANGE", "A session has been unlocked at " & _Now() )

                   Case $WTS_SESSION_REMOTE_CONTROL
                       MsgBox(0, "WM_WTSESSION_CHANGE", "A session has changed its remote controlled status. To determine the status, call GetSystemMetrics and check the SM_REMOTECONTROL metric at " & _Now() )

               EndSwitch

       EndSwitch
    Return $GUI_RUNDEFMSG
EndFunc
Edited by ProgAndy

*GERMAN* [note: you are not allowed to remove author / modified info from my UDFs]My UDFs:[_SetImageBinaryToCtrl] [_TaskDialog] [AutoItObject] [Animated GIF (GDI+)] [ClipPut for Image] [FreeImage] [GDI32 UDFs] [GDIPlus Progressbar] [Hotkey-Selector] [Multiline Inputbox] [MySQL without ODBC] [RichEdit UDFs] [SpeechAPI Example] [WinHTTP]UDFs included in AutoIt: FTP_Ex (as FTPEx), _WinAPI_SetLayeredWindowAttributes

Link to comment
Share on other sites

Hello again i finnaly found this function that do the trick:

Global Const $DESKTOP_SWITCHDESKTOP = 0x100

Func _CheckLocked()
    $hLockedDLL = DllOpen("user32.dll")
    $hDesktop = DllCall($hLockedDLL, "int", "OpenDesktop", "str", "Default", "int", 0, "int", 0, "int", $DESKTOP_SWITCHDESKTOP)
    $ret = DllCall($hLockedDLL, "int", "SwitchDesktop", "int", $hDesktop[0])
    DllCall($hLockedDLL, "int", "CloseDesktop", "int", $hDesktop[0])

    If $ret[0] = 0 Then
        $iLocked = 1
    ElseIf $ret[0] = 1 Then
        $iLocked = 0
    EndIf
    
    DllClose($hLockedDLL)
    
    If $iLocked Then
        Return 1
    Else
        Return 0
    EndIf
EndFunc
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...