sashi 1,754
Recognized Expert Top Contributor Shut down Windows
For various reasons you may require a shut down of Windows to happen programmatically. For instance if the installation of your program requires system reconfiguration. The following code will do this for you with options to Reboot, etc. -
'Module code - modShutdown
-
-
' Shutdown Flags
-
Const EWX_LOGOFF = 0
-
Const EWX_SHUTDOWN = 1
-
Const EWX_REBOOT = 2
-
Const EWX_FORCE = 4
-
Const SE_PRIVILEGE_ENABLED = &H2
-
Const TokenPrivileges = 3
-
Const TOKEN_ASSIGN_PRIMARY = &H1
-
Const TOKEN_DUPLICATE = &H2
-
Const TOKEN_IMPERSONATE = &H4
-
Const TOKEN_QUERY = &H8
-
Const TOKEN_QUERY_SOURCE = &H10
-
Const TOKEN_ADJUST_PRIVILEGES = &H20
-
Const TOKEN_ADJUST_GROUPS = &H40
-
Const TOKEN_ADJUST_DEFAULT = &H80
-
Const SE_SHUTDOWN_NAME = "SeShutdownPrivilege"
-
Const ANYSIZE_ARRAY = 1
-
Private Type LARGE_INTEGER
-
lowpart As Long
-
highpart As Long
-
End Type
-
Private Type Luid
-
lowpart As Long
-
highpart As Long
-
End Type
-
Private Type LUID_AND_ATTRIBUTES
-
'pLuid As Luid
-
pLuid As LARGE_INTEGER
-
Attributes As Long
-
End Type
-
Private Type TOKEN_PRIVILEGES
-
PrivilegeCount As Long
-
Privileges(ANYSIZE_ARRAY) As LUID_AND_ATTRIBUTES
-
End Type
-
Private Declare Function InitiateSystemShutdown Lib "advapi32.dll" Alias "InitiateSystemShutdownA" (ByVal lpMachineName As String, ByVal lpMessage As String, ByVal dwTimeout As Long, ByVal bForceAppsClosed As Long, ByVal bRebootAfterShutdown As Long) As Long
-
Private Declare Function OpenProcessToken Lib "advapi32.dll" (ByVal ProcessHandle As Long, ByVal DesiredAccess As Long, TokenHandle As Long) As Long
-
Private Declare Function GetCurrentProcess Lib "kernel32" () As Long
-
Private Declare Function LookupPrivilegeValue Lib "advapi32.dll" Alias "LookupPrivilegeValueA" (ByVal lpSystemName As String, ByVal lpName As String, lpLuid As LARGE_INTEGER) 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, PreviousState As TOKEN_PRIVILEGES, ReturnLength As Long) As Long
-
Private Declare Function GetComputerName Lib "kernel32" Alias "GetComputerNameA" (ByVal lpBuffer As String, nSize As Long) As Long
-
Private Declare Function GetLastError Lib "kernel32" () As Long
-
-
Public Function InitiateShutdown(ByVal Machine As String, _
-
Optional Force As Variant, _
-
Optional Restart As Variant, _
-
Optional AllowLocalShutdown As Variant, _
-
Optional Delay As Variant, _
-
Optional Message As Variant) As Boolean
-
-
Dim hProc As Long
-
Dim OldTokenStuff As TOKEN_PRIVILEGES
-
Dim OldTokenStuffLen As Long
-
Dim NewTokenStuff As TOKEN_PRIVILEGES
-
Dim NewTokenStuffLen As Long
-
Dim pSize As Long
-
If IsMissing(Force) Then Force = False
-
If IsMissing(Restart) Then Restart = True
-
If IsMissing(AllowLocalShutdown) Then AllowLocalShutdown = False
-
If IsMissing(Delay) Then Delay = 0
-
If IsMissing(Message) Then Message = ""
-
'Make sure the Machine-name doesn't start with '\\'
-
If InStr(Machine, "\\") = 1 Then
-
Machine = Right(Machine, Len(Machine) - 2)
-
End If
-
'check if it's the local machine that's going to be shutdown
-
If (LCase(GetMachineName) = LCase(Machine)) Then
-
'may we shut this computer down?
-
If AllowLocalShutdown = False Then Exit Function
-
'open access token
-
If OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES Or TOKEN_QUERY, hProc) = 0 Then
-
MsgBox "OpenProcessToken Error: " & GetLastError()
-
Exit Function
-
End If
-
'retrieve the locally unique identifier to represent the Shutdown-privilege name
-
If LookupPrivilegeValue(vbNullString, SE_SHUTDOWN_NAME, OldTokenStuff.Privileges(0).pLuid) = 0 Then
-
MsgBox "LookupPrivilegeValue Error: " & GetLastError()
-
Exit Function
-
End If
-
NewTokenStuff = OldTokenStuff
-
NewTokenStuff.PrivilegeCount = 1
-
NewTokenStuff.Privileges(0).Attributes = SE_PRIVILEGE_ENABLED
-
NewTokenStuffLen = Len(NewTokenStuff)
-
pSize = Len(NewTokenStuff)
-
'Enable shutdown-privilege
-
If AdjustTokenPrivileges(hProc, False, NewTokenStuff, NewTokenStuffLen, OldTokenStuff, OldTokenStuffLen) = 0 Then
-
MsgBox "AdjustTokenPrivileges Error: " & GetLastError()
-
Exit Function
-
End If
-
'initiate the system shutdown
-
If InitiateSystemShutdown("\\" & Machine, Message, Delay, Force, Restart) = 0 Then
-
Exit Function
-
End If
-
NewTokenStuff.Privileges(0).Attributes = 0
-
'Disable shutdown-privilege
-
If AdjustTokenPrivileges(hProc, False, NewTokenStuff, Len(NewTokenStuff), OldTokenStuff, Len(OldTokenStuff)) = 0 Then
-
Exit Function
-
End If
-
Else
-
'initiate the system shutdown
-
If InitiateSystemShutdown("\\" & Machine, Message, Delay, Force, Restart) = 0 Then
-
Exit Function
-
End If
-
End If
-
InitiateShutdown = True
-
End Function
-
-
Function GetMachineName() As String
-
Dim sLen As Long
-
'create a buffer
-
GetMachineName = Space(100)
-
sLen = 100
-
'retrieve the computer name
-
If GetComputerName(GetMachineName, sLen) Then
-
GetMachineName = Left(GetMachineName, sLen)
-
End If
-
End Function
-
-
-
'Form code - frmShutdown
-
-
Private Sub cmdShutdownNow_Click()
-
modShutdown.InitiateShutdown GetMachineName, True, False, True, 60, "Message to state reason for shutdown!"
-
End Sub
-
6 10526
Does this work on all windows Os or xp only ....?
It does not state
is this reliable to windows vista?
avoid more codes and make easier with less codes
i want to write a program in VB6.0 for many users so that each usher has a separate password from another user. I want the users to login only three times after which their password will become invalid. I need your help on how to write the code. One textbox will be used for all of them.
I need your help on how to write a VB code that can save the content of a form so that it can be retrieved after shutting down the system or after exitting the VB environment. Please, your help is highly and urgently needed. Thanks
plz add the shutdown coding to lern others
of course you can also just run that one executive in the windows folder that shuts down windows...but i believe it only works on xp and below...or maybe its 2000 and below
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Jesper DK |
last post by:
Hi,
I would like a program invoked when the computer is shut
down or a user logs off. Is there a fix way of doing
this. I uses the startup folder to run the program, but I
need to run it when...
|
by: Claire |
last post by:
Running XP pro, SP2. Visual studio .NET 2003. App written in C#
I have written an application that hides itself when run and shows a
notification icon in the system tray.
If the main form is...
|
by: Diogo Alves - Software Developer |
last post by:
Hi,
I have a application developed by me in C# that does not let windows shut
down!
What is the problem? is this a Framework bug?
I would like to receive some feedback about this issue
|
by: Mark Rae |
last post by:
Hi,
I wrote a Windows service for a client a few months ago, and the client has
now asked me to modify it so that it shuts itself down under certain
circumstances e.g. a catastrophic failure of...
|
by: pamelafluente |
last post by:
Hi Guys,
I have a small application which uses a NotifyIcon. The user can set a
flag (PreventClosing ) so that when he clicks on the form-cancel button
"X", the program will instead be...
|
by: Jeff S |
last post by:
I'm creating a new Windows Forms MDI application...
How can I add cause the application to shut itself down after a period of
?
I did this in an old COM/VB6 application and I had to have code...
|
by: ArkJ |
last post by:
Hello.
I have a little problem. I created a little Service which uses SIP, all works rather well, but when I want to shut it down in the Services panel, it looks as if it's shut down, but in fact...
|
by: =?Utf-8?B?R2xlbm4=?= |
last post by:
i just upgraded from XP to Vista and so far am kicking myself for it. After I
installed and got all my drivers updated i went to shut down windows and it
will not shut down. It closes all programs...
|
by: Sheena777 |
last post by:
I have a program that I have created and that is permanently running in the back ground on a user's PC. This program is used to check if a finger print scanner is on and if not it will be restarted...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
| |