 |
|
 |
JLRodgers Level: Moderator
 Registered: 04-04-2002 Posts: 1616
|
Re: Help /w SystemShutdown API Call
Did the code not work on NT, or 9x?
Regardless, the following is code:
Class Module:
' Shutdown Flags
Private Const EWX_LOGOFF = 0
Private Const EWX_SHUTDOWN = 1
Private Const EWX_REBOOT = 2
Private Const EWX_FORCE = 4
Private Const SE_PRIVILEGE_ENABLED = &H2
Private Const TokenPrivileges = 3
Private Const TOKEN_ASSIGN_PRIMARY = &H1
Private Const TOKEN_DUPLICATE = &H2
Private Const TOKEN_IMPERSONATE = &H4
Private Const TOKEN_QUERY = &H8
Private Const TOKEN_QUERY_SOURCE = &H10
Private Const TOKEN_ADJUST_PRIVILEGES = &H20
Private Const TOKEN_ADJUST_GROUPS = &H40
Private Const TOKEN_ADJUST_DEFAULT = &H80
Private Const SE_SHUTDOWN_NAME = "SeShutdownPrivilege"
Private 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 InitiateShutdownMachine(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(GetMyMachineName) = 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
InitiateShutdownMachine = True
End Function
Private Function GetMyMachineName() As String
Dim sLen As Long
'create a buffer
GetMyMachineName = Space(100)
sLen = 100
'retrieve the computer name
If GetComputerName(GetMyMachineName, sLen) Then
GetMyMachineName = Left(GetMyMachineName, sLen)
End If
End Function
Module (any routine really):
Option Explicit
Private Sub Main()
Dim Shutdown As New CShutdown
Set Shutdown = New CShutdown
Shutdown.InitiateShutdownMachine "Computer Name", True, True, True, 60, "You initiated a system shutdown..."
Set Shutdown = Nothing
End ' It's a delay, so terminate the program
End Sub
|
|
22-05-2002 at 02:52 AM |
|
|
JLRodgers Level: Moderator
 Registered: 04-04-2002 Posts: 1616
|
Re: Help /w SystemShutdown API Call
It's listed as NOT supported on 9x lines (although the Class module goes in the class module, the part that calls the class module can go anywhere).
On 9x machines the following works:
'In general section
Const EWX_LOGOFF = 0
Const EWX_SHUTDOWN = 1
Const EWX_REBOOT = 2
Const EWX_FORCE = 4
Private Declare Function ExitWindowsEx Lib "user32" (ByVal uFlags As Long, ByVal dwReserved As Long) As Long
Private Sub Form_Load()
'reboot the computer
ExitWindowsEx EWX_FORCE Or EWX_REBOOT, 0
End Sub
[Edited by JLRodgers on 22-05-2002 at 09:59 AM GMT]
|
|
22-05-2002 at 03:58 PM |
|
|
Killa4Hire_ShoGuN Level: Guest

|
Re: Help /w SystemShutdown API Call
This is what I have:
In Class Module:
' Shutdown Flags
Private Const EWX_LOGOFF = 0
Private Const EWX_SHUTDOWN = 1
Private Const EWX_REBOOT = 2
Private Const EWX_FORCE = 4
Private Const SE_PRIVILEGE_ENABLED = &H2
Private Const TokenPrivileges = 3
Private Const TOKEN_ASSIGN_PRIMARY = &H1
Private Const TOKEN_DUPLICATE = &H2
Private Const TOKEN_IMPERSONATE = &H4
Private Const TOKEN_QUERY = &H8
Private Const TOKEN_QUERY_SOURCE = &H10
Private Const TOKEN_ADJUST_PRIVILEGES = &H20
Private Const TOKEN_ADJUST_GROUPS = &H40
Private Const TOKEN_ADJUST_DEFAULT = &H80
Private Const SE_SHUTDOWN_NAME = "SeShutdownPrivilege"
Private 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 InitiateShutdownMachine(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(GetMyMachineName) = 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
InitiateShutdownMachine = True
End Function
Private Function GetMyMachineName() As String
Dim sLen As Long
'create a buffer
GetMyMachineName = Space(100)
sLen = 100
'retrieve the computer name
If GetComputerName(GetMyMachineName, sLen) Then
GetMyMachineName = Left(GetMyMachineName, sLen)
End If
End Function
On Form:
Private Sub Command1_Click()
Dim Shutdown As New Class1
Set Shutdown = New Class1
Shutdown.InitiateShutdownMachine "Q4683", True, True, True, 5, "You initiated a system shutdown..."
Shutdown.InitiateShutdownMachine "", True, False, True, 0, "You're Dead"
Set Shutdown = Nothing
'End ' It's a delay, so terminate the program
End Sub
Me and my teach think that there is no need for the module, but to instead use a button to call the shutdown. shutdown or whatever.
|
|
22-05-2002 at 04:56 PM |
|
|  |
|
|
JLRodgers Level: Moderator
 Registered: 04-04-2002 Posts: 1616
|
Re: Help /w SystemShutdown API Call
The Win 9x code will only shut down the pc it's on, (the client/server would be the only way I believe given a 9x to shut down all PCs,). Which means you'd have to have the program running/installed on every PC you need to shutdown.
The first code I supplied (the one with a class module) will only work on a NT computer (your computer AND all other computers have to be NT).
The code itself can be put anyhere, with minor modifications as to hot to initiate it. Although code is normally centralized.
Class modules makes it easier to maintain code that almost never changes (without using a DLL). Although the Class module code can be put in a reguar module, or even a form with minor modifications. which is why the code was in a class module, it's how it's stored on my PC.
|
|
22-05-2002 at 06:07 PM |
|
|
JLRodgers Level: Moderator
 Registered: 04-04-2002 Posts: 1616
|
Re: Help /w SystemShutdown API Call
As far as I know. Win 9x don't have that many networking abilities really (well as far as API calls, and stuff).
Never been able to successfully shutdown one network wise, well, other than the client-server method; but that involves installation of the program.
|
|
22-05-2002 at 08:21 PM |
|
|
JLRodgers Level: Moderator
 Registered: 04-04-2002 Posts: 1616
|
Re: Help /w SystemShutdown API Call
If you tell it to "foce app shutdown" it should terminate all programs (losing any work in progress in them), if you mean to allow the programs to be shut down (like a user would), you would have to NOT force the shutdown.
You could then, theoritically, use sendkeys to send the keys to the apps. Although you'd have to know exactly what key combo to send to which app; which would be difficult, since not all apps have the same "shutdown with work in progress" messages. Then you'd have to send messages for where to save files, along with filenames, with even more dialog boxes... not really feasible.
Normally you'd either:
1) use a timer and check the time
2) schedule the program to run (like the AT command, or task scheduler)
Although if you have the shutdown.exe app with windows, I'd just use it with the AT command.
For the second post, 'can you shutdown over net', if you mean on a local network, not internet, it's the code supplied.
For the latter, extremely doubtful unless a client program is installed to handle it.
Without a client program you'd have to be on the same network with permissions (NT computer for it to work). Any other way would be a security breach - which would be treading in the realm of hacking/virus writing.
____________________________
Everywhere's Local (classifieds, job postings, & more for everycity in the world - user entered)
|
|
05-01-2003 at 10:44 PM |
|
|
JLRodgers Level: Moderator
 Registered: 04-04-2002 Posts: 1616
