Results 1 to 2 of 2

Thread: auto shutdown winnt thru vb code

  1. #1

    Thread Starter
    Member
    Join Date
    Feb 2000
    Posts
    57

    auto shutdown winnt thru vb code

    hi,

    is there any way to shutdown winnt at a specified time from vb code.

    please
    help

    thanks

    bye.

  2. #2
    Black Cat JoshT's Avatar
    Join Date
    Nov 2000
    Location
    WNY, USA
    Posts
    4,032
    VB Code:
    1. Option Explicit
    2.  
    3. Private Const TOKEN_ADJUST_PRIVILEGES = &H20
    4. Private Const TOKEN_QUERY = &H8
    5. Private Const SE_PRIVILEGE_ENABLED = &H2
    6. Private Const ANYSIZE_ARRAY = 1
    7.  
    8. Private Type LUID
    9.     LowPart As Long
    10.     HighPart As Long
    11. End Type
    12. Private Type LUID_AND_ATTRIBUTES
    13.     pLuid As LUID
    14.     Attributes As Long
    15. End Type
    16. Private Type TOKEN_PRIVILEGES
    17.     PrivilegeCount As Long
    18.     Privileges(ANYSIZE_ARRAY) As LUID_AND_ATTRIBUTES
    19. End Type
    20.  
    21. Private Declare Function GetCurrentProcess Lib "kernel32" () As Long
    22. Private Declare Function OpenProcessToken Lib "advapi32" (ByVal ProcessHandle As Long, ByVal DesiredAccess As Long, TokenHandle As Long) As Long
    23. Private Declare Function LookupPrivilegeValue Lib "advapi32" Alias "LookupPrivilegeValueA" (ByVal lpSystemName As String, ByVal lpName As String, lpLuid As LUID) As Long
    24. 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
    25.  
    26. 'my stuff to shut down windows.  needed to use NT's security
    27. Private Enum SHUTDOWNFLAG
    28.     EWX_LOGOFF = 0
    29.     EWX_SHUTDOWN = 1
    30.     EWX_REBOOT = 2
    31.     EWX_FORCE = 4
    32.     EWX_POWEROFF = 8
    33. End Enum
    34. Private Declare Function ExitWindowsEx Lib "user32" (ByVal uFlags As SHUTDOWNFLAG, ByVal dwReserved As Long) As Long
    35.  
    36. Private Sub EnableShutDown()
    37.     Dim hProc As Long
    38.     Dim hToken As Long
    39.     Dim mLUID As LUID
    40.     Dim mPriv As TOKEN_PRIVILEGES
    41.     Dim mNewPriv As TOKEN_PRIVILEGES
    42.     hProc = GetCurrentProcess()
    43.     OpenProcessToken hProc, TOKEN_ADJUST_PRIVILEGES + TOKEN_QUERY, hToken
    44.     LookupPrivilegeValue "", "SeShutdownPrivilege", mLUID
    45.     mPriv.PrivilegeCount = 1
    46.     mPriv.Privileges(0).Attributes = SE_PRIVILEGE_ENABLED
    47.     mPriv.Privileges(0).pLuid = mLUID
    48.     ' enable shutdown privilege for the current application
    49.     AdjustTokenPrivileges hToken, False, mPriv, 4 + (12 * mPriv.PrivilegeCount), mNewPriv, 4 + (12 * mNewPriv.PrivilegeCount)
    50. End Sub
    51.  
    52. Private Sub cmdLogoff_Click()
    53.     Dim retval As Long
    54.     retval = ExitWindowsEx(EWX_LOGOFF, 0)
    55.     Unload Me
    56. End Sub
    57.  
    58. Private Sub cmdPoweroff_Click()
    59.     Dim retval As Long
    60.     retval = ExitWindowsEx(EWX_POWEROFF, 0)
    61.     Unload Me
    62. End Sub
    63.  
    64. Private Sub cmdReboot_Click()
    65.     Dim retval As Long
    66.     retval = ExitWindowsEx(EWX_REBOOT, 0)
    67.     Unload Me
    68. End Sub
    69.  
    70. Private Sub cmdShutdown_Click()
    71.     Dim retval As Long
    72.     retval = ExitWindowsEx(EWX_SHUTDOWN, 0)
    73.     Unload Me
    74. End Sub
    75.  
    76. Private Sub Form_Load()
    77.     EnableShutDown
    78. End Sub
    Josh
    Get these: Mozilla Opera OpenBSD
    I have books for sale: "MCSD in a Nutshell" and "VB Distributed Exam Cram" - PM me for details. Will also trade for a decent ATX Pentium 2 MB/CPU/RAM combo.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width