Solved

How to shutdown NT4 fom VB5 App

Posted on 1998-09-11
5
350 Views
Last Modified: 2008-02-01

I'd like to shutdown a computer programmatically.

I've written a program to call ExitWindows(0,0) api, but it only logs off the system.

I'm using VB5 SP3 and Windows NT 4.0 SP3.

Does anybody have a piece of code to perform that operation ?
0
Comment
Question by:edsont
  • 2
  • 2
5 Comments
 
LVL 12

Expert Comment

by:mark2150
ID: 1434443
Just a wild guess. Have you tried doing a SendKeys to the Explorer window? Send it an <Alt F4>, etc.?
0
 
LVL 10

Accepted Solution

by:
viktornet earned 50 total points
ID: 1434444
From User32.dll

ExitWindowsEx(1, 0)

Shuts down windows//////
0
 
LVL 10

Expert Comment

by:viktornet
ID: 1434445
Oh, just to remind you...it takes same parameters as ExitWindows, but it's more powerful.....

Regards,
Viktor Ivanov
0
 

Author Comment

by:edsont
ID: 1434446
Viktor,

I've tried using ExitWindowsEx(1,0), but it hasn't worked.

I've called GetLastError() from Kernel32 and it returned the 1314
error:
 
 A required privilege is not held by the client.

It's strange because I run the test as Administrator user.

How could the app get the needed privilege to start a shutdown ?

Regards,
edsont
0
 

Author Comment

by:edsont
ID: 1434447
I found an article explaining how to shutdown NT using VB:

 
PSS ID Number: Q168796
Article last modified on 09-30-1997
 
WINDOWS:4.0 5.0 7.0 97
 
NT WINDOWS
 

======================================================================
---------------------------------------------------------------------
The information in this article applies to:
 
 - Microsoft Visual Basic Control Creation, Learning, Professional and
   Enterprise Editions for Windows, version 5.0
   on the following platform: x86
 - Microsoft Visual Basic Standard, Professional, and Enterprise Editions,
   32-bit only, for Windows, version 4.0
 - Microsoft Access versions 7.0, 97
 - Microsoft Excel 97 for Windows
 - Microsoft Excel for Windows 95, version 7.0
 - Microsoft Word 97 for Windows
 - Microsoft PowerPoint 97 for Windows
---------------------------------------------------------------------
 
SUMMARY
=======
 
This article demonstrates how to programmatically restart or log off a
computer in Visual Basic. The sample code shown in this article creates a
project that can normally or forcibly perform these functions on a computer
with a Windows 95 or Windows NT operating system. A normal procedure allows
the user the opportunity to close all open programs before executing a
restart or log off. A forced procedure automatically closes all open
programs before executing a restart or log off.
 
MORE INFORMATION
================
 
The Windows API has a function to programmatically shut down and restart
or log off a computer. The Boolean function ExitWindowsEx performs the
following tasks:
 
 - Shut down the system.
 - Shut down and restart the system.
 - Log off and force all running applications to terminate.
 
The function returns a value as soon as the function initiates a shutdown.
The function returns True if the shutdown will be successful and False if
the function fails. Extended error information is available by calling the
function GetLastError.
 
To use this function under Windows NT, enable the SE_SHUTDOWN_NAME security
privilege by using the AdjustTokenPrivileges function. Windows 95 does not
support or require these security privileges.
 
The flag passed to the ExitWindowsEx function determines whether the
function performs a forced or normal log off, shutdown, or a restart. You
can pass the following flags to this function:
 
 - EWX_FORCE - Forcibly terminates processes that do not respond to a shut
   down command.
 
   NOTE: Using this flag can cause data loss because Windows does not send
   the messages WM_QUERYENDSESSION and WM_ENDSESSION to applications
   currently running in the computer. Use this flag only in cases where
   data loss is not critical.
 
 - EWX_LOGOFF - terminates all processes and then logs off the computer.
 
 - EWX_POWEROFF - shuts down the computer and turns off the power in
   systems with a power-off feature. Under Windows NT, the process that
   calls the ExitWindowsEx function with this flag must have the
   SE_SHUTDOWN_NAME privilege. Windows 95 does not support or require
   this privilege.
 
 - EWX_REBOOT   shuts down and then restarts the computer. Under Windows
   NT, the process that calls the ExitWindowsEx function with this flag
   must have the SE_SHUTDOWN_NAME privilege. Windows 95 does not support
   or require this privilege.
 
 - EWX_SHUTDOWN - shuts down the computer to a point that is safe to turn
   off the power. All file buffers have been flushed to disk, and all
   running processes have stopped. Under Windows NT, the process that
   calls the ExitWindowsEx function with this flag must have the
   SE_SHUTDOWN_NAME privilege. Windows 95 does not support or require
   this privilege.
 
Sample Program
--------------
 
The sample program illustrates how you can use the ExitWindowsEx function
to perform a shut down or log off. The project has a form with four
command buttons. The program begins by determining the operating system
through the GetVersion function. The operating system determines whether
privileges should be set in order to perform a log off or shut down. The
operating system version is stored in a variable and is then displayed in
a message box. Close the message box and a form appears with four command
buttons. The command buttons perform the following tasks:
 