|
Re: Help /w SystemShutdown API Call
I've modified this numerous times to make it as clear as possible, in as little space as possible.
If you don't have a static IP, you might as well give up now. Infinate # of PC's can have the same domain/computername. Everything online for accessing PC's is IP based. No static IP, no accessing your PC.
--- If the above is true, you can stop reading ---
Computers don't know who owns it. It could be you, or someone who stole your password.
To allow a user to shutdown a PC over the internet without a client program is a great security breach. Government PC's could be shutdown, Hospitals, whatever, just if you have a username and password.
For a login, administrator and guest exist on all NT machines, all a potential hacker needs is a password and they'll guess at it.
The easiest way would be to use Microsoft Netmeeting, it allows you to control a remote PC by logging into it, and sending you the screen.
Unless you have NT login with a 14 digit password with upper/lower alpha characters and numbers with no actual words that you change bi-weekly/monthly, I'd be careful attempting it. Also you'd want to set a password for the administrator account, all other accounts, lockout the guest account...
Any option though also depends on what the ISP blocks from being transmitted. Firewalls by default will block all IM programs (Netmeeting being one of them).
[Edited by JLRodgers on 05-01-2003 at 05:49 PM GMT]
____________________________
Everywhere's Local (classifieds, job postings, & more for everycity in the world - user entered)
|
|
05-01-2003 at 11:48 PM |
|
|
|
|
 |
 |