how to shutdown computer using vb.net

kanak

Member
Joined
Jan 9, 2005
Messages
16
hey does anyone know how to shutdown a computer using the code of vb.net.
can anyone help me with the code
thanks in advance
 
is there any way to do this without an API?

if you can do everything without APIs, then your program will be able to work on linux when they get the .net framework on there, as i understand it...
 
The .NET Framework uses Apis within its self anyway , they are internal ( inside most of the .net dlls ) , you will have to use Api ( whether it be adding them in to a form , or using the internal ( private ) apis via Type.InvokeMethod.
anyway the Api way you will want to set the Privilages to allow you to shut down the pc ( if running an NT based system , ie: Windows XP ) , try this ...
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
	Private Const EWX_FORCE As Int32 = 4
	Private Const EWX_SHUTDOWN As Int32 = 1
	Private Const EWX_REBOOT As Int32 = 2
	Private Const EWX_LOGOFF As Int32 = 0
 
	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 btn_ShutDown_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_ShutDown.Click
		ShutDown()
	End Sub
 
	Private Sub btn_Restart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_Restart.Click
		Restart()
	End Sub
 
	Private Sub Restart()
		If MessageBox.Show("Would you like to re-boot the system", "", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) = DialogResult.OK Then
			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, False, token, System.Runtime.InteropServices.Marshal.SizeOf(blan  k_token), blank_token, ret_length)
 
					ExitWindowsEx(EWX_LOGOFF Or EWX_FORCE Or EWX_REBOOT, &HFFFF)
 
				Case Else
					ExitWindowsEx(EWX_SHUTDOWN Or EWX_FORCE Or EWX_REBOOT, &HFFFF)
			End Select
		End If
	End Sub
	Private Sub ShutDown()
		If MessageBox.Show("Would you like to shut down the system", "", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) = DialogResult.OK Then
			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(blan  k_token), blank_token, ret_length)
 
					ExitWindowsEx(EWX_SHUTDOWN Or EWX_FORCE, &HFFFF)
 
				Case Else
					ExitWindowsEx(EWX_SHUTDOWN Or EWX_FORCE, &HFFFF)
			End Select
		End If
	End Sub
 
Or just do this:

Code:
Sub ShutDown()

        Dim nLogOff = 0
        Dim nReboot = 2
        Dim nForceLogOff = 4
        Dim nForceReboot = 6
        Dim nPowerDown = 8
        Dim nForcePowerDown = 12

        Dim oOS As Object = GetObject("winmgmts:{(Shutdown)}").ExecQuery("Select * from Win32_OperatingSystem")
        Dim oOperatingSystem As Object

        For Each oOperatingSystem In oOS
            oOperatingSystem.Win32Shutdown(nForcePowerDown)
        Next

    End Sub
 
This assumes WMI is turned on for that. Use the API if you want the function to still work on computer like mine where I have WMI turned off.
 
I didnt turn WMI on on mine and it works

It is also running on my Bedtime Monitor program, and it is turning PCs off on kids at bedtime all over the world

Is WMI something you have to go shut off?
 
Yes, its on by default in 2000 and XP, it wasnt present until 98 SE if I know the history right. NT 4 it was there, but many components of it were missing. Some places that I have worked have it turned off because of security reasons, but thats way beyond any at home users, you shouldnt need to worry really.
 
Dont know if anyones still interested in this thread but you can use Process.Start to initiate a shutdown. Sounds a bit like using the Start menu to shutdown doesnt it. There is an executable file in the system32 folder named shutdown.exe. It takes various command line arguments that enable it to do various things. I have three shortcuts on my desktop that use it that allow me to log off, restart or shut down with one click. Use the command prompt to get all the details about the arguments and use Process.Start("shutdown", <arguments>) to initiate it.
 
jmcilhinney said:
Dont know if anyones still interested in this thread but you can use Process.Start to initiate a shutdown. Sounds a bit like using the Start menu to shutdown doesnt it. There is an executable file in the system32 folder named shutdown.exe. It takes various command line arguments that enable it to do various things. I have three shortcuts on my desktop that use it that allow me to log off, restart or shut down with one click. Use the command prompt to get all the details about the arguments and use Process.Start("shutdown", <arguments>) to initiate it.
Interesting, but it is not available in all versions of windows, and just like the ExitWindowsEx API, it is almost guarunteed to not work in Mono.
 
Back
Top