Results 1 to 13 of 13

Thread: Time Limit Log off

  1. #1

    Thread Starter
    New Member
    Join Date
    Aug 2000
    Posts
    7
    I want to write a program that would log a user off from an Winnt 4 workstation or a Windows 2000 prof. machine if he has been idle (if the keyboard or mouse hasn't been used for, say an hr).

    Can anyone offer any suggestions or code snippets as to how I should go about doing this? Thanks.

  2. #2
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    If you intercepted the keyboard handler, then you could have a loop which uses GetTickCount, and when the difference got to high without an event, then log them off.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  3. #3
    Good Ol' Platypus Sastraxi's Avatar
    Join Date
    Jan 2000
    Location
    Ontario, Canada
    Posts
    5,134
    Use the GetAsyncKeyState to find out if it's been null. Then, after an hour, if it's still null, then shut down the computer. I'm sure there's a call you can make to do that. Also, you should enumerate the windows and use TerminateProcess API to close 'em. BTW: Make sure to reset the timer when a key is pressed.

    All contents of the above post that aren't somebody elses are mine, not the property of some media corporation.
    (Just a heads-up)

  4. #4
    Fanatic Member gwdash's Avatar
    Join Date
    Aug 2000
    Location
    Minnesota
    Posts
    666
    to log them off, just use the ExitWindowsEx API function, with the Log-Off Parameter
    GWDASH
    [b]VB6, Perl, ASP, HTML, JavaScript, VBScript, SQL, C, C++, Linux , Java, PHP, MySQL, XML[b]

  5. #5
    Guest
    Try this: Make a Form with a Timer and put the following code into it.

    Code:
    Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer
    Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
    Private Declare Function ExitWindowsEx Lib "user32" (ByVal uFlags As Long, ByVal dwReserved As Long) As Long
    Const EWX_LOGOFF = 0
    
    Private Type POINTAPI
        X As Long
        Y As Long
    End Type
    
    Dim CurPos As POINTAPI
    Dim OldX As Single
    Dim OldY As Single
    Dim StartTime As Date
    
    Function Start()
        'Create a StartTime
        StartTime = Format(Time, "hh:mm:ss")
    End Function
    
    Private Sub Form_Load()
        'Give a StartTime when the Form loads
        Start
        Timer1.Interval = 1
    End Sub
    
    Private Sub Timer1_Timer()
        
            'Loop through the keys and see if they were pressed
            For I = 32 To 127
                If GetAsyncKeyState(I) Then Start
                'Do the same for the Mouse Buttons
                If GetAsyncKeyState(vbLeftButton) Then Start
                If GetAsyncKeyState(vbRightButton) Then Start
            Next
            
            
            'Check if mouse is still
            MyPos = GetCursorPos(CurPos)
            X = CurPos.X
            Y = CurPos.Y
            If OldX = X And OldY = Y Then
                'The mouse is still
            Else
                'the Mouse is not still
                OldX = X
                OldY = Y
                'Create a new StartTime
                Start
            End If
            
            
            'Subtract the current time with the StartTime
            Retval = StartTime - Time
            Retval = Format(Retval, "hh:mm:ss")
            'If it's been 1 hour then log-off
            If Retval = "01:00:00" Then
                ExitWindowsEx EWX_LOGOFF, 0&
            End If
        
    End Sub

  6. #6

    Thread Starter
    New Member
    Join Date
    Aug 2000
    Posts
    7
    Thanks!

    I used your suggestions (polling the keyboard and mouse and using ExitWindowsEx to force them off) and its working. I just have a couple more questions about this:

    These machines are networked....does ExitWindowsEx log the user off the server as well or just the local machine?

    Another problem now is to hide it from the Task Manager so that normal users cannot see it.The only way I can think of doing this is by making this into a service (I think the NTSVC.OCX is used for that).

    The thing I am afraid of is that this service will run even when someone is not logged on,... so what happens when an hour elapses and the computer logs off a user when there is NO user logged on? Is there a way to detect that there is no user logged onto the machine?

    Is there any other way to hide from NT and win2k task manager without making the app into a service?

  7. #7
    Fanatic Member gwdash's Avatar
    Join Date
    Aug 2000
    Location
    Minnesota
    Posts
    666

    Cool

    There is code here at vb world to hide it from the task manager, also, using the QueryUnload event, you could make it so that if they used the task manager to get rid of it, it would log them off. You could probably find out if there is a user using the GetUserName API.

    Code:
    Public Declare Function GetCurrentProcessId _
    Lib "kernel32" () As Long
    Public Declare Function GetCurrentProcess _
    Lib "kernel32" () As Long
    Public Declare Function RegisterServiceProcess _
    Lib "kernel32" (ByVal dwProcessID As Long, _
    ByVal dwType As Long) As Long
    
    Public Const RSP_SIMPLE_SERVICE = 1
    Public Const RSP_UNREGISTER_SERVICE = 0
    
    
    Public Sub MakeMeService()
    Dim pid As Long
    Dim reserv As Long
    
    pid = GetCurrentProcessId()
    regserv = RegisterServiceProcess(pid, RSP_SIMPLE_SERVICE)
    End Sub
    
    
    Public Sub UnMakeMeService()
    Dim pid As Long
    Dim reserv As Long
    
    pid = GetCurrentProcessId()
    regserv = RegisterServiceProcess(pid, _
    RSP_UNREGISTER_SERVICE)
    End Sub
    Use MakeMeService to Hide
    in unload or queryunload use UnMakeMeService.
    GWDASH
    [b]VB6, Perl, ASP, HTML, JavaScript, VBScript, SQL, C, C++, Linux , Java, PHP, MySQL, XML[b]

  8. #8

    Thread Starter
    New Member
    Join Date
    Aug 2000
    Posts
    7
    I have tried using RegisterServiceProcess in Winnt but I can't get it to work. It works perfectly in Win9x though.

    How can I check if the QueryUnload event has been raised?

  9. #9
    Fanatic Member gwdash's Avatar
    Join Date
    Aug 2000
    Location
    Minnesota
    Posts
    666
    it's a form event. ie. Form_QueryUnload(UnloadMode as integer) or something like that
    GWDASH
    [b]VB6, Perl, ASP, HTML, JavaScript, VBScript, SQL, C, C++, Linux , Java, PHP, MySQL, XML[b]

  10. #10
    Guest
    For Windows NT, you can hide your App using the following code.
    Code:
    App.TaskVisible = False

  11. #11

    Thread Starter
    New Member
    Join Date
    Aug 2000
    Posts
    7
    Is there a way I can create some sort of global mouse hook in VB that can catch any mouse activity (not just over my window)?

    Would that be better or worse than polling the mouse for this app?

  12. #12
    Hyperactive Member Steve Stunning's Avatar
    Join Date
    Jul 1999
    Location
    Fairfax, Virginia
    Posts
    314
    Megatron!!!

    Thanks for your code... That works great! I had tried to create the same with poor results.. hahahahhah

    Yours will do the trick just fine!
    Steve Stunning

  13. #13
    C# Aficionado Lord_Rat's Avatar
    Join Date
    Sep 2001
    Location
    Cave
    Posts
    2,497
    Originally posted by Baadshah628
    The thing I am afraid of is that this service will run even when someone is not logged on,... so what happens when an hour elapses and the computer logs off a user when there is NO user logged on? Is there a way to detect that there is no user logged onto the machine?
    It will run, but it will return with an error -- something like "WM_NotLoggedOn". It's only posted to the Messaging Subsystem, so it shouldn't be a problem.
    Need to re-register ASP.NET?
    C:\WINNT\Microsoft.NET\Framework\v#VERSIONNUMBER#\aspnet_regiis -i

    (Edit #VERSIONNUMBER# as needed - do a DIR if you don't know)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width