April 19, 2019
Hot Topics:

VB Coding Tip: How Can I Exit Windows From My Application?

  • January 27, 2002
  • By Developer.com Staff
  • Send Email »
  • More Articles »

Use the function ExitWindowsEx. This function has two parameters. The first is one or more flags to instruct windows how to shutdown. These are listed below along with the declarations. The second part of the function is reserved, and it set to zero. It is important to unload your application to help the system to shut down smoothly.


Copy the following code into the declarations section of a module:

Public Const EWX_LOGOFF = 0 
Public Const EWX_SHUTDOWN = 1 
Public Const EWX_REBOOT = 2 
Public Const EWX_FORCE = 4
Declare Function ExitWindowsEx Lib "user32" Alias _
"ExitWindowsEx" (ByVal uFlags As Long, ByVal dwReserved _
As Long) As Long

If you wanted to forcefully reboot the computer use the following code:

t& = ExitWindowsEx(EWX_FORCE Or EWX_REBOOT, 0)

Download a demo project that shows you how to shut down Windows from Visual Basic.

Tip by James Limm

Comment and Contribute


(Maximum characters: 1200). You have characters left.



Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Thanks for your registration, follow us on our social networks to keep up-to-date