Results 1 to 7 of 7

Thread: Shutdown routine

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Sep 2008
    Posts
    1,260

    Shutdown routine

    I am looking for some code that will let me shutdown/restart/log off/standby my windows system.

  2. #2
    Freelancer akhileshbc's Avatar
    Join Date
    Jun 2008
    Location
    Trivandrum, Kerala, India
    Posts
    7,652

    Re: Shutdown routine

    You can find lots of code snippets in our CodeBank...

    http://www.vbforums.com/search.php?searchid=2445048

    If my post was helpful to you, then express your gratitude using Rate this Post.
    And if your problem is SOLVED, then please Mark the Thread as RESOLVED (see it in action - video)
    My system: AMD FX 6100, Gigabyte Motherboard, 8 GB Crossair Vengance, Cooler Master 450W Thunder PSU, 1.4 TB HDD, 18.5" TFT(Wide), Antec V1 Cabinet

    Social Group: VBForums - Developers from India


    Skills: PHP, MySQL, jQuery, VB.Net, Photoshop, CodeIgniter, Bootstrap,...

  3. #3
    PowerPoster Ellis Dee's Avatar
    Join Date
    Mar 2007
    Location
    New England
    Posts
    3,530

    Re: Shutdown routine

    I didn't write this but I've been using it for years:
    vb Code:
    1. Option Explicit
    2.  
    3. Private Type TOKEN_PRIVILEGES
    4.     PrivilegeCount As Long
    5.     LuidUDT As LUID
    6.    Attributes As Long
    7. End Type
    8.  
    9. Private Declare Function AdjustTokenPrivileges Lib "advapi32" (ByVal TokenHandle As Long, ByVal DisableAllPrivileges As Long, NewState As TOKEN_PRIVILEGES, ByVal BufferLength As Long, PreviousState As Any, ReturnLength As Any) As Long
    10. Private Declare Function ExitWindowsEx Lib "user32" (ByVal dwOptions As Long, ByVal dwReserved As Long) As Long
    11. Private Declare Function GetCurrentProcess Lib "kernel32" () As Long
    12. Private Declare Function GetVersion Lib "kernel32" () As Long
    13. Private Declare Function LookupPrivilegeValue Lib "advapi32" Alias "LookupPrivilegeValueA" (ByVal lpSystemName As String, ByVal lpName As String, lpLuid As LUID) As Long
    14. Private Declare Function OpenProcessToken Lib "advapi32" (ByVal ProcessHandle As Long, ByVal DesiredAccess As Long, TokenHandle As Long) As Long
    15.  
    16. ' Shut down windows, and optional reboot it
    17. ' if the 2nd argument is True, no WM_QUERYENDSESSION and WM_ENDSESSION
    18. ' messages are sent to active applications
    19. Public Sub ShutDownWindows(Optional ByVal Reboot As Boolean = False, Optional ByVal Force As Boolean)
    20.     Const EWX_LOGOFF = 0
    21.     Const EWX_SHUTDOWN = 1
    22.     Const EWX_REBOOT = 2
    23.     Const EWX_FORCE = 4
    24.     Const EWX_POWEROFF = 8
    25.     Const TOKEN_ADJUST_PRIVILEGES = &H20
    26.     Const TOKEN_QUERY = &H8
    27.     Const SE_PRIVILEGE_ENABLED = &H2
    28.     Dim hToken As Long
    29.     Dim tp As TOKEN_PRIVILEGES
    30.     Dim flags As Long
    31.    
    32.     ' Windows NT/2000 require a special treatment
    33.     ' to ensure that the calling process has the
    34.     ' privileges to shut down the system
    35.    
    36.     ' under NT the high-order bit (that is, the sign bit)
    37.     ' of the value retured by GetVersion is cleared
    38.     If GetVersion() >= 0 Then
    39.         ' Open this process for adjusting its privileges
    40.         OpenProcessToken GetCurrentProcess(), (TOKEN_ADJUST_PRIVILEGES Or TOKEN_QUERY), hToken
    41.        
    42.         ' Get the LUID for shutdown privilege.
    43.         ' retrieves the locally unique identifier (LUID) used
    44.         ' to locally represent the specified privilege name
    45.         ' (first argument = "" means the local system)
    46.         LookupPrivilegeValue "", "SeShutdownPrivilege", tp.LuidUDT
    47.        
    48.         ' complete the TOKEN_PRIVILEGES structure with the # of
    49.         ' privileges and the desired attribute
    50.         tp.PrivilegeCount = 1
    51.         tp.Attributes = SE_PRIVILEGE_ENABLED
    52.        
    53.         ' enables or disables privileges in the specified access token
    54.         ' last 3 arguments are zero because we aren't interested
    55.         ' in previous privilege attributes.
    56.         AdjustTokenPrivileges hToken, False, tp, 0, ByVal 0&, ByVal 0&
    57.     End If
    58.    
    59.     ' prepare shutdown flags
    60.     flags = EWX_SHUTDOWN
    61.     If Reboot Then
    62.         flags = flags Or EWX_REBOOT
    63.     Else
    64.         flags = flags Or EWX_POWEROFF
    65.     End If
    66.     If Force Then flags = flags Or EWX_FORCE
    67.    
    68.     ' finally, you can shut down Windows
    69.     ExitWindowsEx flags, &HFFFF
    70. End Sub

  4. #4

    Thread Starter
    Frenzied Member
    Join Date
    Sep 2008
    Posts
    1,260

    Re: Shutdown routine

    Code:
    #
        Const EWX_LOGOFF = 0
    #
        Const EWX_SHUTDOWN = 1
    #
        Const EWX_REBOOT = 2
    #
        Const EWX_FORCE = 4
    #
        Const EWX_POWEROFF = 8
    #
    What is the difference between PowerOff and Shutdown?

    Also, how can i get the system to standby?

  5. #5
    PowerPoster Ellis Dee's Avatar
    Join Date
    Mar 2007
    Location
    New England
    Posts
    3,530

    Re: Shutdown routine

    I never played around with the constants, though I'm glad I preserved them in the code for just such an occasion.

    I'm guessing that PowerOff puts the computer in that sleep mode where the drive spins down, which is I believe what you mean by standby. I don't know the actual official term for it; maybe hibernate?

    EDIT: Of course that can't be right because PowerOff actually shuts the computer down. Maybe Shutdown does the hibernate thing?

  6. #6
    PowerPoster Ellis Dee's Avatar
    Join Date
    Mar 2007
    Location
    New England
    Posts
    3,530

    Re: Shutdown routine

    Okay, a little poking around explains it.

    Since this could conceivably be before your (or a lurker's) time, I'll explain the difference.

    Back in the day when Windows 95 first came out, most computers couldn't completely power down except by having the user physically press the power button on the case. Before Windows 95 the user had to take it upon himself to ensure no programs were running when he clicked the power button. This meant you'd have to manually exit Windows 3.1 until you saw a DOS prompt, and only then could you power off.

    Windows 95 introduced the "Shutdown" concept that had already existed on Macs for years. The only problem was that computers themselves didn't have the required circuitry to physically turn themselves off. So in Windows 95 what typically happened is you'd click Shutdown from the Start Menu and Windows would do its shutdown thing then leave the computer sitting at a screen that said "It is now safe to turn off your computer." The user would then flip the switch on the case to actually turn it off.

    Very quickly after that manufacturers added the necessary circuitry to let computers turn themselves off, so instead of telling the user to turn it off it'd just turn itself off. So that's the difference.

    Shutdown: Shut down Windows and display the "It is now safe to turn off your computer" screen.
    PowerOff: Shut down Windows and turn off the computer.

  7. #7

    Thread Starter
    Frenzied Member
    Join Date
    Sep 2008
    Posts
    1,260

    Re: Shutdown routine

    thanks for your help.

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