Attribute VB_Name = "modShutdown"

Option Explicit

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 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 GetCurrentProcess Lib "kernel32" () As Long
Private Declare Function GetEnvironmentVariable Lib "kernel32" Alias "GetEnvironmentVariableA" (ByVal lpName As String, ByVal lpBuffer As String, ByVal nSize As Long) As Long
Private Declare Function ExitWindowsEx Lib "user32" (ByVal uFlags As Long, ByValdwReserved As Long) As Long

Const ANYSIZE_ARRAY = 1

Type LUID
    LowPart As Long
    HighPart As Long
End Type

Type LUID_AND_ATTRIBUTES
    attrLUID As LUID
    Attributes As Long
End Type

Type TOKEN_PRIVILEGES
    PrivilegeCount As Long
    Privileges(ANYSIZE_ARRAY) As LUID_AND_ATTRIBUTES
End Type

Public Sub Shutdown()
    Dim getVar As String * 500
    GetEnvironmentVariable "OS", getVar, 500
    If (Left$(getVar, 10) = "Windows_NT") Then Shutdown_XP Else Shutdown_9x
End Sub

Public Sub Shutdown_XP()
    Dim MyProc As Long
    MyProc = GetCurrentProcess()

    Dim MyToken As Long
    OpenProcessToken MyProc, &H8 Or &H20, MyToken

    Dim MyLUID As LUID
    LookupPrivilegeValue "", "SeShutdownPrivilege", MyLUID

    Dim MyTkPriv As TOKEN_PRIVILEGES
    MyTkPriv.PrivilegeCount = 1
    MyTkPriv.Privileges(0).Attributes = &H2
    MyTkPriv.Privileges(0).attrLUID = MyLUID

    Dim DontCare As TOKEN_PRIVILEGES

    AdjustTokenPrivileges MyToken, False, MyTkPriv, Len(MyTkPriv), DontCare, Len(DontCare)

    ExitWindowsEx &H4 Or &H8, 0
End Sub

Public Sub Shutdown_9x()
    ExitWindowsEx &H4 Or &H8, 0
End Sub