Results 1 to 6 of 6

Thread: shutdown

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jan 2003
    Posts
    27

    shutdown

    are there functions to reboot, logoff, and shutdown a box in win 2k and xp

  2. #2
    Black Cat JoshT's Avatar
    Join Date
    Nov 2000
    Location
    WNY, USA
    Posts
    4,032
    Look up ExitWindowsEx or InitiateSystemShutdown.
    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.

  3. #3
    PowerPoster
    Join Date
    Aug 2000
    Location
    IN SILENCE
    Posts
    6,441
    Remaining quiet down here !!!

    BRAD HAS GIVEN ME THE ULTIMATIVE. I have chosen to stay....

  4. #4
    PowerPoster
    Join Date
    Aug 2000
    Location
    IN SILENCE
    Posts
    6,441
    Remaining quiet down here !!!

    BRAD HAS GIVEN ME THE ULTIMATIVE. I have chosen to stay....

  5. #5
    Lively Member
    Join Date
    May 2001
    Location
    UK
    Posts
    83
    This code correctly shutsdown the machine on NT/2k/XP, other code displays the "Your Computer is now safe to turn off" screen

    VB Code:
    1. 'Used by ShutdownWindowsNT
    2. Private Declare Function ExitWindowsEx Lib "User32" (ByVal uFlags As Long, ByVal dwReserved As Long) As Long
    3. Private Declare Function GetCurrentProcess Lib "kernel32" () As Long
    4. Private Declare Function OpenProcessToken Lib "advapi32" (ByVal _
    5.    ProcessHandle As Long, _
    6.    ByVal DesiredAccess As Long, TokenHandle As Long) As Long
    7. Private Declare Function LookupPrivilegeValue Lib "advapi32" _
    8.    Alias "LookupPrivilegeValueA" _
    9.    (ByVal lpSystemName As String, ByVal lpName As String, lpLuid _
    10.    As LUID) As Long
    11. Private Declare Function AdjustTokenPrivileges Lib "advapi32" _
    12.    (ByVal TokenHandle As Long, _
    13.    ByVal DisableAllPrivileges As Long, NewState As TOKEN_PRIVILEGES _
    14.    , ByVal BufferLength As Long, _
    15. PreviousState As TOKEN_PRIVILEGES, ReturnLength As Long) As Long
    16.  
    17. Private Const EWX_FORCE As Long = 4
    18. Private Type LUID
    19.    UsedPart As Long
    20.    IgnoredForNowHigh32BitPart As Long
    21. End Type
    22. Private Type TOKEN_PRIVILEGES
    23.   PrivilegeCount As Long
    24.   TheLuid As LUID
    25.   Attributes As Long
    26. End Type
    27. Public Enum EnumExitWindows
    28. WE_LOGOFF = 0
    29. WE_SHUTDOWN = 1
    30. WE_REBOOT = 2
    31. WE_POWEROFF = 8
    32. End Enum
    33.  
    34.  
    35.  
    36.  
    37. Public Function ShutdownWindowsNT(ShutdownType As Integer)
    38.  
    39. '1 = ATX power off
    40. '2 = Shutdown
    41. '3 = Restart
    42. '4 = Logoff
    43.  
    44.  
    45. '******* Adjust Priviliage for NT/Win2k/XP ********
    46.   Const TOKEN_ADJUST_PRIVILEGES = &H20
    47.   Const TOKEN_QUERY = &H8
    48.   Const SE_PRIVILEGE_ENABLED = &H2
    49.   Dim hdlProcessHandle As Long
    50.   Dim hdlTokenHandle As Long
    51.   Dim tmpLuid As LUID
    52.   Dim tkp As TOKEN_PRIVILEGES
    53.   Dim tkpNewButIgnored As TOKEN_PRIVILEGES
    54.   Dim lBufferNeeded As Long
    55.  
    56.   hdlProcessHandle = GetCurrentProcess()
    57.   OpenProcessToken hdlProcessHandle, (TOKEN_ADJUST_PRIVILEGES Or _
    58.      TOKEN_QUERY), hdlTokenHandle
    59.  
    60.   ' Get the LUID for shutdown privilege.
    61.   LookupPrivilegeValue "", "SeShutdownPrivilege", tmpLuid
    62.  
    63.   tkp.PrivilegeCount = 1    ' One privilege to set
    64.   tkp.TheLuid = tmpLuid
    65.   tkp.Attributes = SE_PRIVILEGE_ENABLED
    66.  
    67.   ' Enable the shutdown privilege in the access token of this process.
    68.   AdjustTokenPrivileges hdlTokenHandle, False, _
    69.      tkp, Len(tkpNewButIgnored), tkpNewButIgnored, lBufferNeeded
    70.  
    71.    
    72. If ShutdownType = 1 Then ExitWindowsEx (WE_POWEROFF Or EWX_FORCE), 0
    73. If ShutdownType = 2 Then ExitWindowsEx (WE_SHUTDOWN Or EWX_FORCE), 0
    74. If ShutdownType = 3 Then ExitWindowsEx (WE_REBOOT Or EWX_FORCE), 0
    75. If ShutdownType = 4 Then ExitWindowsEx (WE_LOGOFF Or EWX_FORCE), 0
    76.  
    77.  
    78. End Function

  6. #6
    Fanatic Member Ruku's Avatar
    Join Date
    Jul 2002
    Location
    Canada
    Posts
    655

    Question

    Would the code work for win98se?

    Originally posted by Spiv
    This code correctly shutsdown the machine on NT/2k/XP, other code displays the "Your Computer is now safe to turn off" screen

    VB Code:
    1. 'Used by ShutdownWindowsNT
    2. Private Declare Function ExitWindowsEx Lib "User32" (ByVal uFlags As Long, ByVal dwReserved As Long) As Long
    3. Private Declare Function GetCurrentProcess Lib "kernel32" () As Long
    4. Private Declare Function OpenProcessToken Lib "advapi32" (ByVal _
    5.    ProcessHandle As Long, _
    6.    ByVal DesiredAccess As Long, TokenHandle As Long) As Long
    7. Private Declare Function LookupPrivilegeValue Lib "advapi32" _
    8.    Alias "LookupPrivilegeValueA" _
    9.    (ByVal lpSystemName As String, ByVal lpName As String, lpLuid _
    10.    As LUID) As Long
    11. Private Declare Function AdjustTokenPrivileges Lib "advapi32" _
    12.    (ByVal TokenHandle As Long, _
    13.    ByVal DisableAllPrivileges As Long, NewState As TOKEN_PRIVILEGES _
    14.    , ByVal BufferLength As Long, _
    15. PreviousState As TOKEN_PRIVILEGES, ReturnLength As Long) As Long
    16.  
    17. Private Const EWX_FORCE As Long = 4
    18. Private Type LUID
    19.    UsedPart As Long
    20.    IgnoredForNowHigh32BitPart As Long
    21. End Type
    22. Private Type TOKEN_PRIVILEGES
    23.   PrivilegeCount As Long
    24.   TheLuid As LUID
    25.   Attributes As Long
    26. End Type
    27. Public Enum EnumExitWindows
    28. WE_LOGOFF = 0
    29. WE_SHUTDOWN = 1
    30. WE_REBOOT = 2
    31. WE_POWEROFF = 8
    32. End Enum
    33.  
    34.  
    35.  
    36.  
    37. Public Function ShutdownWindowsNT(ShutdownType As Integer)
    38.  
    39. '1 = ATX power off
    40. '2 = Shutdown
    41. '3 = Restart
    42. '4 = Logoff
    43.  
    44.  
    45. '******* Adjust Priviliage for NT/Win2k/XP ********
    46.   Const TOKEN_ADJUST_PRIVILEGES = &H20
    47.   Const TOKEN_QUERY = &H8
    48.   Const SE_PRIVILEGE_ENABLED = &H2
    49.   Dim hdlProcessHandle As Long
    50.   Dim hdlTokenHandle As Long
    51.   Dim tmpLuid As LUID
    52.   Dim tkp As TOKEN_PRIVILEGES
    53.   Dim tkpNewButIgnored As TOKEN_PRIVILEGES
    54.   Dim lBufferNeeded As Long
    55.  
    56.   hdlProcessHandle = GetCurrentProcess()
    57.   OpenProcessToken hdlProcessHandle, (TOKEN_ADJUST_PRIVILEGES Or _
    58.      TOKEN_QUERY), hdlTokenHandle
    59.  
    60.   ' Get the LUID for shutdown privilege.
    61.   LookupPrivilegeValue "", "SeShutdownPrivilege", tmpLuid
    62.  
    63.   tkp.PrivilegeCount = 1    ' One privilege to set
    64.   tkp.TheLuid = tmpLuid
    65.   tkp.Attributes = SE_PRIVILEGE_ENABLED
    66.  
    67.   ' Enable the shutdown privilege in the access token of this process.
    68.   AdjustTokenPrivileges hdlTokenHandle, False, _
    69.      tkp, Len(tkpNewButIgnored), tkpNewButIgnored, lBufferNeeded
    70.  
    71.    
    72. If ShutdownType = 1 Then ExitWindowsEx (WE_POWEROFF Or EWX_FORCE), 0
    73. If ShutdownType = 2 Then ExitWindowsEx (WE_SHUTDOWN Or EWX_FORCE), 0
    74. If ShutdownType = 3 Then ExitWindowsEx (WE_REBOOT Or EWX_FORCE), 0
    75. If ShutdownType = 4 Then ExitWindowsEx (WE_LOGOFF Or EWX_FORCE), 0
    76.  
    77.  
    78. End Function

    Using VB.NET 2005/.NET 2.0, NetBeans IDE 5, Fujitsu Cobol85,
    Website: http://DreamForgery.com

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