1. Log Off - calls the ExitWindowsEx function with the EWX_Logoff flag to
   log off the user. The user can shut down all running programs.
 
2. Forced Log off - calls the ExitWindowsEx function with the EWX_Logoff or
   the EWX_Forced flags to initiate a forced log off.
 
3. Shut Down - if the operating system is Windows NT, the correct privilege
   is set. A message box then displays the last error value of the process
   thread. Otherwise, the ExitWindowsEx function is called with the
   EWX_Shutdown flag.
 
4. Forced Shut Down - if the operating system is Windows NT, the correct
   privilege is set. A message box then displays the last error value of
   the process thread. Otherwise, the ExitWindowsEx function is called with
   the EWX_Shutdown or the EWX_Force flags to initiate a forced shutdown.
 
Steps To Reproduce Behavior
---------------------------
 
1. Start the development environment and add a new form (Access and Visual
   Basic) or UserForm (other Office 97 programs). In Excel 95, you can call
   the code from the Tools|Macro menu.
 
2. Place the following objects on the form (or UserForm) and set the
   appropriate properties:
 
   Control          Name               Caption
   ---------------------------------------------------
   Command Button   cmdLogoff          Log Off
   Command Button   cmdForceLogoff     Force Log Off
   Command Button   cmdShutdown        Shut Down
   Command Button   cmdForceShutdown   Forced Shut Down
 
3. Copy and paste the following code to the form's Module:
 
      Option Explicit
      Private Const EWX_LogOff As Long = 0
      Private Const EWX_SHUTDOWN As Long = 1
      Private Const EWX_REBOOT As Long = 2
      Private Const EWX_FORCE As Long = 4
      Private Const EWX_POWEROFF As Long = 8
 
      'The ExitWindowsEx function either logs off, shuts down, or shuts
      'down and restarts the system.
      Private Declare Function ExitWindowsEx Lib "user32" _
         (ByVal dwOptions As Long, _
          ByVal dwReserved As Long) As Long
 
      'The GetLastError function returns the calling thread's last-error
      'code value. The last-error code is maintained on a per-thread basis.
      'Multiple threads do not overwrite each other's last-error code.
      Private Declare Function GetLastError Lib "kernel32" () As Long
 
      Private Const mlngWindows95 = 0
      Private Const mlngWindowsNT = 1
 
      Public glngWhichWindows32 As Long
 
      'The GetVersion function returns the operating system in use.
      Private Declare Function GetVersion Lib "kernel32" () As Long
 
      Private Type LUID
         UsedPart As Long
         IgnoredForNowHigh32BitPart As Long
      End Type
 
      Private Type LUID_AND_ATTRIBUTES
         TheLuid As LUID
         Attributes As Long
      End Type
 
      Private Type TOKEN_PRIVILEGES
         PrivilegeCount As Long
         TheLuid As LUID
         Attributes As Long
      End Type
 
      'The GetCurrentProcess function returns a pseudohandle for the
      'current process.
      Private Declare Function GetCurrentProcess Lib "kernel32" () As Long
 
      'The OpenProcessToken function opens the access token associated with
      'a process.
      Private Declare Function OpenProcessToken Lib "advapi32" _
         (ByVal ProcessHandle As Long, _
          ByVal DesiredAccess As Long, _
          TokenHandle As Long) As Long
 
      'The LookupPrivilegeValue function retrieves the locally unique
      'identifier (LUID) used on a specified system to locally represent
      'the specified privilege name.
      Private Declare Function LookupPrivilegeValue Lib "advapi32" _
         Alias "LookupPrivilegeValueA" _
         (ByVal lpSystemName As String, _
          ByVal lpName As String, _
          lpLuid As LUID) As Long
 
      'The AdjustTokenPrivileges function enables or disables privileges
      'in the specified access token. Enabling or disabling privileges
      'in an access token requires TOKEN_ADJUST_PRIVILEGES access.
      Private Declare Function AdjustTokenPrivileges Lib "advapi32" _
         (ByVal TokenHandle As Long, _
          ByVal DisableAllPrivileges As Long, _
          NewState As TOKEN_PRIVILEGES, _
          ByVal BufferLength As Long, _
          PreviousState As TOKEN_PRIVILEGES, _
          ReturnLength As Long) As Long
 
      Private Declare Sub SetLastError Lib "kernel32" _
         (ByVal dwErrCode As Long)
 
      Private Sub AdjustToken()
 
      '********************************************************************
      '* This procedure sets the proper privileges to allow a log off or a
      '* shut down to occur under Windows NT.
      '********************************************************************
 
         Const TOKEN_ADJUST_PRIVILEGES = &H20
         Const TOKEN_QUERY = &H8
         Const SE_PRIVILEGE_ENABLED = &H2
 
         Dim hdlProcessHandle As Long
         Dim hdlTokenHandle As Long
         Dim tmpLuid As LUID
         Dim tkp As TOKEN_PRIVILEGES
         Dim tkpNewButIgnored As TOKEN_PRIVILEGES
         Dim lBufferNeeded As Long
 
         'Set the error code of the last thread to zero using the
         'SetLast Error function. Do this so that the GetLastError
         'function does not return a value other than zero for no
         'apparent reason.
         SetLastError 0
 
         'Use the GetCurrentProcess function to set the hdlProcessHandle
         'variable.
         hdlProcessHandle = GetCurrentProcess()
 
         If GetLastError <> 0 Then
            MsgBox "GetCurrentProcess error==" & GetLastError
         End If
 
         OpenProcessToken hdlProcessHandle, _
            (TOKEN_ADJUST_PRIVILEGES Or TOKEN_QUERY), hdlTokenHandle
 
         If GetLastError <> 0 Then
            MsgBox "OpenProcessToken error==" & GetLastError
         End If
 
         'Get the LUID for shutdown privilege
         LookupPrivilegeValue "", "SeShutdownPrivilege", tmpLuid
 
         If GetLastError <> 0 Then
            MsgBox "LookupPrivilegeValue error==" & GetLastError
         End If
 
         tkp.PrivilegeCount = 1    ' One privilege to set
         tkp.TheLuid = tmpLuid
         tkp.Attributes = SE_PRIVILEGE_ENABLED
 
         'Enable the shutdown privilege in the access token of this process
         AdjustTokenPrivileges hdlTokenHandle, _
                               False, _
                               tkp, _
                               Len(tkpNewButIgnored), _
                               tkpNewButIgnored, _
                               lBufferNeeded
 
         If GetLastError <> 0 Then
            MsgBox "AdjustTokenPrivileges error==" & GetLastError
         End If
 
      End Sub
 
      Private Sub cmdLogoff_Click()
 
         ExitWindowsEx (EWX_LogOff), &HFFFF
         MsgBox "ExitWindowsEx's GetLastError " & GetLastError
 
      End Sub
 
      Private Sub cmdForceLogoff_Click()
 
         ExitWindowsEx (EWX_LogOff Or EWX_FORCE), &HFFFF
         MsgBox "ExitWindowsEx's GetLastError " & GetLastError
 
      End Sub
 
      Private Sub cmdShutdown_Click()
 
         If glngWhichWindows32 = mlngWindowsNT Then
            AdjustToken
            MsgBox "Post-AdjustToken GetLastError " & GetLastError
         End If
 
         ExitWindowsEx (EWX_SHUTDOWN), &HFFFF
         MsgBox "ExitWindowsEx's GetLastError " & GetLastError
 
      End Sub
 
      Private Sub cmdForceShutdown_Click()
         If glngWhichWindows32 = mlngWindowsNT Then
            AdjustToken
            MsgBox "Post-AdjustToken GetLastError " & GetLastError
         End If
 
      ExitWindowsEx (EWX_SHUTDOWN Or EWX_FORCE), &HFFFF
      MsgBox "ExitWindowsEx's GetLastError " & GetLastError
 
      End Sub
 
      Private Sub Form_Load()
      '********************************************************************
      '* When the project starts, check the operating system used by
      '* calling the GetVersion function.
      '********************************************************************
         Dim lngVersion As Long
 
         lngVersion = GetVersion()
 
         If ((lngVersion And &H80000000) = 0) Then
            glngWhichWindows32 = mlngWindowsNT
            MsgBox "Running Windows NT"
         Else
            glngWhichWindows32 = mlngWindows95
            MsgBox "Running Windows 95"
         End If
 
      End Sub
 
