472,778 Members | 2,543 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,778 software developers and data experts.

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 1379

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

Similar topics

2
by: Frank Py | last post by:
I have a SQL Server 2000 that failed to start after a Windows reboot. Agent properties are checked to auto start if stopped. Under the Agent Error Log, I have the following data: Information:...
2
by: innesm | last post by:
Hi, Although I havent been able to find any documentation to confirm it, it looks like any change to a windows local group's membership is only reflected in the group editing UI (and the...
0
by: Manoj Nair | last post by:
Hi, I am writing a console application using WMI with C# . Development is for Windows XP machine. One of the methods requires to reboot the local terminal. In my application I make a call to...
1
by: Howard Pinsley | last post by:
Every time I reboot my machine, I can no longer run ASP.NET applications. I have to run aspnet_regiis.exe /i after every reboot. The error: Server Application Unavailable The web application...
0
by: Loretta | last post by:
I have been trying to install VB.net 2002 and keep getting the following error: "Setup has detected that another program requires the computer to reboot. You must reboot the computer before...
14
by: Ed L. | last post by:
A power failure led to failed postmaster restart using 7.4.6 (see output below). The short-term fix is usually to delete the pid file and restart. I often wonder why ipcs never seems to show the...
3
by: iamsiju | last post by:
Hi, I am facing a problem with the Perl Telnet Object. My Telnet Object hangs just after issueing reboot command on the remote host. Please help me who faced this problem !!! Thanks in...
3
by: cj | last post by:
My program shuts itself down and reboots the pc it's running on at 2:00am twice a week and has been doing so without fail for over a year. But twice in the past month it's been found running in the...
3
by: Gladiator | last post by:
Hi , Can someone tell me how do i grant a Read privilage on all the tables in a schema X to a different user who is not the owner of the Schema . I dont want to grant him the Alterin privilage...
0
by: Rina0 | last post by:
Cybersecurity engineering is a specialized field that focuses on the design, development, and implementation of systems, processes, and technologies that protect against cyber threats and...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.