Solved

Reboot Computer in VB6 (Visual Basic)

Posted on 2004-08-18
4
2,558 Views
Last Modified: 2013-11-25
Ok heres a simple one.

Here is what im doing for log off:

Private Declare Function ExitWindowsEx Lib "user32" (ByVal uFlags As Long, ByVal dwReserved As Long) As Long
Private Const EWX_LOGOFF = 0
Private Const EWX_REBOOT = 2

Private Sub lnkLogOff_Click()
ExitWindowsEx EWX_LOGOFF, 0
End Sub

Private Sub lnkRestart_Click()
ExitWindowsEx EWX_REBOOT, 0
End Sub

Reboot doesn't work... what is up? Ive seen this post countless times, but the reboot feature doesn't work! it only logs me off! Any ideas?
0
Comment
Question by:lpyzr
4 Comments
 
LVL 8

Expert Comment

by:mladenovicz
ID: 11834401
  Option Explicit

Private Const TOKEN_ADJUST_PRIVILEGES As Long = &H20
Private Const TOKEN_QUERY As Long = &H8
Private Const SE_PRIVILEGE_ENABLED As Long = &H2

Private Const EWX_LOGOFF As Long = &H0
Private Const EWX_SHUTDOWN As Long = &H1
Private Const EWX_REBOOT As Long = &H2
Private Const EWX_FORCE As Long = &H4
Private Const EWX_POWEROFF As Long = &H8

Private Const VER_PLATFORM_WIN32_NT As Long = 2

Private Type OSVERSIONINFO
  OSVSize         As Long
  dwVerMajor      As Long
  dwVerMinor      As Long
  dwBuildNumber   As Long
  PlatformID      As Long
  szCSDVersion    As String * 128
End Type

Private Type LUID
   dwLowPart As Long
   dwHighPart As Long
End Type

Private Type LUID_AND_ATTRIBUTES
   udtLUID As LUID
   dwAttributes As Long
End Type

Private Type TOKEN_PRIVILEGES
   PrivilegeCount As Long
   laa As LUID_AND_ATTRIBUTES
End Type
     
Private Declare Function ExitWindowsEx Lib "user32" _
   (ByVal dwOptions As Long, _
   ByVal dwReserved As Long) As Long

Private Declare Function GetCurrentProcess Lib "kernel32" () As Long

Private Declare Function OpenProcessToken Lib "advapi32" _
  (ByVal ProcessHandle As Long, _
   ByVal DesiredAccess As Long, _
   TokenHandle As Long) As Long

Private Declare Function LookupPrivilegeValue Lib "advapi32" _
   Alias "LookupPrivilegeValueA" _
  (ByVal lpSystemName As String, _
   ByVal lpName As String, _
   lpLuid As LUID) As Long

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 Long) As Long

Private Declare Function GetVersionEx Lib "kernel32" _
   Alias "GetVersionExA" _
  (lpVersionInformation As OSVERSIONINFO) As Long
 
 
 
 

Private Function IsWinNTPlus() As Boolean

#If Win32 Then
 
      Dim OSV As OSVERSIONINFO
   
      OSV.OSVSize = Len(OSV)
   
      If GetVersionEx(OSV) = 1 Then

         IsWinNTPlus = (OSV.PlatformID = VER_PLATFORM_WIN32_NT) And _
                       (OSV.dwVerMajor >= 4)
      End If

   #End If

End Function


Private Function EnableShutdownPrivledges() As Boolean

   Dim hProcessHandle As Long
   Dim hTokenHandle As Long
   Dim lpv_la As LUID
   Dim token As TOKEN_PRIVILEGES
   
   hProcessHandle = GetCurrentProcess()
   
   If hProcessHandle <> 0 Then

If OpenProcessToken(hProcessHandle, (TOKEN_ADJUST_PRIVILEGES Or TOKEN_QUERY), hTokenHandle) <> 0 Then

If LookupPrivilegeValue(vbNullString, "SeShutdownPrivilege", lpv_la) <> 0 Then

With token
               .PrivilegeCount = 1
               .laa.udtLUID = lpv_la
               .laa.dwAttributes = SE_PRIVILEGE_ENABLED
            End With
   
If AdjustTokenPrivileges(hTokenHandle, False, token, ByVal 0&, ByVal 0&, ByVal 0&) <> 0 Then
                                   
EnableShutdownPrivledges = True
   
            End If
End If
End If
End If

End Function



Private Sub Command1_Click()
   Dim uflags As Long
   Dim success As Long
   'force shutdown
uflags = EWX_FORCE
If IsWinNTPlus() Then
   
      success = EnableShutdownPrivledges()
      If success Then Call ExitWindowsEx(uflags, 0&)
         
   Else

Call ExitWindowsEx(uflags, 0&)
     
   End If
End Sub

Private Sub Command2_Click()
   Dim uflags As Long
   Dim success As Long
   'log off
uflags = EWX_LOGOFF
If IsWinNTPlus() Then
   
      success = EnableShutdownPrivledges()
      If success Then Call ExitWindowsEx(uflags, 0&)
         
   Else

Call ExitWindowsEx(uflags, 0&)
     
   End If
End Sub

Private Sub Command3_Click()
   Dim uflags As Long
   Dim success As Long
    'shut down
uflags = EWX_SHUTDOWN
If IsWinNTPlus() Then
   
      success = EnableShutdownPrivledges()
      If success Then Call ExitWindowsEx(uflags, 0&)
         
   Else

Call ExitWindowsEx(uflags, 0&)
     
   End If

End Sub

Private Sub Command4_Click()
   Dim uflags As Long
   Dim success As Long
   'reboot
uflags = EWX_REBOOT
If IsWinNTPlus() Then
   
      success = EnableShutdownPrivledges()
      If success Then Call ExitWindowsEx(uflags, 0&)
         
   Else

Call ExitWindowsEx(uflags, 0&)
     
   End If
End Sub

Private Sub Command5_Click()
   Dim uflags As Long
   Dim success As Long
   'power off za gasenje windowsa
uflags = EWX_POWEROFF
If IsWinNTPlus() Then
   
      success = EnableShutdownPrivledges()
      If success Then Call ExitWindowsEx(uflags, 0&)
         
   Else

Call ExitWindowsEx(uflags, 0&)
     
   End If

End Sub
0
 
LVL 26

Expert Comment

by:EDDYKT
ID: 11834402
Try

Public Const EWX_SHUTDOWN = 1
0
 
LVL 52

Accepted Solution

by:
Carl Tawn earned 125 total points
ID: 11834431
If your running NT or 2000 then it won't.  Reboot requires SE_SHUTDOWN_NAME privileges, which, by default, VB doesn't have.

This article will show you how yo set the relevant permissions:

http://support.microsoft.com/default.aspx?scid=kb;en-us;176695


Hope this helps
0
 
LVL 4

Expert Comment

by:ark4877
ID: 11834701
try this link, hope this solves ur problem


http://www.vb-helper.com/howto_shutdown_windows.html
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now