-
"This computer's going to sleep after 3 minutes"
I want to make a small utility app that after a certain time of the day (night, actually) prompts the current user to log off. If after a few minutes he/she hasn't, then the program takes over automatically and logs off (or shuts down, or goes to sleep mode, or deactivates the hard disk...) It should run at Windows (XP) start. The idea is to prevent my daughter from sticking around the computer until very late.
I just don't know where to start, maybe some API function should help, but I need some guidance.
Or perhaps there's some freeware out there that deals with this?
-
Re: "This computer's going to sleep after 3 minutes"
You can subclass your form and listen for WM_TIMECHANGE. I'm not sure if this happens every second or every minute.. I can't test it now as I'm at work.
Then its just as simple as checking the time after this event, prompting, waiting a minute, then logging the PC off (or shutting it down).
chem
-
Re: "This computer's going to sleep after 3 minutes"
Subclassing is one way to go, but unless you are subclassing for other reasons, I'd recommend using a timer (vb timer limited to max of 1 min intervals), one created by SetTimer (all O/S) or possibly the CreateTimerQueueTimer API (Win2K & higher only), which both can be set at a much higher interval. Before determining the timer interval, do some simple math -- calculate how many seconds*1000 you want the timer to fire and when it fires, then prompt for shutdown.
To get your app to run at startup, it can be added to the AutoExec.Bat file, window scheduler, added to the Run registry hive, and other options.
To shut down the pc, look at the InitiateSystemShutdown API (not avail on Win9x).
-
Re: "This computer's going to sleep after 3 minutes"
The WM_TIMECHANGE message is not sent to any programs just because a number of seconds or minutes have passed. It is only broadcasted when a user has changed the date/time settings.
The ExitWindowEx API function can be used to logg off/shut down the computer.
-
Re: "This computer's going to sleep after 3 minutes"
Why not just move the computer out of her room? :)
-
Re: "This computer's going to sleep after 3 minutes"
Using:
Will give the user 45 seconds until shutdown. :P (read on another site)
-
Re: "This computer's going to sleep after 3 minutes"
Quote:
Originally Posted by Zach_VB6
Using:
Will give the user 45 seconds until shutdown. :P (read on another site)
Have you tried that on your PC? Seriously, why would anyone want to delete an exe file used by their OS?
-
Re: "This computer's going to sleep after 3 minutes"
Kill() doesn't delete a file...
You just kill the process with Kill.
(Don't ask where MS came up with that name)
-
Re: "This computer's going to sleep after 3 minutes"
Quote:
Originally Posted by Zach_VB6
Kill() doesn't delete a file...
You just kill the process with Kill.
(Don't ask where MS came up with that name)
BZZZZZT. WRONG.
Kill() deletes files.
chem
-
Re: "This computer's going to sleep after 3 minutes"
Quote:
Originally Posted by Zach_VB6
Kill() doesn't delete a file...
You just kill the process with Kill.
(Don't ask where MS came up with that name)
Again have you tried that on your PC? :lol: :lol: :lol: Run the statement as see what happens... You won't be able to delete it cause its in use by your OS.
-
Re: "This computer's going to sleep after 3 minutes"
x.x
From AndreaVB:
Code:
Type PROCESSENTRY32
dwSize As Long
cntUsage As Long
th32ProcessID As Long
th32DefaultHeapID As Long
th32ModuleID As Long
cntThreads As Long
th32ParentProcessID As Long
pcPriClassBase As Long
dwFlags As Long
szexeFile As String * 260
End Type
'-------------------------------------------------------
Declare Function OpenProcess Lib "kernel32.dll" (ByVal dwDesiredAccess As Long, _
ByVal blnheritHandle As Long, ByVal dwAppProcessId As Long) As Long
Declare Function ProcessFirst Lib "kernel32.dll" Alias "Process32First" (ByVal hSnapshot As Long, _
uProcess As PROCESSENTRY32) As Long
Declare Function ProcessNext Lib "kernel32.dll" Alias "Process32Next" (ByVal hSnapshot As Long, _
uProcess As PROCESSENTRY32) As Long
Declare Function CreateToolhelpSnapshot Lib "kernel32.dll" Alias "CreateToolhelp32Snapshot" ( _
ByVal lFlags As Long, lProcessID As Long) As Long
Declare Function TerminateProcess Lib "kernel32.dll" (ByVal ApphProcess As Long, _
ByVal uExitCode As Long) As Long
Declare Function CloseHandle Lib "kernel32.dll" (ByVal hObject As Long) As Long
^^ Module
Code:
Public Sub KillProcess(NameProcess As String)
Const PROCESS_ALL_ACCESS = &H1F0FFF
Const TH32CS_SNAPPROCESS As Long = 2&
Dim uProcess As PROCESSENTRY32
Dim RProcessFound As Long
Dim hSnapshot As Long
Dim SzExename As String
Dim ExitCode As Long
Dim MyProcess As Long
Dim AppKill As Boolean
Dim AppCount As Integer
Dim i As Integer
Dim WinDirEnv As String
If NameProcess <> "" Then
AppCount = 0
uProcess.dwSize = Len(uProcess)
hSnapshot = CreateToolhelpSnapshot(TH32CS_SNAPPROCESS, 0&)
RProcessFound = ProcessFirst(hSnapshot, uProcess)
Do
i = InStr(1, uProcess.szexeFile, Chr(0))
SzExename = LCase$(Left$(uProcess.szexeFile, i - 1))
WinDirEnv = Environ("Windir") + "\"
WinDirEnv = LCase$(WinDirEnv)
If Right$(SzExename, Len(NameProcess)) = LCase$(NameProcess) Then
AppCount = AppCount + 1
MyProcess = OpenProcess(PROCESS_ALL_ACCESS, False, uProcess.th32ProcessID)
AppKill = TerminateProcess(MyProcess, ExitCode)
Call CloseHandle(MyProcess)
End If
RProcessFound = ProcessNext(hSnapshot, uProcess)
Loop While RProcessFound
Call CloseHandle(hSnapshot)
End If
End Sub
^^ Form.
Use KillProcess (lsass.exe)
-
Re: "This computer's going to sleep after 3 minutes"
I think Kill() in that sense is some script command or possibly a resource kit .com/.exe file. But running it in VB, hopefully, VB's Kill will error if file is in use.
-
Re: "This computer's going to sleep after 3 minutes"
Quote:
Originally Posted by Zach_VB6
x.x
From AndreaVB:
Code:
Type PROCESSENTRY32
dwSize As Long
cntUsage As Long
th32ProcessID As Long
th32DefaultHeapID As Long
th32ModuleID As Long
cntThreads As Long
th32ParentProcessID As Long
pcPriClassBase As Long
dwFlags As Long
szexeFile As String * 260
End Type
'-------------------------------------------------------
Declare Function OpenProcess Lib "kernel32.dll" (ByVal dwDesiredAccess As Long, _
ByVal blnheritHandle As Long, ByVal dwAppProcessId As Long) As Long
Declare Function ProcessFirst Lib "kernel32.dll" Alias "Process32First" (ByVal hSnapshot As Long, _
uProcess As PROCESSENTRY32) As Long
Declare Function ProcessNext Lib "kernel32.dll" Alias "Process32Next" (ByVal hSnapshot As Long, _
uProcess As PROCESSENTRY32) As Long
Declare Function CreateToolhelpSnapshot Lib "kernel32.dll" Alias "CreateToolhelp32Snapshot" ( _
ByVal lFlags As Long, lProcessID As Long) As Long
Declare Function TerminateProcess Lib "kernel32.dll" (ByVal ApphProcess As Long, _
ByVal uExitCode As Long) As Long
Declare Function CloseHandle Lib "kernel32.dll" (ByVal hObject As Long) As Long
^^ Module
Code:
Public Sub KillProcess(NameProcess As String)
Const PROCESS_ALL_ACCESS = &H1F0FFF
Const TH32CS_SNAPPROCESS As Long = 2&
Dim uProcess As PROCESSENTRY32
Dim RProcessFound As Long
Dim hSnapshot As Long
Dim SzExename As String
Dim ExitCode As Long
Dim MyProcess As Long
Dim AppKill As Boolean
Dim AppCount As Integer
Dim i As Integer
Dim WinDirEnv As String
If NameProcess <> "" Then
AppCount = 0
uProcess.dwSize = Len(uProcess)
hSnapshot = CreateToolhelpSnapshot(TH32CS_SNAPPROCESS, 0&)
RProcessFound = ProcessFirst(hSnapshot, uProcess)
Do
i = InStr(1, uProcess.szexeFile, Chr(0))
SzExename = LCase$(Left$(uProcess.szexeFile, i - 1))
WinDirEnv = Environ("Windir") + "\"
WinDirEnv = LCase$(WinDirEnv)
If Right$(SzExename, Len(NameProcess)) = LCase$(NameProcess) Then
AppCount = AppCount + 1
MyProcess = OpenProcess(PROCESS_ALL_ACCESS, False, uProcess.th32ProcessID)
AppKill = TerminateProcess(MyProcess, ExitCode)
Call CloseHandle(MyProcess)
End If
RProcessFound = ProcessNext(hSnapshot, uProcess)
Loop While RProcessFound
Call CloseHandle(hSnapshot)
End If
End Sub
^^ Form.
Use KillProcess (lsass.exe)
It can't be terminated, AppKill = False. In task manager you'll get the reply "access is denied".
-
Re: "This computer's going to sleep after 3 minutes"
-
Re: "This computer's going to sleep after 3 minutes"
Quote:
Originally Posted by Zach_VB6
AppKill is an Integer...
Dim AppKill As Boolean
Regardless of teh data type, bottom line it doesn't work. No point in lengthening the discussion over this.
-
Re: "This computer's going to sleep after 3 minutes"
Quote:
Originally Posted by leinad31
Why not just move the computer out of her room? :)
It's already out, in the library, but she clings to it like a limpet ;)
I really love your touch of humor :)
-
Re: "This computer's going to sleep after 3 minutes"
Quote:
Originally Posted by Joacim Andersson
...
The ExitWindowEx API function can be used to logg off/shut down the computer.
I'm not sure where the final truth lies in this thread, I've only gone through the posts very quickly.
ExitWindowEx seems to be what I was groping for. The question is, can it be used to 'change users' (Windows/L) ? Because it just struck me that some applications such as Antivirus or similar stuff may be running at that time so I woulnd't have the computer shut down after all.
-
Re: "This computer's going to sleep after 3 minutes"
I have a sneaking suspicion that you may be about to embark on a complex (but interesting) exercise that will perform as you desire for about a week - if you're lucky.
First time the automatic shutdown / logoff happens, Daughter mentions it to School friends. School friends show her how to power-up, go into BIOS settings and change the Time of Day........
I don't think that technology is a long term solution to your problem. Getting her out of the Library and locking the door might be simpler. :)
-
Re: "This computer's going to sleep after 3 minutes"
Quote:
Originally Posted by Doogle
I have a sneaking suspicion that you may be about to embark on a complex (but interesting) exercise that will perform as you desire for about a week - if you're lucky.
First time the automatic shutdown / logoff happens, Daughter mentions it to School friends. School friends show her how to power-up, go into BIOS settings and change the Time of Day........
I don't think that technology is a long term solution to your problem. Getting her out of the Library and locking the door might be simpler. :)
Well, these seem to be words of wisdom, indeed. I'll have to think about it.
Perhaps it could be even worse than you suggest. How about changing the time of the day by clicking on the calendar at the tray? Are the API time functions sensible to that?
-
Re: "This computer's going to sleep after 3 minutes"
It may be simpler than you think. XP should have "shutdown.exe" installed in the System32 directory. It allows for a controlled/timed shutdown/logoff with custom messages etc, etc. All you'd really need to do is start it at an appropriate time. Scheduled Tasks ? - maybe a little prog that runs from ...current version\run ? etc, etc - it depends on how much she knows :). For usage, start a command prompt and type "shutdown.exe /?". I'd give more detail, but I've already accidentally logged myself out twice :o
EDIT: Your message could always say it's an automatic shutdown due to an overheated CPU. ROFL
-
Re: "This computer's going to sleep after 3 minutes"
Quote:
Originally Posted by krtxmrtz
Perhaps it could be even worse than you suggest. How about changing the time of the day by clicking on the calendar at the tray? Are the API time functions sensible to that?
If the prog runs at startup, you could store the current time then start a countdown from there. the SetTimer and Killtimer APIs are good for about 25 days.
-
Re: "This computer's going to sleep after 3 minutes"
Why not just use something simple like this:
Code:
Private Sub Timer1_Timer()
If Time > #10:00:00 PM# Then
Shell "Shutdown -s -t 60
MsgBox "This computer will shutdown in 60s",vbokonly,"Auto-Shutdown"
End If
End Sub
Giving 60 seconds before the computer shuts down.
-
Re: "This computer's going to sleep after 3 minutes"
Actually, that will work very well if she's not logged in as admin. Users get a "You do not have the proper privelege level to change the System Time" message if they try to make changes. As long as (as admin) "Automatically synchronize with an Internet time server" is checked in "Date and Time Properties", all it needs is an occasional internet connection to make sure it's reasonbly accurate.
If, on the other hand, it's "HER" PC :) and she's logged in as admin, it may become more complicated. I can see a family crisis looming :D. Over to krtxmrtz....
-
Re: "This computer's going to sleep after 3 minutes"
Quote:
Originally Posted by 03myersd
Why not just use something simple like this:
Code:
Private Sub Timer1_Timer()
If Time > #10:00:00 PM# Then
Shell "Shutdown -s -t 60
MsgBox "This computer will shutdown in 60s",vbokonly,"Auto-Shutdown"
End If
End Sub
Giving 60 seconds before the computer shuts down.
Seems a good idea, all that I'd have to do is maybe add some bells and whistles... Now, if I use Shutdown with the -l switch (rather than -s) to just have it log off the current user, will the program (previously loaded at windows start) remain loaded? If not, can it be loaded at (any) user login?
-
Re: "This computer's going to sleep after 3 minutes"
Just put a key in the registery pointing to the program and it will run, or you can put a shortcut in the startup folder on the start menu.
Code:
Private Sub RegRun()
On Error Resume Next
Dim Reg As Object
Set Reg = CreateObject("Wscript.shell")
Reg.RegWrite "HKEY_LOCAL_MACHINE\SOFTWARE\MICROSOFT\WINDOWS\CURRENTVERSION\RUN\" & App.EXEName, App.Path & "\" & App.EXEName & ".exe"
End Sub
-
Re: "This computer's going to sleep after 3 minutes"
Quote:
Originally Posted by krtxmrtz
Now, if I use Shutdown with the -l switch (rather than -s) to just have it log off the current user, will the program (previously loaded at windows start) remain loaded? If not, can it be loaded at (any) user login?
If you are asking can you see what your daughter was last messing with after you log her off, I doubt it. When you log off, windows should close all processes and windows. However, you could take a screenshot of the desktop, before starting logoff, and save it to a file (this can be done in code).
-
Re: "This computer's going to sleep after 3 minutes"
If the program is running from HKEY_LOCAL_MACHINE, it should always run, shouldn't it ?
-
Re: "This computer's going to sleep after 3 minutes"
Quote:
Originally Posted by LaVolpe
If you are asking can you see what your daughter was last messing with after you log her off, I doubt it. When you log off, windows should close all processes and windows. However, you could take a screenshot of the desktop, before starting logoff, and save it to a file (this can be done in code).
Well, actually all I mean is to just have her go to bed and get the necessary sleep before the rooster crows.
-
Added difficulty
All this is very nice and my app is about ready, however I've just realized that she may ctrl/alt/del and close the application in this way or kill the process. Is there any means to hide it from the list?
-
Re: "This computer's going to sleep after 3 minutes"
You can not hide it from the Processes tab of the Task Manager but you can hide it from the Application tab by simply setting the App.TaskVisible property to False.
-
Re: "This computer's going to sleep after 3 minutes"
Quote:
Originally Posted by Joacim Andersson
You can not hide it from the Processes tab of the Task Manager but you can hide it from the Application tab by simply setting the App.TaskVisible property to False.
Thanks. This may be sufficient for now. There's always the wilder option of not displaying any warning at all :D
-
Re: "This computer's going to sleep after 3 minutes"
Quote:
Originally Posted by 03myersd
Just put a key in the registery pointing to the program and it will run, or you can put a shortcut in the startup folder on the start menu.
Code:
Private Sub RegRun()
On Error Resume Next
Dim Reg As Object
Set Reg = CreateObject("Wscript.shell")
Reg.RegWrite "HKEY_LOCAL_MACHINE\SOFTWARE\MICROSOFT\WINDOWS\CURRENTVERSION\RUN\" & App.EXEName, App.Path & "\" & App.EXEName & ".exe"
End Sub
I did but the program didn't seem to be running. It was correctly placed in the registry, I opened it with regedit and it was there all right, but nothing happened and when I looked at the processes by ctrl/alt/del-ing it wasn't listed.
-
Re: "This computer's going to sleep after 3 minutes"
Quote:
Originally Posted by krtxmrtz
I did but the program didn't seem to be running. It was correctly placed in the registry, I opened it with regedit and it was there all right, but nothing happened and when I looked at the processes by ctrl/alt/del-ing it wasn't listed.
Have you rebooted the system?
That key makes programs launch at system startup. So if you have not yet rebooted your system yet you won't feel the difference.
Pradeep :)
-
Re: "This computer's going to sleep after 3 minutes"
hi,
Does she use the PC for internet access only? if so you can restrict access times via the router( if you use one) in most cases, this may be a better way of managing this issue. as a father of an 11 year old i believe a telling off may be the way forward as already stated.
I find that kids are better at getting round these types of things than we are at creating them, I certianly was.
good luck.
David
-
Re: Added difficulty
Quote:
Originally Posted by krtxmrtz
...I've just realized that she may ctrl/alt/del and close the application in this way or kill the process...
You can prevent that with an "Access Denied" message:- http://www.vbforums.com/showthread.php?t=409179
(The little zip there of a .reg file I made has had 20,361 :eek: downloads - or so the forum page says :confused: )
-
Re: "This computer's going to sleep after 3 minutes"
Another suggestion:
Create 2 simple vbs files.
1. The first one contains only a MsgBox to say "Your PC will be shutdown in 3 minutes." Don't care she will close it or not.
2. The second one contains the Shell command "Shutdown /s"
Use Task Scheduler, add:
1. First task runs at 10:00 PM daily to run the first vbs file.
2. Second task runs at 10:03 PM daily to run the second vbs file.
Does this work?
Assumed that she doesn't know how to use task scheduler to cancel a task.
This way her PC won't waste resource to run an App in background with a Timer and prevent her to use Task Manager to kill that App or process.
-
Re: "This computer's going to sleep after 3 minutes"
Quote:
Originally Posted by Pradeep1210
Have you rebooted the system?
That key makes programs launch at system startup. So if you have not yet rebooted your system yet you won't feel the difference.
Pradeep :)
Yes I have.
-
Re: "This computer's going to sleep after 3 minutes"
Quote:
Originally Posted by anhn
Another suggestion:
Create 2 simple vbs files.
1. The first one contains only a MsgBox to say "Your PC will be shutdown in 3 minutes." Don't care she will close it or not.
2. The second one contains the Shell command "Shutdown /s"...
This seems easy enough only how do you shell in a vb file? I think there's no shell command.
-
Re: "This computer's going to sleep after 3 minutes"
Quote:
Originally Posted by Davadvice
hi,
Does she use the PC for internet access only? if so you can restrict access times via the router( if you use one) in most cases, this may be a better way of managing this issue. as a father of an 11 year old i believe a telling off may be the way forward as already stated.
I find that kids are better at getting round these types of things than we are at creating them, I certianly was.
good luck.
David
This option is interesting, but I don't easily find my way around the router configuration. Could you provide a few more details?
-
Re: "This computer's going to sleep after 3 minutes"
he didn't say vb file (which DOES in fact have shell) he said vbs which is visual basic scripting engine. It is similar to vb. Anyway, if you make your program have the same .exe name as a required process she won't know what to delete, if anything. And i think there's some way of making it in the backed up system files so if she ever deletes it the OS restores it automatically.