home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Source Code / Visual Basic Source Code.iso / vbsource / howtor_1 / shutdown.bas < prev    next >
Encoding:
BASIC Source File  |  1999-08-20  |  2.9 KB  |  91 lines

  1. Attribute VB_Name = "Shutdown"
  2. Option Explicit
  3.  
  4. '-----CONSTANT DECLARATION-----
  5. Public Const EWX_LogOff As Long = 0
  6. Public Const EWX_SHUTDOWN As Long = 1
  7. Public Const EWX_REBOOT As Long = 2
  8. Public Const EWX_FORCE As Long = 4
  9. Public Const EWX_POWEROFF As Long = 8
  10. Public Const mlngWindows95 = 0
  11. Public Const mlngWindowsNT = 1
  12.  
  13. '-----FUNCTION DECLARATION-----
  14. Public Declare Function ExitWindowsEx Lib "user32" (ByVal dwOptions As Long, ByVal dwReserved As Long) As Long
  15. Public Declare Function GetVersion Lib "kernel32" () As Long
  16.  
  17. Private Declare Function GetLastError Lib "kernel32" () As Long
  18. Private Declare Function GetCurrentProcess Lib "kernel32" () As Long
  19. Private Declare Function OpenProcessToken Lib "advapi32" (ByVal ProcessHandle As Long, ByVal DesiredAccess As Long, TokenHandle As Long) As Long
  20. Private Declare Function LookupPrivilegeValue Lib "advapi32" Alias "LookupPrivilegeValueA" (ByVal lpSystemName As String, ByVal lpName As String, lpLuid As LUID) As Long
  21. 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
  22.  
  23. '-----TYPE DECLARATIONS-----
  24. Private Type LUID
  25.     UsedPart As Long
  26.     IgnoredForNowHigh32BitPart As Long
  27. End Type
  28.  
  29. Private Type LUID_AND_ATTRIBUTES
  30.     TheLuid As LUID
  31.     Attributes As Long
  32. End Type
  33.  
  34. Private Type TOKEN_PRIVILEGES
  35.     PrivilegeCount As Long
  36.     TheLuid As LUID
  37.     Attributes As Long
  38. End Type
  39.  
  40. '-----MISC. DECLARATIONS-----
  41. Public glngWhichWindows32 As Long
  42. Private Declare Sub SetLastError Lib "kernel32" (ByVal dwErrCode As Long)
  43.          
  44.  
  45. Public Sub AdjustToken()
  46.  
  47.     Const TOKEN_ADJUST_PRIVILEGES = &H20
  48.     Const TOKEN_QUERY = &H8
  49.     Const SE_PRIVILEGE_ENABLED = &H2
  50.  
  51.     Dim hdlProcessHandle As Long
  52.     Dim hdlTokenHandle As Long
  53.     Dim tmpLuid As LUID
  54.     Dim tkp As TOKEN_PRIVILEGES
  55.     Dim tkpNewButIgnored As TOKEN_PRIVILEGES
  56.     Dim lBufferNeeded As Long
  57.  
  58.     SetLastError 0
  59.  
  60.     hdlProcessHandle = GetCurrentProcess()
  61.  
  62.     If GetLastError <> 0 Then
  63.         MsgBox "GetCurrentProcess error==" & GetLastError
  64.     End If
  65.  
  66.     OpenProcessToken hdlProcessHandle, _
  67.         (TOKEN_ADJUST_PRIVILEGES Or TOKEN_QUERY), hdlTokenHandle
  68.  
  69.     If GetLastError <> 0 Then
  70.         MsgBox "OpenProcessToken error==" & GetLastError
  71.     End If
  72.  
  73.     LookupPrivilegeValue "", "SeShutdownPrivilege", tmpLuid
  74.  
  75.     If GetLastError <> 0 Then
  76.         MsgBox "LookupPrivilegeValue error==" & GetLastError
  77.     End If
  78.  
  79.     tkp.PrivilegeCount = 1
  80.     tkp.TheLuid = tmpLuid
  81.     tkp.Attributes = SE_PRIVILEGE_ENABLED
  82.  
  83.     AdjustTokenPrivileges hdlTokenHandle, False, tkp, Len(tkpNewButIgnored), tkpNewButIgnored, lBufferNeeded
  84.                                
  85.     If GetLastError <> 0 Then
  86.         MsgBox "AdjustTokenPrivileges error==" & GetLastError
  87.     End If
  88.  
  89. End Sub
  90.  
  91.