system shutdown

Posted on 1999-07-20
Medium Priority
Last Modified: 2010-05-02
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.
Question by:purushotham
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

Expert Comment

ID: 1526241

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.

Accepted Solution

kamall earned 300 total points
ID: 1526242
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

Industry Leaders: 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!

Question has a verified solution.

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

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses

719 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