Attribute VB_Name = "hlp_ExitWindowsExXP"
' Copyright (C) 2004-2006 Jan Vorel
' This program is free software; you can redistribute it and/or modify
' it under the terms of the GNU General Public License as published by
' the Free Software Foundation; either version 2 of the License, or
' (at your option) any later version.

' This program is distributed in the hope that it will be useful,
' but WITHOUT ANY WARRANTY; without even the implied warranty of
' MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
' GNU General Public License for more details.

' You should have received a copy of the GNU General Public License
' along with this program; if not, write to the Free Software
' Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

' If you have any questions, suggestions or bug reports about this product,
' contact me: info@ctuser.net; Do not contact me about general B.A.S.I.C.
' language or A.P.I. issues

Option Explicit
Option Base 0

Private Type LARGE_INTEGER
  lowpart As Long
  highpart As Long
  End Type

Private Type LUID_AND_ATTRIBUTES
  pLuid As LARGE_INTEGER
  Attributes As Long
  End Type

Private Type TOKEN_PRIVILEGES
  PrivilegeCount As Long
  Privileges(1) As LUID_AND_ATTRIBUTES
  End Type

Private Type OSVERSIONINFO
  dwOSVersionInfoSize As Long
  dwMajorVersion As Long
  dwMinorVersion As Long
  dwBuildNumber As Long
  dwPlatformId As Long
  szCSDVersion As String * 128
  End Type

Private Declare Function LookupPrivilegeValueA Lib "advapi32.dll" (ByVal lpSystemName As String, ByVal lpName As String, lpLuid As LARGE_INTEGER) As Long
Private Declare Function GetCurrentProcess Lib "kernel32" () As Long
Private Declare Function AdjustTokenPrivileges Lib "advapi32.dll" (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.dll" (ByVal ProcessHandle As Long, ByVal DesiredAccess As Long, TokenHandle As Long) As Long
Private Declare Function ExitWindowsEx Lib "user32" (ByVal uFlags As Long, ByVal dwReserved As Long) As Long
Private Declare Function GetVersionExA Lib "kernel32" (lpVersionInformation As OSVERSIONINFO) As Long

Public Function ExitWindowsExXP() As Boolean
  Dim xLong1 As Long
  Dim xLong2 As Long
  Dim xType1 As OSVERSIONINFO
  Dim xType2 As TOKEN_PRIVILEGES
  Dim xType3 As TOKEN_PRIVILEGES
  
  xType1.dwOSVersionInfoSize = Len(xType1)
  If Not GetVersionExA(xType1) = 0& Then
    If (xType1.dwPlatformId = &H2&) Then
      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
    End If
'  ExitWindowsExXP = Not (ExitWindowsEx(1&, 0&) = 0&)
  ExitWindowsExXP = Not (ExitWindowsEx(9&, 0&) = 0&)
  'Stop
  'ExitWindowsExXP = True
  End Function

