Results 1 to 5 of 5

Thread: How to restart or turn off the computer?

  1. #1

    Thread Starter
    Member
    Join Date
    Jan 2002
    Location
    Toronto, Canada
    Posts
    50

    Question How to restart or turn off the computer?

    Hi,

    I would like to know how to restart or turn off the computer from the VB application.

    Thanks,
    Andrew
    Best regards, Andrew

  2. #2
    Hyperactive Member Hampster's Avatar
    Join Date
    Feb 2001
    Location
    On my hamster wheel.
    Posts
    374

  3. #3
    ^:^...ANGEL...^:^ wrack's Avatar
    Join Date
    Mar 2002
    Location
    Melbourne, AUSTRALIA
    Posts
    2,695

    Thumbs up SEARCH...

    Search the forum for the posts I have replied...

    I am sure that I have replied for this question...

    Cheers...

  4. #4
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Restart for either Win9x or NT/2000 machines

    VB Code:
    1. Option Explicit
    2.     Private Const EWX_LogOff As Long = 0
    3.     Private Const EWX_SHUTDOWN As Long = 1
    4.     Private Const EWX_REBOOT As Long = 2
    5.     Private Const EWX_FORCE As Long = 4
    6.     Private Const EWX_POWEROFF As Long = 8
    7.  
    8. 'The ExitWindowsEx function either logs off, shuts down, or shuts
    9. 'down and restarts the system.
    10.  
    11. Private Declare Function ExitWindowsEx Lib "user32" _
    12.     (ByVal dwOptions As Long, _
    13.     ByVal dwReserved As Long) As Long
    14.  
    15. 'The GetLastError function returns the calling thread's last-error
    16. 'code value. The last-error code is maintained on a per-thread basis.
    17. 'Multiple threads do not overwrite each other's last-error code.
    18. Private Declare Function GetLastError Lib "kernel32" () As Long
    19.  
    20. Private Const mlngWindows95 = 0
    21. Private Const mlngWindowsNT = 1
    22. Public glngWhichWindows32 As Long
    23. 'The GetVersion function returns the operating system in use.
    24. Private Declare Function GetVersion Lib "kernel32" () As Long
    25.  
    26. Private Type LUID
    27.     UsedPart As Long
    28.     IgnoredForNowHigh32BitPart As Long
    29. End Type
    30.  
    31. Private Type LUID_AND_ATTRIBUTES
    32.     TheLuid As LUID
    33.     Attributes As Long
    34. End Type
    35. Private Type TOKEN_PRIVILEGES
    36.     PrivilegeCount As Long
    37.     TheLuid As LUID
    38.     Attributes As Long
    39. End Type
    40. 'The GetCurrentProcess function returns a pseudohandle for the
    41. 'current process.
    42. Private Declare Function GetCurrentProcess Lib "kernel32" () As Long
    43. 'The OpenProcessToken function opens the access token associated with
    44. 'a process.
    45. Private Declare Function OpenProcessToken Lib "advapi32" _
    46.     (ByVal ProcessHandle As Long, _
    47.     ByVal DesiredAccess As Long, _
    48.     TokenHandle As Long) As Long
    49. 'The LookupPrivilegeValue function retrieves the locally unique
    50. 'identifier (LUID) used on a specified system to locally represent
    51. 'the specified privilege name.
    52. Private Declare Function LookupPrivilegeValue Lib "advapi32" _
    53.     Alias "LookupPrivilegeValueA" _
    54.     (ByVal lpSystemName As String, _
    55.     ByVal lpName As String, _
    56.     lpLuid As LUID) As Long
    57. 'The AdjustTokenPrivileges function enables or disables privileges
    58. 'in the specified access token. Enabling or disabling privileges
    59. 'in an access token requires TOKEN_ADJUST_PRIVILEGES access.
    60. Private Declare Function AdjustTokenPrivileges Lib "advapi32" _
    61.     (ByVal TokenHandle As Long, _
    62.     ByVal DisableAllPrivileges As Long, _
    63.     NewState As TOKEN_PRIVILEGES, _
    64.     ByVal BufferLength As Long, _
    65.     PreviousState As TOKEN_PRIVILEGES, _
    66.     ReturnLength As Long) As Long
    67.     Private Declare Sub SetLastError Lib "kernel32" _
    68.     (ByVal dwErrCode As Long)
    69. Private Sub AdjustToken()
    70. 'This procedure sets the proper privileges to allow a log off or a
    71. 'shut down to occur under Windows NT.
    72. Const TOKEN_ADJUST_PRIVILEGES = &H20
    73. Const TOKEN_QUERY = &H8
    74. Const SE_PRIVILEGE_ENABLED = &H2
    75. Dim hdlProcessHandle As Long
    76. Dim hdlTokenHandle As Long
    77. Dim tmpLuid As LUID
    78. Dim tkp As TOKEN_PRIVILEGES
    79. Dim tkpNewButIgnored As TOKEN_PRIVILEGES
    80. Dim lBufferNeeded As Long
    81. 'Set the error code of the last thread to zero using the
    82. 'SetLast Error function. Do this so that the GetLastError
    83. 'function does not return a value other than zero for no
    84. 'apparent reason.
    85.     SetLastError 0
    86. 'Use the GetCurrentProcess function to set the hdlProcessHandle
    87. 'variable.
    88.     hdlProcessHandle = GetCurrentProcess()
    89.     If GetLastError <> 0 Then
    90.         systemMessage.AddItem "GetCurrentProcess Error==" & GetLastError
    91.     End If
    92.     OpenProcessToken hdlProcessHandle, _
    93.         (TOKEN_ADJUST_PRIVILEGES Or TOKEN_QUERY), hdlTokenHandle
    94.     If GetLastError <> 0 Then
    95.         systemMessage.AddItem "OpenProcessToken Error==" & GetLastError
    96.     End If
    97. 'Get the LUID for shutdown privilege
    98.     LookupPrivilegeValue "", "SeShutdownPrivilege", tmpLuid
    99.     If GetLastError <> 0 Then
    100.         systemMessage.AddItem "LookupPrivilegeValue Error==" & GetLastError
    101.     End If
    102.     tkp.PrivilegeCount = 1    ' One privilege to set
    103.     tkp.TheLuid = tmpLuid
    104.     tkp.Attributes = SE_PRIVILEGE_ENABLED
    105. 'Enable the shutdown privilege in the access token of this process
    106.     AdjustTokenPrivileges hdlTokenHandle, _
    107.     False, _
    108.     tkp, _
    109.     Len(tkpNewButIgnored), _
    110.     tkpNewButIgnored, _
    111.     lBufferNeeded
    112.     If GetLastError <> 0 Then
    113.         systemMessage.AddItem "AdjustTokenPrivileges Error==" & GetLastError
    114.     End If
    115. End Sub
    116. Private Sub cmdLogoff_Click()
    117.     ExitWindowsEx (EWX_LogOff), &HFFFF
    118.     systemMessage.AddItem "ExitWindowsEx's GetLastError " & GetLastError
    119. End Sub
    120. Private Sub cmdForceLogoff_Click()
    121.     ExitWindowsEx (EWX_LogOff Or EWX_FORCE), &HFFFF
    122.     systemMessage.AddItem "ExitWindowsEx's GetLastError " & GetLastError
    123. End Sub
    124. Private Sub cmdShutdown_Click()
    125.     If glngWhichWindows32 = mlngWindowsNT Then
    126.         AdjustToken
    127.         systemMessage.AddItem "Post-AdjustToken GetLastError " & GetLastError
    128.     End If
    129.     ExitWindowsEx (EWX_SHUTDOWN), &HFFFF
    130.     systemMessage.AddItem "ExitWindowsEx's GetLastError " & GetLastError
    131. End Sub
    132. Private Sub cmdForceShutdown_Click()
    133.     If glngWhichWindows32 = mlngWindowsNT Then
    134.         AdjustToken
    135.         systemMessage.AddItem "Post-AdjustToken GetLastError " & GetLastError
    136.     End If
    137.     ExitWindowsEx (EWX_SHUTDOWN Or EWX_FORCE), &HFFFF
    138.     systemMessage.AddItem "ExitWindowsEx's GetLastError " & GetLastError
    139. End Sub
    140. Private Sub exit_Click()
    141.     End
    142. End Sub
    143. Private Sub Form_Load()
    144. 'When the project starts, check the operating system used by
    145. 'calling the GetVersion function.
    146. Dim lngVersion As Long
    147.     lngVersion = GetVersion()
    148.     If ((lngVersion And &H80000000) = 0) Then
    149.         glngWhichWindows32 = mlngWindowsNT
    150.         systemType.Text = "Windows NT"
    151.     Else
    152.         glngWhichWindows32 = mlngWindows95
    153.         systemType.Text = "Windows 95"
    154.     End If
    155. End Sub

  5. #5
    ^:^...ANGEL...^:^ wrack's Avatar
    Join Date
    Mar 2002
    Location
    Melbourne, AUSTRALIA
    Posts
    2,695

    Thumbs up Try this...

    This should work for any Operating System...9X/ME/NT/2K/XP

    VB Code:
    1. Private Declare Function SHShutDownDialog Lib "shell32" _
    2.         Alias "#60" (ByVal something As Long) As Long
    3.  
    4. Private Sub cmdShutdown_Click()
    5.     SHShutDownDialog 0
    6. End Sub

    Cheers...
    Last edited by wrack; Aug 20th, 2002 at 08:54 AM.

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