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 TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

API shutdown problem

Status
Not open for further replies.

scrano

Technical User
Jan 24, 2001
51
AU
Hi I got this code from a web site and I don't know a lot about API calls at all and I'm having a bit of a problem with it, can any of you guys please help. Basically I'm not real real sure what argument I should be using when I call the function. When I try what I thought was right all it does is shut the system down, when I should be able to choose between Shut Down, Restart, Force Log Off and so on. The other thing is that it doesn't actually power the computer off. It just comes up with a message saying that "The computer can now be switched off" or something like that. I'm using W2K pro by the way if that makes any difference. Any help anyone can give me I would very much appreciate. Thanx in advance.Anyways here's the code:

Private Type LUID
LowPart As Long
HighPart As Long
End Type

Private Type TOKEN_PRIVILEGES
PrivilegeCount As Long
LuidUDT As LUID
Attributes As Long
End Type

Const TOKEN_ADJUST_PRIVILEGES = &H20
Const TOKEN_QUERY = &H8
Const SE_PRIVILEGE_ENABLED = &H2

Const EWX_SHUTDOWN = 1
Const EWX_REBOOT = 2
Const EWX_FORCE = 4

Private Declare Function GetVersion Lib "kernel32" () 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 Any, ReturnLength As Any) As Long
Private Declare Function ExitWindowsEx Lib "user32" (ByVal dwOptions As Long, _
ByVal dwReserved As Long) As Long

' Shut down windows, and optional reboot it
' if the 2nd argument is True, no WM_QUERYENDSESSION and WM_ENDSESSION
' messages are sent to active applications

Sub ShutDownWindows(ByVal Reboot As Boolean, Optional ByVal Force As Boolean)
Dim hToken As Long
Dim tp As TOKEN_PRIVILEGES
Dim flags As Long

' Windows NT/2000 require a special treatment
' to ensure that the calling process has the
' privileges to shut down the system

' under NT the high-order bit (that is, the sign bit)
' of the value retured by GetVersion is cleared
If GetVersion() >= 0 Then
' Open this process for adjusting its privileges
OpenProcessToken GetCurrentProcess(), (TOKEN_ADJUST_PRIVILEGES Or _
TOKEN_QUERY), hToken

' Get the LUID for shutdown privilege.
' retrieves the locally unique identifier (LUID) used
' to locally represent the specified privilege name
' (first argument = "" means the local system)
LookupPrivilegeValue "", "SeShutdownPrivilege", tp.LuidUDT

' complete the TOKEN_PRIVILEGES structure with the # of
' privileges and the desired attribute
tp.PrivilegeCount = 1
tp.Attributes = SE_PRIVILEGE_ENABLED

' enables or disables privileges in the specified access token
' last 3 arguments are zero because we aren't interested
' in previous privilege attributes.
AdjustTokenPrivileges hToken, False, tp, 0, ByVal 0&, ByVal 0&
End If

' prepare shutdown flags
flags = EWX_SHUTDOWN
If Reboot Then flags = flags Or EWX_REBOOT
If Force Then flags = flags Or EWX_FORCE

' finally, you can shut down Windows
ExitWindowsEx flags, &HFFFF

End Sub


 
Well i tried it and managed to restart (in windows 2000 Server)

ShutDownWindows True, True

As for removing the main screen saying you may now switch off i dont know

Does it show this message when you shut down normally???
 
I may be wrong on this, but I belive it will be a bios setting. There is a setting somewhere in there that says if the OS can powerdown the PC or not. it may be that this isn't set. I know this was a problem I had with 98 a while ago. This is assuming the message you get is 'It is now safe to turn off your computer'. If this isn't the kind of message you are getting ignore me! [smile]

If it is, you may need a bios update.

Hope this helps

Matt
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top