Is there a way in Visual Basic to program a button to shut off the computer? :)
I have visual basic 2005 and 2008.
Printable View
Is there a way in Visual Basic to program a button to shut off the computer? :)
I have visual basic 2005 and 2008.
Moved to VB.NET
Yes there is - but it is not that simple.
Code:Private Declare Function GetCurrentProcess Lib "kernel32.dll" () As IntPtr
Private Declare Function OpenProcessToken Lib "advapi32.dll" (ByVal ProcessHandle As IntPtr, ByVal DesiredAccess As Int32, ByRef TokenHandle As IntPtr) As Int32
Private Declare Function LookupPrivilegeValue Lib "advapi32.dll" Alias "LookupPrivilegeValueA" (ByVal lpSystemName As String, ByVal lpName As String, ByRef lpLuid As Luid) As Int32
Private Declare Function AdjustTokenPrivileges Lib "advapi32.dll" (ByVal TokenHandle As IntPtr, ByVal DisableAllPrivileges As Int32, ByRef NewState As TOKEN_PRIVILEGES, ByVal BufferLength As Int32, ByRef PreviousState As TOKEN_PRIVILEGES, ByRef ReturnLength As Int32) As Int32
Private Declare Function ExitWindowsEx Lib "user32.dll" (ByVal uFlags As Int32, ByVal dwReserved As Int32) As Int32
Const SE_PRIVILEGE_ENABLED As Integer = &H2
Const TOKEN_QUERY As Integer = &H8
Const TOKEN_ADJUST_PRIVILEGES As Integer = &H20
Const SE_SHUTDOWN_NAME As String = "SeShutdownPrivilege"
Const EWX_LOGOFF As Integer = &H0
Const EWX_SHUTDOWN As Integer = &H1
Const EWX_REBOOT As Integer = &H2
Const EWX_FORCE As Integer = &H4
Const EWX_POWEROFF As Integer = &H8
Const EWX_FORCEIFHUNG As Integer = &H10
Public Structure LUID
Dim LowPart As Int32
Dim HighPart As Int32
End Structure
Public Structure TOKEN_PRIVILEGES
Public PrivilegeCount As Integer
Public Privileges As Luid
Public Attributes As Int32
End Structure
Private Sub ShutDown()
Dim platform As New PlatformID
Select Case Environment.OSVersion.Platform
Case PlatformID.Win32NT
Dim token As TOKEN_PRIVILEGES
Dim blank_token As TOKEN_PRIVILEGES
Dim token_handle As IntPtr
Dim uid As LUID
Dim ret_length As Integer
Dim ptr As IntPtr = GetCurrentProcess() '/// get the process handle
OpenProcessToken(ptr, &H20 Or &H8, token_handle)
LookupPrivilegeValue("", "SeShutdownPrivilege", uid)
token.PrivilegeCount = 1
token.Privileges = uid
token.Attributes = &H2
AdjustTokenPrivileges(token_handle, 0, token, System.Runtime.InteropServices.Marshal.SizeOf(blank_token), blank_token, ret_length)
ExitWindowsEx(EWX_SHUTDOWN Or EWX_FORCE, &HFFFF)
Case Else
ExitWindowsEx(EWX_SHUTDOWN Or EWX_FORCE, &HFFFF)
End Select
End Sub
or
Code:'to stop shutdown
'open a command window and enter
'shutdown -a
Dim extProc As ProcessStartInfo
extProc = New ProcessStartInfo("shutdown", "-s -t 5")
extProc.UseShellExecute = True
System.Diagnostics.Process.Start(extProc)
Yeah - you could do that too. :bigyello:
Just one point to note here that isn't specifically on-topic. There's not much point setting UseShellExecute to True when you're executing an EXE file. The whole point of UseShellExecute is to get data files to open in their default application. If the file you specify is not a data file then UseShellExecute has no effect. The simplest way is just:Quote:
Originally Posted by dbasnett
vb.net Code:
Process.Start("shutdown", "-s -t 5")
noted - it was a modify / test / copy / paste of something else.
I should also point out that UseShellExecute is True by default. You'd only set it to False if you were starting an EXE and wanted to redirect standard streams, i.e. stdin, stdout and stderror.