|
-
Feb 24th, 2002, 05:26 PM
#1
Thread Starter
New Member
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
-
Feb 25th, 2002, 03:13 PM
#2
Lively Member
As far as I know there isn't any support for that in the Framework.
But you could PInvoke ExitWindowsEX or use WMI.
-
Feb 25th, 2002, 03:34 PM
#3
Thread Starter
New Member
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
-
Feb 26th, 2002, 08:23 AM
#4
Thread Starter
New Member
Thank you
Thank you. That was exactly what I needed.
-
Feb 3rd, 2003, 10:04 AM
#5
Junior Member
??
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.
-
Feb 3rd, 2003, 10:30 AM
#6
Sleep mode
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:
'in a module
Declare Function ExitWindowsEx Lib "user32" (ByVal uFlags As Integer, ByVal dwReserved As Integer) As Integer
Public Const EWX_REBOOT As Short = 0
'in a form
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim lresult As Object
lresult = ExitWindowsEx(EWX_REBOOT, 0) 'close all programs and log on as a different user
End Sub
-
Feb 3rd, 2003, 11:02 AM
#7
Junior Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|