|
-
Jul 31st, 2002, 04:59 AM
#1
Thread Starter
Addicted Member
[resolved]forcing a computer to shutdown
i have a mate writing a program which controls 4 fans within his computer case he needs and option where the computer shutdown if temperatures exceed a certain level.. any suggestions?
Last edited by Jack Daniels; Jul 31st, 2002 at 06:03 AM.
-
Jul 31st, 2002, 05:49 AM
#2
Addicted Member
Try this:
Code:
Option Explicit
Private Const EWX_SHUTDOWN As Long = 1
Private Const EWX_REBOOT As Long = 2
Private Const EWX_FORCE As Long = 4
Private Const EWX_POWEROFF As Long = 8
'The ExitWindowsEx function either logs off, shuts down, or shuts
'down and restarts the system.
Private Declare Function ExitWindowsEx Lib "user32" _
(ByVal dwOptions As Long, _
ByVal dwReserved As Long) As Long
'The GetLastError function returns the calling thread's last-error
'code value. The last-error code is maintained on a per-thread basis.
'Multiple threads do not overwrite each other's last-error code.
Private Declare Function GetLastError Lib "kernel32" () As Long
Private Const mlngWindows95 = 0
Private Const mlngWindowsNT = 1
Public glngWhichWindows32 As Long
'The GetVersion function returns the operating system in use.
Private Declare Function GetVersion Lib "kernel32" () As Long
Private Type LUID
UsedPart As Long
IgnoredForNowHigh32BitPart As Long
End Type
Private Type LUID_AND_ATTRIBUTES
TheLuid As LUID
Attributes As Long
End Type
Private Type TOKEN_PRIVILEGES
PrivilegeCount As Long
TheLuid As LUID
Attributes As Long
End Type
'The GetCurrentProcess function returns a pseudohandle for the
'current process.
Private Declare Function GetCurrentProcess Lib "kernel32" () As Long
'The OpenProcessToken function opens the access token associated with
'a process.
Private Declare Function OpenProcessToken Lib "advapi32" _
(ByVal ProcessHandle As Long, _
ByVal DesiredAccess As Long, _
TokenHandle As Long) As Long
'The LookupPrivilegeValue function retrieves the locally unique
'identifier (LUID) used on a specified system to locally represent
'the specified privilege name.
Private Declare Function LookupPrivilegeValue Lib "advapi32" _
Alias "LookupPrivilegeValueA" _
(ByVal lpSystemName As String, _
ByVal lpName As String, _
lpLuid As LUID) As Long
'The AdjustTokenPrivileges function enables or disables privileges
'in the specified access token. Enabling or disabling privileges
'in an access token requires TOKEN_ADJUST_PRIVILEGES access.
Private Declare Function AdjustTokenPrivileges Lib "advapi32" _
(ByVal TokenHandle As Long, _
ByVal DisableAllPrivileges As Long, _
NewState As TOKEN_PRIVILEGES, _
ByVal BufferLength As Long, _
PreviousState As TOKEN_PRIVILEGES, _
ReturnLength As Long) As Long
Private Declare Sub SetLastError Lib "kernel32" (ByVal dwErrCode As Long)
Private Sub AdjustToken()
'********************************************************************
'* This procedure sets the proper privileges to allow a log off or a
'* shut down to occur under Windows NT.
'********************************************************************
Const TOKEN_ADJUST_PRIVILEGES = &H20
Const TOKEN_QUERY = &H8
Const SE_PRIVILEGE_ENABLED = &H2
Dim hdlProcessHandle As Long
Dim hdlTokenHandle As Long
Dim tmpLuid As LUID
Dim tkp As TOKEN_PRIVILEGES
Dim tkpNewButIgnored As TOKEN_PRIVILEGES
Dim lBufferNeeded As Long
'Set the error code of the last thread to zero using the
'SetLast Error function. Do this so that the GetLastError
'function does not return a value other than zero for no
'apparent reason.
SetLastError 0
'Use the GetCurrentProcess function to set the hdlProcessHandle
'variable.
hdlProcessHandle = GetCurrentProcess()
' If GetLastError <> 0 Then
' systemMessage.AddItem "GetCurrentProcess Error==" & GetLastError
' End If
OpenProcessToken hdlProcessHandle, _
(TOKEN_ADJUST_PRIVILEGES Or TOKEN_QUERY), hdlTokenHandle
' If GetLastError <> 0 Then
' systemMessage.AddItem "OpenProcessToken Error==" & GetLastError
' End If
'Get the LUID for shutdown privilege
LookupPrivilegeValue "", "SeShutdownPrivilege", tmpLuid
' If GetLastError <> 0 Then
' systemMessage.AddItem "LookupPrivilegeValue Error==" & GetLastError
' End If
tkp.PrivilegeCount = 1 ' One privilege to set
tkp.TheLuid = tmpLuid
tkp.Attributes = SE_PRIVILEGE_ENABLED
'Enable the shutdown privilege in the access token of this process
AdjustTokenPrivileges hdlTokenHandle, _
False, _
tkp, _
Len(tkpNewButIgnored), _
tkpNewButIgnored, _
lBufferNeeded
' If GetLastError <> 0 Then
' systemMessage.AddItem "AdjustTokenPrivileges Error==" & GetLastError
' End If
End Sub
Private Sub cmdShutdown_Click()
If glngWhichWindows32 = mlngWindowsNT Then
AdjustToken
' systemMessage.AddItem "Post-AdjustToken GetLastError " & GetLastError
End If
ExitWindowsEx (EWX_SHUTDOWN), &HFFFF
' systemMessage.AddItem "ExitWindowsEx's GetLastError " & GetLastError
End Sub
Private Sub cmdForceShutdown_Click()
If glngWhichWindows32 = mlngWindowsNT Then
AdjustToken
' systemMessage.AddItem "Post-AdjustToken GetLastError " & GetLastError
End If
ExitWindowsEx (EWX_SHUTDOWN Or EWX_FORCE), &HFFFF
' systemMessage.AddItem "ExitWindowsEx's GetLastError " & GetLastError
End Sub
Public Sub Main()
'********************************************************************
'* When the project starts, check the operating system used by
'* calling the GetVersion function.
'********************************************************************
Dim lngVersion As Long
lngVersion = GetVersion()
If ((lngVersion And &H80000000) = 0) Then
glngWhichWindows32 = mlngWindowsNT
' systemType.Text = "Windows NT"
Else
glngWhichWindows32 = mlngWindows95
' systemType.Text = "Windows 95"
End If
Call cmdForceShutdown_Click
End
End Sub
-
Jul 31st, 2002, 05:50 AM
#3
-= B u g S l a y e r =-
-
Jul 31st, 2002, 05:50 AM
#4
Addicted Member
Use the ExitWindowsEx API. Explanation on this site:
http://216.26.168.92/vbapi/ref/e/exitwindowsex.html
"Computers are incredibly fast, accurate and stupid. Human beings are incredibly slow, inaccurate and brilliant. Together they are powerful beyond imagination." - Albert Einstein
-
Jul 31st, 2002, 05:52 AM
#5
New Member
forcing a computer to shutdown
There are some simple APIs that can do that for you. All you have to do is call one of them. For example:
public sub blah blah....()etc.
Call ExitWindowsEx(EWX_SHUTDOWN, 0)
end sub
Hope it helps
Bogdan
-
Jul 31st, 2002, 05:58 AM
#6
Thread Starter
Addicted Member
-
Jul 31st, 2002, 07:46 AM
#7
Thread Starter
Addicted Member
sorry
i know i edited it to resolved, and it works but on winxp, it shutdown to logon menu thingy only? this doesn't matter muh but if someone knows how to shut down the computer completely with XP im open to suggestions thanks for all the help.
-
Jul 31st, 2002, 07:48 AM
#8
-= B u g S l a y e r =-
-
Jul 31st, 2002, 07:50 AM
#9
Thread Starter
Addicted Member
yeh i tried that and the value 4 which is the same thing isn't it??
i think it's got something to do with XP cause it worked on 98...
-
Jul 31st, 2002, 08:01 AM
#10
-= B u g S l a y e r =-
mmm....
what happens if you add
Private Const EWX_POWEROFF As Long = 8
and uses that together with EWX_SHUTDOWN and EWX_FORCE
like this
EWX_POWEROFF Or EWX_SHUTDOWN Or EWX_FORCE
-
Jul 31st, 2002, 08:17 AM
#11
Thread Starter
Addicted Member
hmmm no go :-( um there was a varible reserved for future versions of windows, which XP is, do you think thats been employed or something?
-
Jul 31st, 2002, 08:23 AM
#12
-= B u g S l a y e r =-
hmmm don't know... never used this meself 
found this in the MSDN... might be worth a reading 
MSDN
PRB: ExitWindowsEx API Does Not Reboot Windows NT
Q176695
--------------------------------------------------------------------------------
The information in this article applies to:
Microsoft Visual Basic Learning, Professional, and Enterprise Editions for Windows, versions 5.0, 6.0
Microsoft Visual Basic Standard, Professional, and Enterprise Editions, 32-bit only, for Windows, version 4.0
--------------------------------------------------------------------------------
SYMPTOMS
When you use the ExitWindowsEx API to reboot the system under Windows NT and Windows 2000, the machine does not reboot.
CAUSE
In order to programmatically reboot a Windows NT or Windows 2000 system, the process requires the SE_SHUTDOWN_NAME privilege. By default, Visual Basic applications do not have this privilege and therefore will not reboot the machine.
RESOLUTION
In order to get ExitWindowsEx API to reboot the system under Windows NT or Windows 2000, the SE_SHUTDOWN_NAME privilege must be set. The following steps describe how to get the ExitWindowsEx API to work under Windows NT and Windows 2000.
Step By Step Example
Create a new standard EXE in Visual Basic. Form1 is created by default.
View the code for Form1. In the Declarations section, add the following code:
VB Code:
Private Type LUID
UsedPart As Long
IgnoredForNowHigh32BitPart As Long
End Type
Private Type TOKEN_PRIVILEGES
PrivilegeCount As Long
TheLuid As LUID
Attributes As Long
End Type
Private Const EWX_SHUTDOWN As Long = 1
Private Const EWX_FORCE As Long = 4
Private Const EWX_REBOOT = 2
Private Declare Function ExitWindowsEx Lib "user32" (ByVal _
dwOptions As Long, ByVal dwReserved As Long) As Long
Private Declare Function GetCurrentProcess Lib "kernel32" () As Long
Private Declare Function OpenProcessToken Lib "advapi32" (ByVal _
ProcessHandle As Long, _
ByVal DesiredAccess As Long, TokenHandle As Long) As Long
Private Declare Function LookupPrivilegeValue Lib "advapi32" _
Alias "LookupPrivilegeValueA" _
(ByVal lpSystemName As String, ByVal lpName As String, lpLuid _
As LUID) As Long
Private Declare Function AdjustTokenPrivileges Lib "advapi32" _
(ByVal TokenHandle As Long, _
ByVal DisableAllPrivileges As Long, NewState As TOKEN_PRIVILEGES _
, ByVal BufferLength As Long, _
PreviousState As TOKEN_PRIVILEGES, ReturnLength As Long) As Long
'Add a procedure called AdjustToken with the following code:
Private Sub AdjustToken()
Const TOKEN_ADJUST_PRIVILEGES = &H20
Const TOKEN_QUERY = &H8
Const SE_PRIVILEGE_ENABLED = &H2
Dim hdlProcessHandle As Long
Dim hdlTokenHandle As Long
Dim tmpLuid As LUID
Dim tkp As TOKEN_PRIVILEGES
Dim tkpNewButIgnored As TOKEN_PRIVILEGES
Dim lBufferNeeded As Long
hdlProcessHandle = GetCurrentProcess()
OpenProcessToken hdlProcessHandle, (TOKEN_ADJUST_PRIVILEGES Or _
TOKEN_QUERY), hdlTokenHandle
' Get the LUID for shutdown privilege.
LookupPrivilegeValue "", "SeShutdownPrivilege", tmpLuid
tkp.PrivilegeCount = 1 ' One privilege to set
tkp.TheLuid = tmpLuid
tkp.Attributes = SE_PRIVILEGE_ENABLED
' Enable the shutdown privilege in the access token of this process.
AdjustTokenPrivileges hdlTokenHandle, False, _
tkp, Len(tkpNewButIgnored), tkpNewButIgnored, lBufferNeeded
End Sub
'Add a CommandButton to the form. In the Click event, add the following code:
Private Sub Command1_Click()
AdjustToken
ExitWindowsEx (EWX_SHUTDOWN Or EWX_FORCE Or EWX_REBOOT), &HFFFF
End Sub
Save the project and build an executable. When you run the executable and click the CommandButton the computer will reboot as expected.
-
Jul 31st, 2002, 08:29 AM
#13
Thread Starter
Addicted Member
ah thankyou muchly :-) um i think that was included in what the first guys reply said but it makes so much more sense now.
very annoying function to debug ! esp. when your chatting with 10 ppl or so.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|