Class: Shutdown in Category General VBA/VB6 : Application Techniques from Total Visual SourceBook

Shutting down Windows with options to restart and logoff in VB6 and VBA with 32 and 64-bit Windows API calls.

This class allows you to shutdown Windows in one of four ways:

  1. Shutdown
  2. Restart
  3. Logoff
  4. Shutdown with Power Off
Procedure Name Type Description
(Declarations) Declarations Declarations and private variables for the CShutdown class.
ForceReboot Property Determine whether or not the class forces Windows to reboot regardless of running process status. If this property is set to True, the system does not send the WM_QUERYENDSESSION or WM_ENDSESSION messages to processes. This can result in data loss, and should only be used in an emergency.
ShutdownMode Property Get the shutdown mode.
Shutdown Method Shuts down Windows according to the mode specified by the ShutdownMode property and the Force property. The Windows function returns as soon as it is called since all subsequent activity related to the shutdown is then handled asynchronously by Windows. When a shutdown is initiated, all applications are informed that they need to shutdown. If an application does not respond after a specific period of time, Windows displays a dialog allowing you to:
  • Forcibly shut down the application.
  • Retry the shutdown.
  • Cancel the shutdown.
If you set the Force property to True, Windows always forces applications to close and does not display the dialog.
AdjustToken Private Windows requires that you have SE_SHUTDOWN_NAME privilege. VB6 apps don't have this by default, so this procedure grants it.
IsNT Private Determine if the OS is Windows NT.
MakeFlags Private Build the flags from class property values.
' Example of the CShutdown class
'
' To use this example:
' 1. Create a new form.
' 2. Create a new checkbox named chkForce, with associated label named lblForce
' 3. Create a command button named cmdLogOff
' 4. Create a command button named cmdShutdown
' 5. Create a command button named cmdPowerOff
' 6. Create a command button named cmdReboot
' 7. Paste the entire contents of this module into the new form's module.

Private mclsShutdown As CShutdown

Private Sub DoIt(eMode As EnumShutdownModes)

  If mclsShutdown Is Nothing Then
    CreateShutdownObject
  End If

  ' Set the properties
  mclsShutdown.ForceReboot = (Me.chkForce.Value = 1)
  mclsShutdown.ShutdownMode = eMode

  ' Call the shutdown
  mclsShutdown.Shutdown

  ' Close the form (this is because the LogOff mode doesn't close our VB6 app.)
  DoCmd.Close acForm, Me.name, acSaveYes
  DoCmd.Quit

End Sub

Private Sub cmdLogOff_Click()

  Call DoIt(smLogOff)

End Sub

Private Sub cmdShutdown_Click()

  Call DoIt(smShutdown)

End Sub

Private Sub cmdPowerOff_Click()

  Call DoIt(smPowerOff)

End Sub

Private Sub cmdReboot_Click()

  Call DoIt(smReboot)

End Sub

Private Sub CreateShutdownObject()

  ' Instantiate the class
  Set mclsShutdown = New CShutdown

  ' Set up the form

  Me.Caption = "Shutdown Windows"

  With chkForce
    .Top = 450
    .Left = 45
    .Width = 200
    .Height = 330
  End With

  ' Check Box Labels in Access are not referred to by the
  ' Caption property.  You must explicitly use the
  ' Label's .Caption property instead.
  With lblForce
    .Caption = "Force reboot (may cause data loss in unsaved documents)"

    ' Might as well reset the Label position as well
    .Top = 450
    .Left = chkForce.Left + chkForce.Width
    .Width = 7000
    .Height = 330
  End With

  With cmdLogOff
    .Top = 855
    .Left = 1440
    .Height = 510
    .Width = 2400
    .Caption = "Log Off"
  End With

  With cmdShutdown
    .Top = 1395
    .Left = 1440
    .Height = 510
    .Width = 2400
    .Caption = "Shutdown Windows"
  End With

  With cmdPowerOff
    .Top = 1980
    .Left = 1440
    .Height = 510
    .Width = 2400
    .Caption = "Shutdown Windows, Power Off"
  End With

  With cmdReboot
    .Top = 2565
    .Left = 1440
    .Height = 510
    .Width = 2400
    .Caption = "Restart Windows"
  End With

End Sub

Private Sub Form_Load()
  CreateShutdownObject
End Sub

Total Visual SourceBook The source code in Total Visual Sourcebook includes modules and classes for Microsoft Access, Visual Basic 6 (VB6), and Visual Basic for Applications (VBA) developers. Easily add this professionally written, tested, and documented royalty-free code into your applications to simplify your application development efforts.

Total Visual SourceBook is written for the needs of a developer using a source code library covering the many challenges you face. Countless developers over the years have told us they learned some or much of their development skills and tricks from our code. You can too!

Additional Resources

Total Visual SourceBook CD and Printed Manual

Microsoft Access/ Office 2016, 2013, 2010, and 2007 Version
is Shipping!

New features in Total Visual SourceBook for Access, Office and VB6

Supports Access/Office 2016, 2013, 2010 and 2007, and Visual Basic 6.0!


View all FMS products for Microsoft Access All Our Microsoft Access Products

Reviews

Reader Choice Award for MS Access Source Code Library
Reader Choice

"The code is exactly how I would like to write code and the algorithms used are very efficient and well-documented."

Van T. Dinh, Microsoft MVP

SourceBook Info

Additional Info

Question

 

 

Free Product Catalog from FMS