Results 1 to 7 of 7

Thread: Reboot Windows 2000 using VB.NET

  1. #1

    Thread Starter
    New Member
    Join Date
    Feb 2002
    Posts
    3

    Cool Reboot Windows 2000 using VB.NET

    I need a way to reboot Windows 2000 server using VB.NET. I have seen the reboot command, but I cannot find any examples. Anyone with an example to share or a URL would be most appreciated.

    TIA,

    Jim

  2. #2
    Lively Member
    Join Date
    Aug 1999
    Location
    Amsterdam
    Posts
    117
    As far as I know there isn't any support for that in the Framework.
    But you could PInvoke ExitWindowsEX or use WMI.

  3. #3

    Thread Starter
    New Member
    Join Date
    Feb 2002
    Posts
    3

    Thanks, WMI was what I was referring to

    Thanks for the reply. WMI was what I was looking at. I just can't figure out what syntax it wants related to the privileges and the actual reboot method. The help that is provided with Visual Studio is of no use without examples.

    Jim

  4. #4

    Thread Starter
    New Member
    Join Date
    Feb 2002
    Posts
    3

    Thank you

    Thank you. That was exactly what I needed.

  5. #5
    Junior Member
    Join Date
    Oct 2002
    Posts
    23

    Angry ??

    Can you please post the code here ?

    I'm looking for a way to reboot the win2k client. I've made it in vb6.0, but the API won't work with .NET. I tried to convert it with no luck.

  6. #6
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083

    Re: ??

    Originally posted by manpowre
    Can you please post the code here ?

    I'm looking for a way to reboot the win2k client. I've made it in vb6.0, but the API won't work with .NET. I tried to convert it with no luck.
    try this
    VB Code:
    1. 'in a module
    2. Declare Function ExitWindowsEx Lib "user32" (ByVal uFlags As Integer, ByVal dwReserved As Integer) As Integer
    3. Public Const EWX_REBOOT As Short = 0
    4.  
    5. 'in a form
    6. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    7.         Dim lresult As Object
    8.         lresult = ExitWindowsEx(EWX_REBOOT, 0)  'close all programs and log on as a different user
    9.     End Sub

  7. #7
    Junior Member
    Join Date
    Oct 2002
    Posts
    23

    Doesn't work

    I have converted my vb6.0 code:
    Still theres an error somewhere...The computer won't go into reboot.

    Public Class clsReboot
    Private Const TOKEN_ADJUST_PRIVILEGES = &H20
    Private Const TOKEN_QUERY = &H8
    Private Const SE_PRIVILEGE_ENABLED = &H2
    Private Const EWX_SHUTDOWN As Integer = 1
    Private Const EWX_FORCE As Integer = 4
    Private Const EWX_REBOOT = 2


    Private Structure LUID_
    Dim UsedPart As Integer
    Dim IgnoredForNowHigh32BitPart As Integer
    End Structure

    Private Structure TOKEN_PRIVILEGES_
    Dim PrivilegeCount As Integer
    Dim TheLuid As LUID_
    Dim Attributes As Integer
    End Structure

    Private TOKEN_PRIVILEGES As TOKEN_PRIVILEGES_
    Private LUID As LUID_

    Private Declare Function ExitWindowsEx Lib "user32" (ByVal dwOptions As Integer, ByVal dwReserved As Integer) As Integer
    Private Declare Function GetCurrentProcess Lib "kernel32" () As Integer
    Private Declare Function OpenProcessToken Lib "advapi32" (ByVal ProcessHandle As Integer, ByVal DesiredAccess As Integer, ByVal TokenHandle As Integer) As Integer
    Private Declare Function LookupPrivilegeValue Lib "advapi32" Alias "LookupPrivilegeValueA" (ByVal lpSystemName As String, ByVal lpName As String, ByVal lpLuid As LUID_) As Integer
    Private Declare Function AdjustTokenPrivileges Lib "advapi32" (ByVal TokenHandle As Integer, ByVal DisableAllPrivileges As Integer, ByVal NewState As TOKEN_PRIVILEGES_, ByVal BufferLength As Integer, ByVal PreviousState As TOKEN_PRIVILEGES_, ByVal ReturnLength As Integer) As Integer

    Public Sub rebootPC()
    Const csProcName = "RebootPC"
    Dim hProcessHandle As Integer
    Dim hTokenHandle As Integer
    Dim tmpLuid As LUID_
    Dim tkpNew As TOKEN_PRIVILEGES_
    Dim tkpPrevious As TOKEN_PRIVILEGES_
    Dim lBufferNeeded As Integer

    hProcessHandle = GetCurrentProcess()
    OpenProcessToken(hProcessHandle, TOKEN_ADJUST_PRIVILEGES Or TOKEN_QUERY, hTokenHandle)

    ' Get the LUID for the shutdown privilege
    LookupPrivilegeValue("", "SeShutdownPrivilege", tmpLuid)

    tkpNew.PrivilegeCount = 1 ' One privilege to set
    tkpNew.TheLuid = tmpLuid
    tkpNew.Attributes = SE_PRIVILEGE_ENABLED

    ' Enable the shutdown privilege in the access token of this process.
    lBufferNeeded = 0
    AdjustTokenPrivileges(hTokenHandle, False, tkpNew, Len(tkpPrevious), tkpPrevious, lBufferNeeded)

    ' Force a Reboot (no option to save files to cancel out)
    ExitWindowsEx(EWX_FORCE Or EWX_REBOOT, &HFFFF)
    End Sub

    End Class

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width