VB.NET force reboot

I need to be able to force reboots on Win 2k and XP pro machines using VB.NET.  Does anyone know how this is done?
LVL 2
forrest321Asked:
Who is Participating?
 
Jaime OlivaresConnect With a Mentor Software ArchitectCommented:
Just need this:

Private Declare Function ExitWindowEx Lib "user32" Alias "ExitWindowsEx" (ByVal uFlags As Long, ByVal  dwReserved As Long) As Long

ExitWindowsEx 2, 0
0
 
Fahad MukhtarConnect With a Mentor Distinguished EngineerCommented:
0
 
forrest321Author Commented:
It says: Name "exitWindowsEx" is not declared.
0
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

 
Jaime OlivaresSoftware ArchitectCommented:
Sorry it is a typo, missing s:

Private Declare Function ExitWindowsEx Lib "user32" Alias "ExitWindowsEx" (ByVal uFlags As Long, ByVal  dwReserved As Long) As Long

ExitWindowsEx 2, 0
0
 
Jaime OlivaresSoftware ArchitectCommented:
Some refinements:

In Vb.net declaration could be simpler:
Private Declare Function ExitWindowsEx Lib "user32" (ByVal uFlags As Long, ByVal dwReserved As Long) As Long

To ensure will reboot you can use:
Const EWX_REBOOT = 2
Const EWX_FORCE = 4
ExitWindowsEx(EWX_FORCE Or EWX_REBOOT, 0)

0
 
forrest321Author Commented:
That is compiling with no errors, but its not rebooting....
0
 
Jaime OlivaresSoftware ArchitectCommented:
What OS? Maybe you don't have privileges.
0
 
forrest321Author Commented:
Win XP Pro.  I am local and domain admin...privileges should be fine.
0
 
forrest321Author Commented:
Desp, I do not quite understand that source, or how to use it.  Have you used that before?
0
 
kHSwConnect With a Mentor Commented:
These are the API declarations for VB6

You have to replace Long with Integer (Integer is 32 bits in .NET, the same as a Long in VB6):
Private Declare Function ExitWindowsEx Lib "user32" Alias "ExitWindowsEx" (ByVal uFlags As Integer, ByVal  dwReserved As Integer) As Integer


Then put this into your code:
Public Const EWX_LOGOFF As Short = 0
Public Const EWX_SHUTDOWN As Short = 1
Public Const EWX_REBOOT As Short = 2
Public Const EWX_FORCE As Short = 4

ExitWindowsEx(EWX_FORCE, 0)


0
 
kHSwCommented:
Just a little typo in my previous post.

You have to call ExitWindowsEx(EWX_SHUTDOWN)
0
 
Jaime OlivaresSoftware ArchitectCommented:
Actually correct call to reboot must be:
ExitWindowsEx(EWX_FORCE Or EWX_REBOOT, 0)
0
 
forrest321Author Commented:
I ended up using Shutdown.exe via shell call.

Thanks for the help.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.