469,951 Members | 2,703 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,951 developers. It's quick & easy.

Auto Reboot Error - Privilage not held...

115 100+
i'm using VB.NET.

i wrote a Auto Reboot program. and here is the codes i tried...

Code:
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 Tryand 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

Quote:
Privilege not held.


and i search this error in internet and found some helping codes like this...

Code:
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(blan k_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 Functionbut 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...
Oct 2 '07 #1
0 1250

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

2 posts views Thread by Frank Py | last post: by
reply views Thread by Manoj Nair | last post: by
1 post views Thread by Howard Pinsley | last post: by
reply views Thread by Loretta | last post: by
14 posts views Thread by Ed L. | last post: by
3 posts views Thread by Gladiator | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.