Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How can I turn the computer off by a command? 1

Status
Not open for further replies.

SurvivorTiger

Programmer
Jul 9, 2002
265
0
0
US
I want to have a command button that when you click on it, it turns the computer off, and if possible I don't even want that screen that appears and asks you if you want to restart or shut down or hibernate, I want the computer to shut down with out the user seeing the diolog and choosing shut down.How can I do this?
Thanks in advance
 
When you want your VB app to force a Windows computer to shut down, restart, or log off, you can use the ExitWindowsEx API. Read about Windows API

'Api function and the constants required for ExitWindowsEx
Declare Function ExitWindowsEx& Lib "user32" (ByVal uFlags As Long, _
ByVal dwReserved As Long)

Public Const EWX_FORCE = 4
Public Const EWX_LOGOFF = 0
Public Const EWX_REBOOT = 2
Public Const EWX_SHUTDOWN = 1

Private Sub cmdShutdown_Click()

Dim MsgRes As Long

'Make sure that the user really want to shutdown
MsgRes = MsgBox("Are you sure you want to Shut Down Windows 95?", vbYesNo Or vbQuestion)

'If the user selects no, exit this sub
If MsgRes = vbNo Then Exit Sub

'else, shutdown windows and unload this form
Call ExitWindowsEx(EWX_SHUTDOWN, 0)
Unload Me

End Sub



AYh8DisJob [morning]

"Ev erhav eo neof thosed ayswh ennot hinggo esr ight?"
 
I got a compile error that said I couldn't set those things as public, so I changed it to Private, but now it's givving me the same compile error and it's highlighting the first Decleration, but I don't know how to change that one so it would work.
Thanks for helping
 
SurvivorTiger,
OK...ok I think that is too difficult right but the trick here is to copy this part to a Module in your VB project...
called it Module1.bas
'Api function and the constants required for ExitWindowsEx
Declare Function ExitWindowsEx& Lib "user32" (ByVal uFlags As Long, _
ByVal dwReserved As Long)

Public Const EWX_FORCE = 4
Public Const EWX_LOGOFF = 0
Public Const EWX_REBOOT = 2
Public Const EWX_SHUTDOWN = 1

Then Create a Form let say form1.frm and copy this code with a button name cmdShutdown.....

Private Sub cmdShutdown_Click()
Dim MsgRes As Long
'Make sure that the user really want to shutdown
MsgRes = MsgBox("Are you sure you want to Shut Down Windows 95?", vbYesNo Or vbQuestion)

'If the user selects no, exit this sub
If MsgRes = vbNo Then Exit Sub

'else, shutdown windows and unload this form
Call ExitWindowsEx(EWX_SHUTDOWN, 0)
Unload Me

End Sub



If the above code still doesnt work for you because its API then forget about Win API.... i suggest you use this simple code.

Private Sub cmdShutdown_Click()

Set colOperatingSystems = GetObject("winmgmts:{(Shutdown)}").ExecQuery("Select * from Win32_OperatingSystem")
For Each objOperatingSystem in colOperatingSystems
ObjOperatingSystem.Win32Shutdown(1)
Next

End Sub




[morning] AYh8DisJob


"God can turn any difficulty into an opportunity."
 
I tried API and it still wouldn't work, I geuss it's because i'm using XP and I don't think API works with XP.
But the second thing that you told me, did work and thank you very much!
 
Quick point: the second variant won't work on any machine without the WMI libraries installed, i.e anything earlier than W2000, as far as I can recall (although you can download the necessary WMI SDK for those earlier operating systems).
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top