Results 1 to 5 of 5

Thread: Detecting Windows Shut down

  1. #1
    Guest

    Talking

    Hallo

    I need to log when a user is shutting down the computer. How can I detect this to log time in a logfile?

    Thank you

  2. #2
    Fanatic Member coox's Avatar
    Join Date
    Oct 1999
    Posts
    550
    Hi Herman - what I'd do is make a little exe which run's on startup (either put a shortcut in the Startup folder or put it in the registry - can't remember where exactly but it's pretty easy to find) and then use your exe's form's QueryUnload event to see if windows is shutting down - then run a bit of code to append the time to a text file or something...
    Code:
    Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
     If UnloadMode = vbAppWindows Then
      LogTime
     End If
    End Sub
    I've used the above code on a little prog I made to map some network drives, and the above calls a sub that unmaps them before shutting down (I was finding my machine was hanging if I didn't unmap them) - works fine...
    Nick Cook
    VB6 (SP6)

  3. #3
    Guest
    Hallo

    Will VB pickup (in this way) when Windows95,98 or NT shuts down? I would like to detect when Windows 95,98 or NT starts shutting down. Only then must I log the time.

    Thanx

  4. #4
    Fanatic Member coox's Avatar
    Join Date
    Oct 1999
    Posts
    550
    Herman, yeah it will. Try it!
    Nick Cook
    VB6 (SP6)

  5. #5
    Guru Yonatan's Avatar
    Join Date
    Apr 1999
    Location
    Israel
    Posts
    892
    If Windows is shut down with the EWX_FORCE flag (for example: Ctrl+Alt+Del -> Shut Down), there is no way to log it.
    Also, if your program is closed (Ctrl+Alt+Del -> your program -> End Task), it won't log it.

    If you can count on the users of your program to shut down the computer normally and to not close your program, coox's method works.

    To make it a tiny bit harder to close your program (it will always be possible to close it somehow):
    Code:
    Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
        Dim btFile As Byte
        
        If UnloadMode = vbAppWindows Then
            ' Windows is closing... Log it:
            btFile = FreeFile
            
            Open "C:\ShutDown.log" For Append As btFile
                Print #btFile, "Shutdown at: " & Now
            Close btFile
        Else
            ' Your app is closing... Try to prevent it:
            Cancel = True
        End If
    End Sub

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