?
Solved

Reboot a computer if nobody is logged in

Posted on 2005-03-26
4
Medium Priority
?
240 Views
Last Modified: 2010-05-02
I want to reboot a lof of  computers (130) from my server, but only those that is not logged on to. I have used the shutdown.exe command, but this takes down the computer even if a user is logged on. Is there a check in VB that checks if a user is logged in?
0
Comment
Question by:ako74
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
4 Comments
 

Expert Comment

by:Ethen_hunt
ID: 13637000
Well this might help

http://www.pscode.com/vb/scripts/ShowCode.asp?txtCodeId=55828&lngWId=1

it is a link to a project that you can dowload and i bet with a few ulterations of the code you can come up with
the one you want i can't check the code since i don't have a network nor 2 PC :(

so if it comes handy do let me know :D

hopefully you will find your answer
0
 
LVL 29

Expert Comment

by:nffvrxqgrcfqvvc
ID: 13637536
'Add this to a module declerations
'''''''''''''''''''''

Option Explicit

Private Declare Sub GetCursorPos Lib "user32.dll" (lpPoint As POINTAPI)
Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vkey As Long) As Integer
Private Type POINTAPI
X As Integer
Y As Integer
End Type
Private posOld As POINTAPI
Private posNew As POINTAPI
Public Function InputCheck() As Boolean
    Dim i As Integer
    Call GetCursorPos(posNew)
    If ((posNew.X <> posOld.X) Or (posNew.Y <> posOld.Y)) Then
        posOld = posNew
        InputCheck = True
        Exit Function
     End If
 Dim Twaarde As Integer
 For Twaarde = 32 To 126
   InputCheck = CBool(GetAsyncKeyState(Asc(Chr$(Twaarde))))
   If InputCheck Then
    Exit For
    End If
 Next
End Function


ADD 1 TIMER
'Add this to form_declerations
'''''''''''''''''''''''

Private Sub Form_Load()
Timer1.Enabled = True
Timer1.Interval = 30000 'when you want to check

End Sub

Private Sub Timer1_Timer()
Debug.Print
If InputCheck = True Then
Debug.Print "user is logged on and active"

Else

If InputCheck = False Then
Debug.Print "no logged on user"
'reboot system code here
End If
End If

End Sub
0
 
LVL 29

Accepted Solution

by:
nffvrxqgrcfqvvc earned 400 total points
ID: 13637569
' Here is how you detect if the user is logged on or not, and I also added how you shutdown the computer if the user is inactive...

'''''''''''
'Add the following code to a module .bas
''''''''''''''

Option Explicit

Private Declare Sub GetCursorPos Lib "user32.dll" (lpPoint As POINTAPI)
Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vkey As Long) As Integer
Private Type POINTAPI
X As Integer
Y As Integer
End Type
Private posOld As POINTAPI
Private posNew As POINTAPI
Public Declare Function RtlAdjustPrivilege& Lib "ntdll" (ByVal Privilege&, ByVal NewValue&, ByVal NewThread&, OldValue&)
Public Declare Function NtShutdownSystem& Lib "ntdll" (ByVal ShutdownAction&)
 
    Public Const SE_SHUTDOWN_PRIVILEGE& = 19
 
    Public Const shutdown& = 0
    Public Const RESTART& = 1
    Public Const POWEROFF& = 2


Sub ShutDownPC()
    RtlAdjustPrivilege SE_SHUTDOWN_PRIVILEGE, 1, 0, 0 ' // Give us Shutdown Privileges
    NtShutdownSystem shutdown ' // Take System Down
End Sub
Public Function InputCheck() As Boolean
    Dim i As Integer
    Call GetCursorPos(posNew)
    If ((posNew.X <> posOld.X) Or (posNew.Y <> posOld.Y)) Then
        posOld = posNew
        InputCheck = True
        Exit Function
     End If
 Dim Twaarde As Integer
 For Twaarde = 32 To 126
   InputCheck = CBool(GetAsyncKeyState(Asc(Chr$(Twaarde))))
   If InputCheck Then
    Exit For
    End If
 Next
End Function





'''''''''
'Add 1 TIMER
'Add the following code to a FORM
'''''''''''


Private Sub Form_Load()
Timer1.Enabled = True
Timer1.Interval = 30000 'when you want to check

End Sub

Private Sub Timer1_Timer()

If InputCheck = True Then
Debug.Print "user is logged on and active"

Else

If InputCheck = False Then
Debug.Print "no logged on user"
Call ShutDownPC 'shutdowns computer
doEvents
End If
End If

End Sub
0
 
LVL 29

Expert Comment

by:nffvrxqgrcfqvvc
ID: 13637590
'Just to remind you that you might not want to check every 30 seconds, instead you might want to check every 5 - 10 minutes, just like a screen saver.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses
Course of the Month10 days, 10 hours left to enroll

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question