Wrox Programmer Forums
Go Back   Wrox Programmer Forums > .NET > Other .NET > General .NET
|
General .NET For general discussion of MICROSOFT .NET topics that don't fall within any of the other .NET forum subcategories or .NET language forums.  If your question is specific to a language (C# or Visual Basic) or type of application (Windows Forms or ASP.Net) try an applicable forum category. ** PLEASE BE SPECIFIC WITH YOUR QUESTION ** When posting here, provide details regarding the Microsoft .NET language you are using and/or what type of application (Windows/Web Forms, etc) you are working in, if applicable to the question. This will help others answer the question without having to ask.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the General .NET section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old October 1st, 2007, 10:16 PM
Authorized User
 
Join Date: Sep 2007
Posts: 62
Thanks: 0
Thanked 0 Times in 0 Posts
Default Auto Reboot Error - Privilage not held...

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 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
 
Quote:
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(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...





Similar Threads
Thread Thread Starter Forum Replies Last Post
track mouse position while it's held down and move lina10 VBScript 0 April 9th, 2007 12:50 AM
how to maintain the records for Reboot Divesh4u Visual C++ 0 June 15th, 2006 07:55 AM
Please help with this am held up p2ptolu Classic ASP Databases 1 May 19th, 2005 07:43 AM
Use Hand Held Scanner Mahavishnu Classic ASP Professional 1 November 16th, 2004 12:50 PM
Reboot a HP/pc ezmee SQL Server DTS 1 June 28th, 2004 09:07 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.