kanak Posted January 9, 2005 Posted January 9, 2005 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 Quote
coldfusion244 Posted January 9, 2005 Posted January 9, 2005 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 Use ExitWindowsEx API... http://msdn.microsoft.com/library/default.asp?url=/library/en-us/sysinfo/base/exitwindowsex.asp Quote -Sean
rot13 Posted January 29, 2005 Posted January 29, 2005 is there any way to do this without an API? if you can do everything without API's, then your program will be able to work on linux when they get the .net framework on there, as i understand it... Quote
Leaders dynamic_sysop Posted January 29, 2005 Leaders Posted January 29, 2005 The .NET Framework uses Api's within it's 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 ) api's 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 ... 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 Quote
TheWizardofInt Posted June 10, 2005 Posted June 10, 2005 Or just do this: 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 Quote Read the Fovean Chronicles Because you just can't spend your whole day programming!
bri189a Posted June 10, 2005 Posted June 10, 2005 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. Quote
TheWizardofInt Posted June 10, 2005 Posted June 10, 2005 I didn't turn WMI on on mine and it works It is also running on my Bedtime Monitor program, and it is turning PC's off on kids at bedtime all over the world Is WMI something you have to go shut off? Quote Read the Fovean Chronicles Because you just can't spend your whole day programming!
bri189a Posted June 10, 2005 Posted June 10, 2005 Yes, it's on by default in 2000 and XP, it wasn't 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 that's way beyond any at home users, you shouldn't need to worry really. Quote
jmcilhinney Posted June 17, 2005 Posted June 17, 2005 Don't know if anyone's 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 doesn't 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. Quote
Leaders snarfblam Posted June 17, 2005 Leaders Posted June 17, 2005 Don't know if anyone's 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 doesn't 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' date=' 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.[/quote'] 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. Quote [sIGPIC]e[/sIGPIC]
HJB417 Posted June 20, 2005 Posted June 20, 2005 Use ExitWindowsEx API... http://msdn.microsoft.com/library/default.asp?url=/library/en-us/sysinfo/base/exitwindowsex.asp excellent find! ;) Quote
Recommended Posts