|
-
Oct 30th, 2008, 09:35 AM
#1
Thread Starter
New Member
How to shut down...
Is there a way in Visual Basic to program a button to shut off the computer? 
I have visual basic 2005 and 2008.
-
Oct 30th, 2008, 09:36 AM
#2
-
Oct 30th, 2008, 09:38 AM
#3
Hyperactive Member
Re: How to shut down...
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
-
Oct 30th, 2008, 09:45 AM
#4
Re: How to shut down...
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)
-
Oct 30th, 2008, 09:46 AM
#5
Hyperactive Member
Re: How to shut down...
Yeah - you could do that too.
-
Oct 30th, 2008, 07:40 PM
#6
Re: How to shut down...
 Originally Posted by dbasnett
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)
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:
vb.net Code:
Process.Start("shutdown", "-s -t 5")
-
Oct 30th, 2008, 09:31 PM
#7
Re: How to shut down...
noted - it was a modify / test / copy / paste of something else.
-
Oct 31st, 2008, 12:03 AM
#8
Re: How to shut down...
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|