home *** CD-ROM | disk | FTP | other *** search
/ Planet Source Code Jumbo …e CD Visual Basic 1 to 7 / 5_2007-2008.ISO / data / Zips / TimerLogic2054203162007.psc / clsExitWindows.cls < prev    next >
Text File  |  2006-06-25  |  4KB  |  120 lines

  1. VERSION 1.0 CLASS
  2. BEGIN
  3.   MultiUse = -1  'True
  4.   Persistable = 0  'NotPersistable
  5.   DataBindingBehavior = 0  'vbNone
  6.   DataSourceBehavior  = 0  'vbNone
  7.   MTSTransactionMode  = 0  'NotAnMTSObject
  8. END
  9. Attribute VB_Name = "clsExitWindows"
  10. Attribute VB_GlobalNameSpace = False
  11. Attribute VB_Creatable = True
  12. Attribute VB_PredeclaredId = False
  13. Attribute VB_Exposed = False
  14. Option Explicit
  15.  
  16. 'APIs
  17. '******************************************************************************
  18. Private Declare Function ExitWindowsEx Lib "user32" (ByVal uFlags As Long, ByVal dwReserved As Long) As Long
  19. Private Declare Function GetCurrentProcess Lib "kernel32" () As Long
  20. Private Declare Function OpenProcessToken Lib "advapi32" (ByVal _
  21.    ProcessHandle As Long, _
  22.    ByVal DesiredAccess As Long, TokenHandle As Long) As Long
  23. Private Declare Function LookupPrivilegeValue Lib "advapi32" _
  24.    Alias "LookupPrivilegeValueA" _
  25.    (ByVal lpSystemName As String, ByVal lpName As String, lpLuid _
  26.    As LUID) As Long
  27. Private Declare Function AdjustTokenPrivileges Lib "advapi32" _
  28.    (ByVal TokenHandle As Long, _
  29.    ByVal DisableAllPrivileges As Long, NewState As TOKEN_PRIVILEGES _
  30.    , ByVal BufferLength As Long, _
  31. PreviousState As TOKEN_PRIVILEGES, ReturnLength As Long) As Long
  32. '******************************************************************************
  33.  
  34. 'Constants
  35. '******************************************************************************
  36. Private Const EWX_FORCE As Long = 4
  37. '******************************************************************************
  38.  
  39. 'Types
  40. '******************************************************************************
  41. Private Type LUID
  42.    UsedPart As Long
  43.    IgnoredForNowHigh32BitPart As Long
  44. End Type
  45.  
  46. Private Type TOKEN_PRIVILEGES
  47.   PrivilegeCount As Long
  48.   TheLuid As LUID
  49.   Attributes As Long
  50. End Type
  51. '******************************************************************************
  52.  
  53. 'Enumeration
  54. '******************************************************************************
  55. Public Enum EnumExitWindows
  56.  
  57.   WE_LOGOFF = 0
  58.   WE_SHUTDOWN = 1
  59.   WE_REBOOT = 2
  60.   WE_POWEROFF = 8
  61.  
  62. End Enum
  63. '******************************************************************************
  64.  
  65. 'Functions and Subs
  66. '******************************************************************************
  67. Private Sub AdjustToken()
  68.     Const TOKEN_ADJUST_PRIVILEGES = &H20
  69.     Const TOKEN_QUERY = &H8
  70.     Const SE_PRIVILEGE_ENABLED = &H2
  71.     Dim hdlProcessHandle As Long
  72.     Dim hdlTokenHandle As Long
  73.     Dim tmpLuid As LUID
  74.     Dim tkp As TOKEN_PRIVILEGES
  75.     Dim tkpNewButIgnored As TOKEN_PRIVILEGES
  76.     Dim lBufferNeeded As Long
  77.  
  78.     hdlProcessHandle = GetCurrentProcess()
  79.     OpenProcessToken hdlProcessHandle, (TOKEN_ADJUST_PRIVILEGES Or _
  80.        TOKEN_QUERY), hdlTokenHandle
  81.  
  82.  ' Get the LUID for shutdown privilege.
  83.     LookupPrivilegeValue "", "SeShutdownPrivilege", tmpLuid
  84.  
  85.     tkp.PrivilegeCount = 1    ' One privilege to set
  86.     tkp.TheLuid = tmpLuid
  87.     tkp.Attributes = SE_PRIVILEGE_ENABLED
  88.  
  89. ' Enable the shutdown privilege in the access token of this process.
  90.     AdjustTokenPrivileges hdlTokenHandle, False, _
  91.     tkp, Len(tkpNewButIgnored), tkpNewButIgnored, lBufferNeeded
  92.  
  93. End Sub
  94.  
  95.  
  96. Public Sub ExitWindows(ByVal aOption As EnumExitWindows)
  97.   
  98.   AdjustToken
  99.   
  100.   Select Case aOption
  101.     Case EnumExitWindows.WE_LOGOFF
  102.       ExitWindowsEx (EnumExitWindows.WE_LOGOFF Or EWX_FORCE), &HFFFF
  103.     Case EnumExitWindows.WE_REBOOT
  104.       ExitWindowsEx (EnumExitWindows.WE_SHUTDOWN Or EWX_FORCE Or EnumExitWindows.WE_REBOOT), &HFFFF
  105.     Case EnumExitWindows.WE_SHUTDOWN
  106.       ExitWindowsEx (EnumExitWindows.WE_SHUTDOWN Or EWX_FORCE), &HFFFF
  107.     Case EnumExitWindows.WE_POWEROFF
  108.       ExitWindowsEx (EnumExitWindows.WE_POWEROFF Or EWX_FORCE), &HFFFF
  109.   End Select
  110.  
  111. End Sub
  112. '******************************************************************************
  113.  
  114.  
  115.  
  116.  
  117.  
  118.  
  119.  
  120.