[API]
LARGE_INTEGER
LUID_AND_ATTRIBUTES
TOKEN_PRIVILEGES
LookupPrivilegeValueA
GetCurrentProcess
AdjustTokenPrivileges
OpenProcessToken
ExitWindowsEx
GetVersion
SetSystemPowerState
SendMessageA

[DEFINE]
Public Enum ShutDownWindowsEx_Mode
  SDWE_Shutdown = 1
  SDWE_Hibernate = 2
  SDWE_Standby = 3
  SDWE_MonitorsOff = 4
  End Enum

[CODE]
Public Function ShutdownWindowsEx(ByVal Mode As ShutDownWindowsEx_Mode) As Boolean
  Dim xLong1 As Long
  Dim xLong2 As Long
  Dim xType2 As TOKEN_PRIVILEGES
  Dim xType3 As TOKEN_PRIVILEGES
  
#If Unicode Then
  If IsUnicode Then
#Else
  If (GetVersion And &H80000000) = 0& Then
#End If
    If OpenProcessToken(GetCurrentProcess(), 40&, xLong2) Then
      xLong1 = LookupPrivilegeValueA(vbNullString, "SeShutdownPrivilege", xType2.Privileges(0).pLuid)
      xType2.PrivilegeCount = 1&
      xType2.Privileges(0).Attributes = 2&
      Call AdjustTokenPrivileges(xLong2, 0&, xType2, LenB(xType3), xType3, xLong1)
      End If
    End If
  Select Case Mode
    Case SDWE_Shutdown
      ShutdownWindowsEx = Not (ExitWindowsEx(9&, 0&) = 0&)
    Case SDWE_Hibernate
      On Error Resume Next
      ShutdownWindowsEx = SetSystemPowerState(1&, 0&)
      On Error GoTo 0
    Case SDWE_Standby
      On Error Resume Next
      ShutdownWindowsEx = SetSystemPowerState(0&, 0&)
      On Error GoTo 0
    Case SDWE_MonitorsOff
      ShutdownWindowsEx = SendMessageA(-1&, &H112&, &HF170&, byval 1&)
    End Select
  End Function