4. To run the code:
 
   - In Visual Basic, run the project or compile to an EXE and run the EXE.
 
   - In Access, open the form.
 
   - In the other Office 97 applications, add a macro to show the UserForm:
 
      Sub ShowShutDownForm()
        UserForm1.Show
      End Sub
 
You can run this code from the Tools|Macro menu.
 
Also, in the UserForm, the last procedure's declaration needs to be
changed from:
 
   Private Sub Form_Load()
 
to:
 
   Sub Form_Initialize()
 
 - In Excel 95, you can assign the code to be run from a Dialog or from the
   Tools|Macro menu.
 
Notes
-----
 
If you aren't implementing the code on a form, the code contained in the
Form_Load must be run prior to the other code.
 
Make sure you have saved all running programs before clicking a command
button.
 
In Windows 95, your application may remain visible when executing the
logoff and force logoff code. You may want to close your application after
the API call returns. This is not a problem with the shutdown or force
shutdown code.
 
REFERENCES
==========
 
Win32 Programmer's Reference-Search on the following topics:
 
 - AdjustTokenPrivileges
 - ExitWindowsEx
 - GetLastError
 - Privileges
 - Logging Off or Shutting Down
 
Additional query words: Log Off Shut Down ExitWindowsEx
======================================================================
Keywords          : vb432 VB4WIN vb5all vb5howto VBKBWinAPI vbwin GnrlVb kbprg
Technology        : kbvba
Version           : WINDOWS:4.0 5.0 7.0 97
Platform          : NT WINDOWS
Hardware          : x86
Issue type        : kbhowto
=============================================================================
Copyright Microsoft Corporation 1997.


 

0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

762 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now