Solved

VB.NET force reboot

Posted on 2004-08-24
13
694 Views
Last Modified: 2012-08-13
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
Comment
Question by:forrest321
  • 5
  • 5
  • 2
  • +1
13 Comments
 
LVL 19

Assisted Solution

by:Desp
Desp earned 25 total points
ID: 11882347
0
 
LVL 55

Accepted Solution

by:
Jaime Olivares earned 50 total points
ID: 11883706
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
 
LVL 2

Author Comment

by:forrest321
ID: 11884692
It says: Name "exitWindowsEx" is not declared.
0
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 11884877
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
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 11884902
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
 
LVL 2

Author Comment

by:forrest321
ID: 11884936
That is compiling with no errors, but its not rebooting....
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 11884976
What OS? Maybe you don't have privileges.
0
 
LVL 2

Author Comment

by:forrest321
ID: 11884989
Win XP Pro.  I am local and domain admin...privileges should be fine.
0
 
LVL 2

Author Comment

by:forrest321
ID: 11885768
Desp, I do not quite understand that source, or how to use it.  Have you used that before?
0
 
LVL 2

Assisted Solution

by:kHSw
kHSw earned 25 total points
ID: 11891191
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
 
LVL 2

Expert Comment

by:kHSw
ID: 11891244
Just a little typo in my previous post.

You have to call ExitWindowsEx(EWX_SHUTDOWN)
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 11892029
Actually correct call to reboot must be:
ExitWindowsEx(EWX_FORCE Or EWX_REBOOT, 0)
0
 
LVL 2

Author Comment

by:forrest321
ID: 12007389
I ended up using Shutdown.exe via shell call.

Thanks for the help.
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

786 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question