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.
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.


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.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.