[RESOLVED] Shutting down/standby/or hibernate windows with VB6
Hi everyone,
I have used this site once previously and you guys were able to figure it out really quickly so I hope I can get the same results this time. :bigyello: I wish I could give back help to this site but I doubt I'm in a position of expertise to do so. xD
Background:
I have already made a program that opens and closes web browsers to designated pages at on one minute intervals, kind of like a macro, (to get views, kind of like youtube views).
I have this program set up to read from a file that the user can edit to add or delete urls in the file and right now there are a quite a bit of urls. The program takes a bit of time to do everything it needs to do so I just let it run over night and turn of my computer in the morning.
Problem:
However, I would like to be able to make my program automatically turn off/standby/hibernate when its done doing its thang.
I have looked around for a few hours and tried several coding samples such as
Code:
#If Win32 Then
Private Declare Function Shutdown Lib "user32" Alias _
"ExitWindowsEx" (ByVal uFlags As Long, ByVal _
dwReserved As Long) As Long
Private Const EWX_LOGOFF = 0
Private Const EWX_SHUTDOWN = 1
Private Const EWX_REBOOT = 2
Private Const EWX_FORCE = 4
#Else
Private Declare Function Shutdown Lib "User" Alias _
"ExitWindows" (ByVal dwReturnCode As Long, ByVal _
wReserved As Integer) As Integer
Private Const EW_REBOOTSYSTEM = &H43
Private Const EW_RESTARTWINDOWS = &H42
#End If
Private SelectedOption As Integer
Private Sub Form_Load()
#If Win32 Then
' Prepare for 32 bit ExitWindowsEx.
optExitOption(0).Caption = "Normal Shutdown"
optExitOption(0).Tag = EWX_SHUTDOWN
optExitOption(1).Caption = "Reboot"
optExitOption(1).Tag = EWX_REBOOT
optExitOption(2).Caption = "Log Off"
optExitOption(2).Tag = EWX_LOGOFF
optExitOption(3).Caption = "Forced Shutdown"
optExitOption(3).Tag = EWX_FORCE
#Else
' Prepare for 16 bit ExitWindows.
optExitOption(0).Caption = "Normal Shutdown"
optExitOption(0).Tag = 0
optExitOption(1).Caption = "Reboot"
optExitOption(1).Tag = EW_REBOOTSYSTEM
optExitOption(2).Caption = "Restart Windows"
optExitOption(2).Tag = EW_RESTARTWINDOWS
optExitOption(3).Visible = False
#End If
End Sub
Private Sub cmdOk_Click()
Dim exit_option As Long
exit_option = CLng(optExitOption(SelectedOption).Tag)
'MsgBox ("")
Shutdown exit_option, 0
End Sub
(which I believe I found on this site)
and
Code:
Private Sub Command1_Click()
Shell _
("Rundll32.exe user,_exitwindows")
End Sub
I am not too experienced in VB (mostly self taught) but I have managed to make something that can shell a program and i/o files and info so its not like I'm learning how to use a command button to display "Hello World!" :p
Any help is greatly appreciated :)
Re: Shutting down/standby/or hibernate windows with VB6
the api should work fine
what is happening now, error or wrong result?
what value for exit_option when calling shutdown?
what value for selectedoption, or is it a function?
you could also try shelling out to shutdown.exe, system32 directory
Re: Shutting down/standby/or hibernate windows with VB6
Quote:
Originally Posted by
westconn1
the api should work fine
what is happening now, error or wrong result?
what value for exit_option when calling shutdown?
what value for selectedoption, or is it a function?
you could also try shelling out to shutdown.exe, system32 directory
For the second code, I got an error saying "error loading user the specified module could not be found"
For the first one, nothing would happen.
And there was another one that I tried that was similar to the second code that when I put a value of 0 (I think it was zero) my computer would log off right away. But supposedly there was another value (i think 4) for shut down (as well as others for restarting) that upon trying to use, nothing would happen.
I think the only reason the first code is so long is to that the user can decide if they want to shut down/restart/log off, etc.
Now about that shutdown thing in system32, would that ask the user to save open applications and stuff? I need the computer to just shut down right away after its done. Either that or put it into standby or hibernation so that the computer isnt just running all night.
I tried to make a timed shut down off the power options (as in Hibernate after 40 minutes) but in the morning my computer was still running so I don't really know what happened there. I think it was becaus a message box is desplayed at the end saying that the program is finished and the computer wasn't actually idle while that box was displayed so it never activated the "hibernate after 40 minutes" option
Thoughts?
Re: Shutting down/standby/or hibernate windows with VB6
Quote:
I think the only reason the first code is so long is to that the user can decide if they want to shut down/restart/log off, etc.
in that case try cutting all the options and just call shutdown with value of ewx_force
Re: Shutting down/standby/or hibernate windows with VB6
Quote:
Originally Posted by
westconn1
in that case try cutting all the options and just call shutdown with value of ewx_force
ok i simplified it down into this code:
Code:
Private Declare Function Shutdown Lib "user32" Alias _
"ExitWindowsEx" (ByVal uFlags As Long, ByVal _
dwReserved As Long) As Long
Option Explicit
Private Sub Command1_Click()
Shutdown 0, 0
End Sub
it works with values of "0, 0" (log off) and "4, 0" (force log off)
but nothing happens when i click it for values of "1, 0" (shut down) and "2, 0" (reboot)
:(
Re: Shutting down/standby/or hibernate windows with VB6
Quote:
but nothing happens when i click it for values of "1, 0" (shut down) and "2, 0" (reboot)
probably waiting for some user interaction, if unattended shutdown, use force
Re: Shutting down/standby/or hibernate windows with VB6
From the Microsoft documentation for ExitWindowsEx (http://msdn.microsoft.com/en-us/libr...(v=VS.85).aspx)
Quote:
To shut down or restart the system, the calling process must use the AdjustTokenPrivileges function to enable the SE_SHUTDOWN_NAME privilege. For more information, see Running with Special Privileges.
Even though the process is asynchronous, you ought to be checking the return value from the function.
Re: Shutting down/standby/or hibernate windows with VB6
While you're at it you may want to consider how Anti-Virus software will react to this call if you plan to deploy your app.
Re: Shutting down/standby/or hibernate windows with VB6
i kind of found out how to do it.
http://www.vbforums.com/showthread.php?t=396217
this link is to a program that hibernates and unhibernates the computer like an alarm clock - more or less what i wanted. I dont quite understand it but i guess i can try to tinker with it to suit my needs