[Webinar] Streamline your web hosting managementRegister Today

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

system shutdown

iam working in windows nt work station 4.0 environment and i have got service pack 5.0 installed.

problem:- i would like to shutdown my system as soon as i exit my vb application.However i have tried using exitwindowex and initiatesystemshutdown api's and all but i could n't succeed in winnt but it works fine for win95.could any one help me out.
1 Solution

VB Declaration

Declare Function ExitWindowsEx& Lib "user32" (ByVal uFlags As Long, ByVal _
dwReserved As Long)


Allows you to exit and optionally restart windows.

Use with VB

No problem.

Parameter      Type/Description
uFlags      Long—Specify one or more of the following flags (combined using the OR operation):EWX_FORCE—Forcibly terminates processes that do not respond.EWX_LOGOFF—Terminates processes, then logs off.EWX_SHUTDOWN—Powers the system off, if possible.EWX_REBOOT—Reboots the system.EWX_SHUTDOWN—Shuts the system down.
dwReserved      Long—Reserved—set to zero.
Return Value

Long—Nonzero on success, zero on error. Sets GetLastError.


Windows 95, Windows NT


This function returns immediately and the shutdown proceeds in the background. Be sure to terminate your own application to help the shutdown proceed smoothly. Your process must have sufficient privilege to execute this operation.

Porting Notes

Replaces the ExitWindows API call in Win16


ExitWindowsEx EWX_LOGOFF, 0
Unload Me

All of the material presented here is copyrighted by either Desaware or Macmillan. No part of this material may be used or reproduced in any fashion (except in brief quotations used in critical articles and reviews) without prior consent.
Windows NT can not be shutdown easily as is the case with Win95. In Win NT, you must set the privileges. Here is the code:


       Private Const SE_SHUTDOWN_NAME = "SeShutdownPrivilege"
       Private Const SE_PRIVILEGE_ENABLED = &H2
       Private Const EWX_REBOOT = 2
       Private Const EWX_FORCE = 4
       Private Const TOKEN_ADJUST_PRIVILEGES = &H20
       Private Const TOKEN_QUERY = &H8

       Private Type LUID
           LowPart As Long
           HighPart As Long
       End Type

       Private Type LUID_AND_ATTRIBUTES
               LowPart As Long
               HighPart As Long
               Attributes As Long
       End Type

       Private Type TOKEN_PRIVILEGES
           PrivilegeCount As Long
           LowPart As Long
           HighPart As Long
           Attributes As Long
       End Type

       Private Declare Function OpenProcessToken Lib "advapi32.dll" (ByVal ProcessHandle As Long, ByVal DesiredAccess As Long, TokenHandle
       As Long) As Long
       Private Declare Function LookupPrivilegeValue Lib "advapi32.dll" Alias "LookupPrivilegeValueA" (ByVal lpSystemName As String, ByVal
       lpName As String, lpLuid As LUID) As Long
       Private Declare Function AdjustTokenPrivileges Lib "advapi32.dll" (ByVal TokenHandle As Long, ByVal DisableAllPrivileges As Long,
       NewState As TOKEN_PRIVILEGES, ByVal BufferLength As Long, ByVal PreviousState As Long, ByVal ReturnLength As Long) As Long
       Private Declare Function ExitWindowsEx Lib "user32" (ByVal uFlags As Long, ByVal dwReserved As Long) As Long
       Private Declare Function GetCurrentProcess Lib "kernel32" () As Long

       Code to actually do the shutdown:

           Dim tok As Long
           Dim sht As LUID
           Dim st As TOKEN_PRIVILEGES
           Dim success As Long
           success = OpenProcessToken(GetCurrentProcess, TOKEN_QUERY + TOKEN_ADJUST_PRIVILEGES, tok)
           Debug.Print "OpenProcessToken"; success
           success = LookupPrivilegeValue(vbNullString, SE_SHUTDOWN_NAME, sht)
           Debug.Print "LookupPrivilegeValue"; success
           st.PrivilegeCount = 1
           st.LowPart = sht.LowPart
           st.HighPart = sht.HighPart
           st.Attributes = SE_PRIVILEGE_ENABLED
           success = AdjustTokenPrivileges(tok, 0, st, 0, 0, 0)
           Debug.Print "AdjustTokenPrivileges"; success
           success = ExitWindowsEx(EWX_REBOOT, 0)
           Debug.Print "ExitWindowsEx"; success

If you have any difficulties, I can send you a complete working project.

Featured Post

[Webinar] Improve your customer journey

A positive customer journey is important in attracting and retaining business. To improve this experience, you can use Google Maps APIs to increase checkout conversions, boost user engagement, and optimize order fulfillment. Learn how in this webinar presented by Dito.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now