• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 715
  • Last Modified:

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?
0
forrest321
Asked:
forrest321
  • 5
  • 5
  • 2
  • +1
3 Solutions
 
Fahad MukhtarDistinguished EngineerCommented:
0
 
Jaime OlivaresCommented:
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
 
forrest321Author Commented:
It says: Name "exitWindowsEx" is not declared.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Jaime OlivaresCommented:
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 OlivaresCommented:
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 OlivaresCommented:
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
 
kHSwCommented:
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 OlivaresCommented:
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

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 5
  • 5
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now