how to shutdown computer using vb.net

kanak

Member
Joined
Jan 9, 2005
Messages
17
Programming Experience
Beginner
can anyone tell me hw to shut down a computer using vb.net or restart the windows..if possible help me with the code
 
you need to use the ExitWindowsEX api , first you will need to check the OS version ( if it's NT based , which XP is anyway ) , here's an example i put together quite some time ago, just dug it up for you.
VB.NET:
[font=Courier New][color=#0000ff]	Private[/color] [color=#0000ff]Declare[/color] [color=#0000ff]Function[/color] GetCurrentProcess [color=#0000ff]Lib[/color] "kernel32.dll" () [color=#0000ff]As[/color] IntPtr
	[color=#0000ff]Private[/color] [color=#0000ff]Declare[/color] [color=#0000ff]Function[/color] OpenProcessToken [color=#0000ff]Lib[/color] "advapi32.dll" ([color=#0000ff]ByVal[/color] ProcessHandle [color=#0000ff]As[/color] IntPtr, [color=#0000ff]ByVal[/color] DesiredAccess [color=#0000ff]As[/color] Int32, [color=#0000ff]ByRef[/color] TokenHandle [color=#0000ff]As[/color] IntPtr) [color=#0000ff]As[/color] Int32
	[color=#0000ff]Private[/color] [color=#0000ff]Declare[/color] [color=#0000ff]Function[/color] LookupPrivilegeValue [color=#0000ff]Lib[/color] "advapi32.dll" [color=#0000ff]Alias[/color] "LookupPrivilegeValueA" ([color=#0000ff]ByVal[/color] lpSystemName [color=#0000ff]As[/color] [color=#0000ff]String[/color], [color=#0000ff]ByVal[/color] lpName [color=#0000ff]As[/color] [color=#0000ff]String[/color], [color=#0000ff]ByRef[/color] lpLuid [color=#0000ff]As[/color] LUID) [color=#0000ff]As[/color] Int32
	[color=#0000ff]Private[/color] [color=#0000ff]Declare[/color] [color=#0000ff]Function[/color] AdjustTokenPrivileges [color=#0000ff]Lib[/color] "advapi32.dll" ([color=#0000ff]ByVal[/color] TokenHandle [color=#0000ff]As[/color] IntPtr, [color=#0000ff]ByVal[/color] DisableAllPrivileges [color=#0000ff]As[/color] Int32, [color=#0000ff]ByRef[/color] NewState [color=#0000ff]As[/color] TOKEN_PRIVILEGES, [color=#0000ff]ByVal[/color] BufferLength [color=#0000ff]As[/color] Int32, [color=#0000ff]ByRef[/color] PreviousState [color=#0000ff]As[/color] TOKEN_PRIVILEGES, [color=#0000ff]ByRef[/color] ReturnLength [color=#0000ff]As[/color] Int32) [color=#0000ff]As[/color] Int32
 
	[color=#0000ff]Private[/color] [color=#0000ff]Declare[/color] [color=#0000ff]Function[/color] ExitWindowsEx [color=#0000ff]Lib[/color] "user32.dll" ([color=#0000ff]ByVal[/color] uFlags [color=#0000ff]As[/color] Int32, [color=#0000ff]ByVal[/color] dwReserved [color=#0000ff]As[/color] Int32) [color=#0000ff]As[/color] Int32
	[color=#0000ff]Private[/color] [color=#0000ff]Const[/color] EWX_FORCE [color=#0000ff]As[/color] Int32 = [color=#008000]4[/color]
	[color=#0000ff]Private[/color] [color=#0000ff]Const[/color] EWX_SHUTDOWN [color=#0000ff]As[/color] Int32 = [color=#008000]1[/color]
	[color=#0000ff]Private[/color] [color=#0000ff]Const[/color] EWX_REBOOT [color=#0000ff]As[/color] Int32 = [color=#008000]2[/color]
	[color=#0000ff]Private[/color] [color=#0000ff]Const[/color] EWX_LOGOFF [color=#0000ff]As[/color] Int32 = [color=#008000]0[/color]
 
	[color=#0000ff]Public[/color] [color=#0000ff]Structure[/color] LUID
		[color=#0000ff]Dim[/color] LowPart [color=#0000ff]As[/color] Int32
		[color=#0000ff]Dim[/color] HighPart [color=#0000ff]As[/color] Int32
	[color=#0000ff]End[/color] [color=#0000ff]Structure[/color]
 
	[color=#0000ff]Public[/color] [color=#0000ff]Structure[/color] TOKEN_PRIVILEGES
		[color=#0000ff]Public[/color] PrivilegeCount [color=#0000ff]As[/color] [color=#0000ff]Integer[/color]
		[color=#0000ff]Public[/color] Privileges [color=#0000ff]As[/color] LUID
		[color=#0000ff]Public[/color] Attributes [color=#0000ff]As[/color] Int32
	[color=#0000ff]End[/color] [color=#0000ff]Structure[/color]
 
	[color=#0000ff]Private[/color] [color=#0000ff]Sub[/color] btn_ShutDown_Click([color=#0000ff]ByVal[/color] sender [color=#0000ff]As[/color] System.Object, [color=#0000ff]ByVal[/color] e [color=#0000ff]As[/color] System.EventArgs) [color=#0000ff]Handles[/color] btn_ShutDown.Click
		ShutDown()
	[color=#0000ff]End[/color] [color=#0000ff]Sub[/color]
 
	[color=#0000ff]Private[/color] [color=#0000ff]Sub[/color] btn_Restart_Click([color=#0000ff]ByVal[/color] sender [color=#0000ff]As[/color] System.Object, [color=#0000ff]ByVal[/color] e [color=#0000ff]As[/color] System.EventArgs) [color=#0000ff]Handles[/color] btn_Restart.Click
		Restart()
	[color=#0000ff]End[/color] [color=#0000ff]Sub[/color]
 
	[color=#0000ff]Private[/color] [color=#0000ff]Sub[/color] Restart()
		[color=#0000ff]If[/color] MessageBox.Show("Would you like to re-boot the system", "", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) = DialogResult.OK [color=#0000ff]Then[/color]
			[color=#0000ff]Dim[/color] platform [color=#0000ff]As[/color] [color=#0000ff]New[/color] PlatformID
			[color=#0000ff]Select[/color] [color=#0000ff]Case[/color] Environment.OSVersion.Platform
				[color=#0000ff]Case[/color] PlatformID.Win32NT
					[color=#0000ff]Dim[/color] token [color=#0000ff]As[/color] TOKEN_PRIVILEGES
					[color=#0000ff]Dim[/color] blank_token [color=#0000ff]As[/color] TOKEN_PRIVILEGES
					[color=#0000ff]Dim[/color] token_handle [color=#0000ff]As[/color] IntPtr
					[color=#0000ff]Dim[/color] uid [color=#0000ff]As[/color] LUID
					[color=#0000ff]Dim[/color] ret_length [color=#0000ff]As[/color] [color=#0000ff]Integer[/color]
					[color=#0000ff]Dim[/color] ptr [color=#0000ff]As[/color] IntPtr = GetCurrentProcess() [color=#008000]'/// get the process handle[/color]
 
					OpenProcessToken(ptr, &H20 [color=#0000ff]Or[/color] &H8, token_handle)
					LookupPrivilegeValue("", "SeShutdownPrivilege", uid)
					token.PrivilegeCount = [color=#008000]1[/color]
					token.Privileges = uid
					token.Attributes = &H2
 
					AdjustTokenPrivileges(token_handle, [color=#0000ff]False[/color], token, System.Runtime.InteropServices.Marshal.SizeOf(blank_token), blank_token, ret_length)
 
					ExitWindowsEx(EWX_LOGOFF [color=#0000ff]Or[/color] EWX_FORCE [color=#0000ff]Or[/color] EWX_REBOOT, &HFFFF)
 
				[color=#0000ff]Case[/color] [color=#0000ff]Else[/color]
					ExitWindowsEx(EWX_SHUTDOWN [color=#0000ff]Or[/color] EWX_FORCE [color=#0000ff]Or[/color] EWX_REBOOT, &HFFFF)
			[color=#0000ff]End[/color] [color=#0000ff]Select[/color]
		[color=#0000ff]End[/color] [color=#0000ff]If[/color]
	[color=#0000ff]End[/color] [color=#0000ff]Sub[/color]
	[color=#0000ff]Private[/color] [color=#0000ff]Sub[/color] ShutDown()
		[color=#0000ff]If[/color] MessageBox.Show("Would you like to shut down the system", "", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) = DialogResult.OK [color=#0000ff]Then[/color]
			[color=#0000ff]Dim[/color] platform [color=#0000ff]As[/color] [color=#0000ff]New[/color] PlatformID
			[color=#0000ff]Select[/color] [color=#0000ff]Case[/color] Environment.OSVersion.Platform
				[color=#0000ff]Case[/color] PlatformID.Win32NT
					[color=#0000ff]Dim[/color] token [color=#0000ff]As[/color] TOKEN_PRIVILEGES
					[color=#0000ff]Dim[/color] blank_token [color=#0000ff]As[/color] TOKEN_PRIVILEGES
					[color=#0000ff]Dim[/color] token_handle [color=#0000ff]As[/color] IntPtr
					[color=#0000ff]Dim[/color] uid [color=#0000ff]As[/color] LUID
					[color=#0000ff]Dim[/color] ret_length [color=#0000ff]As[/color] [color=#0000ff]Integer[/color]
					[color=#0000ff]Dim[/color] ptr [color=#0000ff]As[/color] IntPtr = GetCurrentProcess() [color=#008000]'/// get the process handle[/color]
 
					OpenProcessToken(ptr, &H20 [color=#0000ff]Or[/color] &H8, token_handle)
					LookupPrivilegeValue("", "SeShutdownPrivilege", uid)
					token.PrivilegeCount = [color=#008000]1[/color]
					token.Privileges = uid
					token.Attributes = &H2
 
					AdjustTokenPrivileges(token_handle, [color=#008000]0[/color], token, System.Runtime.InteropServices.Marshal.SizeOf(blank_token), blank_token, ret_length)
 
					ExitWindowsEx(EWX_SHUTDOWN [color=#0000ff]Or[/color] EWX_FORCE, &HFFFF)
 
				[color=#0000ff]Case[/color] [color=#0000ff]Else[/color]
					ExitWindowsEx(EWX_SHUTDOWN [color=#0000ff]Or[/color] EWX_FORCE, &HFFFF)
			[color=#0000ff]End[/color] [color=#0000ff]Select[/color]
		[color=#0000ff]End[/color] [color=#0000ff]If[/color]
	[color=#0000ff]End[/color] [color=#0000ff]Sub[/color][/font]
 
do i need priviledges

hey thanks for the code.. but since there is osme prob in my system and has gone for a repair .. i will only be able to check the code after couple of days .. but i want to ask something .. Will this require administrator priviledge ... i mean can i use this code if i my system is running in guest account ...
 
Back
Top