Does any one can tell me which fuction call in VB can use to turn off the computer, thanks.
Printable View
Does any one can tell me which fuction call in VB can use to turn off the computer, thanks.
Originally from pavan:
Quote:
' In the declararations section
Const EWX_LOGOFF = 0
Const EWX_SHUTDOWN = 1
Const EWX_REBOOT = 2
Const EWX_FORCE = 4
Private Declare Function ExitWindowsEx Lib "user32" (ByVal uFlags As Long, ByVal dwReserved As Long) As Long
Private Sub Form_Unload()
msg = MsgBox("This program is going to reboot your computer. Press OK to continue or Cancel to stop.", vbCritical + vbOKCancel + 256, App.Title)
If msg = vbCancel Then End
'reboot the computer
ret& = ExitWindowsEx(EWX_FORCE Or EWX_REBOOT, 0)
End Sub
This code restarts the system
U can replace EWX_REBOOTwith EWX_SHUTDOWN to shutdown the system
Note that code works for 9x - NT you have to call security apis first.
Depending if your computer supports this, you can use EWX_POWEROFF to turn the power off (a complete shutdown).
Here is the constant.
VB Code:
Private Const EWX_POWEROFF = &H00000008
I haven't had success with the Poweroff on computers that don't support it. I had NT4 boxes that would reboot rather than display "It is now safe to turn off the computer" message.
The code works perfect, but i need a little explanation :
--------------------------------------------------------------------
Private Declare Function ExitWindowsEx Lib "user32" (ByVal uFlags As Long, ByVal dwReserved As Long) As Long
-------------------------------------------------------------------
what is "ExitWindowsEx Lib " and "user32"?
--------------------------------------------------------------------
ret& = ExitWindowsEx(EWX_FORCE Or EWX_REBOOT, 0)
------------------------------------------------------------------------
why "ret" have to with a "&"?
and what " EWX_FORCE " ,"EWX_REBOOT","0" these
parameter's meaning?
why i can not find any reference in VB about EWX_...?
How to apply this this stament?
Private Const EWX_POWEROFF = &H00000008
just change "Const EWX_SHUTDOWN = 1 " to "Const EWX_SHUTDOWN = &H00000008 " ? or change to "Const EWX_POWEROFF = &H00000008" ?
thanks
[quote]
what is "ExitWindowsEx Lib " and "user32"?
[quote]
ExitWindowsEx is the function name. User32 is the library it's stored in.
Ret& is not needed. You could easily useQuote:
why "ret" have to with a "&"?
and what " EWX_FORCE " ,"EWX_REBOOT","0" these
parameter's meaning?
ExitWindowsEx EWX_FORCE Or EWX_REBOOT, 0
EWX_FORCE and EWX_REBOOT are constants that tell ExitWindowsEx what operation to preform. 0 must be 0 as it is a reserved argument.
Because it's part of the Win32 API. See the API section of this site, or see www.vbapi.com for more information.Quote:
why i can not find any reference in VB about EWX_...?
To change to EWX_POWEROFF, modify your code so it looks like this:
ExitWindowsEx(EWX_FORCE Or EWX_POWEROFF, 0
what about to try Shell "rundll32 krnl386.exe,exitkernel"
PS: I think it works only on W9X.
PS: Save your work first.