Auto Reboot Error - Privilage not held...

remya1000

Well-known member
Joined
Mar 29, 2007
Messages
122
Programming Experience
Beginner
i'm using VB.NET.

i wrote a Auto Reboot program. and here is the codes i tried...
VB.NET:
Private Enum ShutDown1
        LogOff = 0
        Shutdown = 1
        Reboot = 2
        ForcedLogOff = 4
        ForcedShutdown = 5
        ForcedReboot = 6
        PowerOff = 8
        ForcedPowerOff = 12
End Enum

Public Function ShutDown()

       Try
            Dim w32_os As ManagementClass
            w32_os = New ManagementClass("Win32_OperatingSystem")
            Dim inParams, outParams As ManagementBaseObject
            Dim result As Integer
            w32_os.Scope.Options.EnablePrivileges = True
            Dim obj As ManagementObject
            For Each obj In w32_os.GetInstances()
               inParams = obj.GetMethodParameters("Win32Shutdown")
                inParams("Flags") = ShutDown1.ForcedReboot
                inParams("Reserved") = 0
                outParams = obj.InvokeMethod("Win32Shutdown", inParams, Nothing)
                result = Convert.ToInt32(outParams("returnValue"))
                If (result <> 0) Then
                   Throw New System.ComponentModel.Win32Exception(result)
                End If
            Next
           Return 0
       Catch ex As Exception
            MessageBox.Show("ERROR" & ex.Message)
            Return -10
        End Try
and its Rebooting the machine while this function is called. and i tried this Reboot machine in 4 machines to check its working or not... and 3 machines got Rebooted, but one machine is not getting Rebooted...

and the error that occurs in that machine is
VB.NET:
Privilege not held.
and i search this error in internet and found some helping codes like this...
VB.NET:
Private Declare Function GetCurrentProcess Lib "kernel32.dll" () As IntPtr

    Private Declare Function OpenProcessToken Lib "advapi32.dll" (ByVal ProcessHandle As IntPtr, ByVal DesiredAccess As Int32, ByRef TokenHandle As IntPtr) As Int32

    Private Declare Function LookupPrivilegeValue Lib "advapi32.dll" Alias "LookupPrivilegeValueA" (ByVal lpSystemName As String, ByVal lpName As String, ByRef lpLuid As LUID) As Int32

    Private Declare Function AdjustTokenPrivileges Lib "advapi32.dll" (ByVal TokenHandle As IntPtr, ByVal DisableAllPrivileges As Int32, ByRef NewState As TOKEN_PRIVILEGES, ByVal BufferLength As Int32, ByRef PreviousState As TOKEN_PRIVILEGES, ByRef ReturnLength As Int32) As Int32

    Private Declare Function ExitWindowsEx Lib "user32.dll" (ByVal uFlags As Int32, ByVal dwReserved As Int32) As Int32

    Private Const EWX_FORCE As Int32 = 4
    Private Const EWX_SHUTDOWN As Int32 = 1
    Private Const EWX_REBOOT As Int32 = 2
    Private Const EWX_LOGOFF As Int32 = 0
 
    Public Structure LUID
        Dim LowPart As Int32
        Dim HighPart As Int32
    End Structure

    Public Structure TOKEN_PRIVILEGES
        Public PrivilegeCount As Integer
        Public Privileges As LUID
        Public Attributes As Int32
    End Structure

Public Function ShutDown()
        If MessageBox.Show("Would you like to re-boot the system", "", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) = DialogResult.OK Then
            Dim platform As New PlatformID
            Select Case Environment.OSVersion.Platform
                Case PlatformID.Win32NT
                    Dim token As TOKEN_PRIVILEGES
                    Dim blank_token As TOKEN_PRIVILEGES
                    Dim token_handle As IntPtr
                    Dim uid As LUID
                    Dim ret_length As Integer
                    Dim ptr As IntPtr = GetCurrentProcess() '/// get the process handle
                    OpenProcessToken(ptr, &H20 Or &H8, token_handle)
                    LookupPrivilegeValue("", "SeShutdownPrivilege", uid)
                    token.PrivilegeCount = 1
                    token.Privileges = uid
                    token.Attributes = &H2
                    AdjustTokenPrivileges(token_handle, False, token, System.Runtime.InteropServices.Marshal.SizeOf(blank_token), blank_token, ret_length)
                    ExitWindowsEx(EWX_LOGOFF Or EWX_FORCE Or EWX_REBOOT, &HFFFF)
                Case Else
                    ExitWindowsEx(EWX_SHUTDOWN Or EWX_FORCE Or EWX_REBOOT, &HFFFF)
            End Select
        End If
    End Function
but this too is not working.... and its all work in admin controlled machines.

if you have any idea what's wrong with my codes or what's missing in this codes or what i need to check for this problem, please let me know.... it will be great help for me....

thanks in advanace...
 
Back
Top