none
how to shut down the computer RRS feed

  • Question

  • I am using this code to shut down my pc

      Public Shared Sub ShutDown(ByVal Wait_in_Seconds As Short)
            System.Diagnostics.Process.Start("shutdown.exe", "-s -t " & Wait_in_Seconds)
        End Sub

    but it is not working,nothing happens, so what is the problem and any solution for it?

    Sunday, May 19, 2013 11:14 AM

Answers

  • Hi, here is my simple solution:

        Public Sub Shutdown(ByVal time As Integer)
    
            Dim Command As String = "shutdown /s /t " & CStr(time) & " {ENTER}"
            Shell("c:\windows\system32\cmd.exe", AppWinStyle.NormalFocus)
            SendKeys.Send(Command)
    
        End Sub

    Best wishes

    You don`t really need to show the cmd window and use senkeys. You could do it with just the Shell command.  :)

    Shell("shutdown /s /t " & CStr(time), AppWinStyle.Hide) 'This will not show the cmd window when starting shutdown
    

    Sunday, May 19, 2013 1:03 PM

All replies

  • If you set a breakpoint in this code, is it hit?

    Mike

    Sunday, May 19, 2013 11:20 AM
  • Hi,

     I am not sure if it will make a difference or not but, i see that windows XP uses the (-s -t) and windows 7 and 8 use (/s /t) at this site so maybe if your using 7 or 8 it may make a difference.

    http://www.computerhope.com/shutdown.htm

    EDIT : I am not sure if you are using a Short for a reason or not but, maybe setting it as an Int32 and converting it to a string in your command args will help.

        Public Shared Sub ShutDown(ByVal Wait_in_Seconds As Int32)
            System.Diagnostics.Process.Start("shutdown.exe", "-s -t " & Wait_in_Seconds.ToString)
        End Sub
    

    • Edited by IronRazerz Sunday, May 19, 2013 11:33 AM
    Sunday, May 19, 2013 11:24 AM
  • Hi, here is my simple solution:

        Public Sub Shutdown(ByVal time As Integer)
    
            Dim Command As String = "shutdown /s /t " & CStr(time) & " {ENTER}"
            Shell("c:\windows\system32\cmd.exe", AppWinStyle.NormalFocus)
            SendKeys.Send(Command)
    
        End Sub

    Best wishes

    Sunday, May 19, 2013 12:47 PM
  • Hi, here is my simple solution:

        Public Sub Shutdown(ByVal time As Integer)
    
            Dim Command As String = "shutdown /s /t " & CStr(time) & " {ENTER}"
            Shell("c:\windows\system32\cmd.exe", AppWinStyle.NormalFocus)
            SendKeys.Send(Command)
    
        End Sub

    Best wishes

    You don`t really need to show the cmd window and use senkeys. You could do it with just the Shell command.  :)

    Shell("shutdown /s /t " & CStr(time), AppWinStyle.Hide) 'This will not show the cmd window when starting shutdown
    

    Sunday, May 19, 2013 1:03 PM

  • You don`t really need to show the cmd window and use senkeys. You could do it with just the Shell command.  :)

    Shell("shutdown /s /t " & CStr(time), AppWinStyle.Hide) 'This will not show the cmd window when starting shutdown

    mm, Yes I think this is more appropriate... and it works well :)

    But, How does it work? I mean I can't find any executable in the system named "shutdown" so what does the "Shell" really execute in this case?

    • Edited by BGQQ Sunday, May 19, 2013 6:34 PM
    Sunday, May 19, 2013 6:29 PM
  • I am not sure of the location of it on windows 7 or 8 but, on XP (shutdown.exe) is located in the Windows\system32 folder. Being that the system32 folder is included in the environment PATH it is found and executed just like if you executed "Notepad" from the shell command or a Process.  :)
    Sunday, May 19, 2013 6:46 PM
  • Imports System.Runtime.InteropServices
    
    Public Class Form1
    
        Private objExitWin As New cWrapExitWindows()
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Me.Text = "Exit Windows"
        End Sub
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            If TextBox1.Text = "LogOff" Then
                objExitWin.ExitWindows(cWrapExitWindows.Action.LogOff)
            ElseIf TextBox1.Text = "Restart" Then
                objExitWin.ExitWindows(cWrapExitWindows.Action.Restart)
            ElseIf TextBox1.Text = "Shutdown" Then
                objExitWin.ExitWindows(cWrapExitWindows.Action.Shutdown)
            ElseIf TextBox1.Text = "PowerOff" Then
                objExitWin.ExitWindows(cWrapExitWindows.Action.PowerOff)
            Else
                Exit Sub
            End If
        End Sub
    
    End Class
    
    Public Class cWrapExitWindows
    
        Private Declare Function ExitWindowsEx Lib "user32.dll" (ByVal uFlags As Int32, ByVal dwReserved As Int32) As Boolean
        Private Declare Function GetCurrentProcess Lib "kernel32.dll" () As IntPtr
        Private Declare Sub OpenProcessToken Lib "advapi32.dll" (ByVal ProcessHandle As IntPtr, ByVal DesiredAccess As Int32, ByRef TokenHandle As IntPtr)
        Private Declare Sub LookupPrivilegeValue Lib "advapi32.dll" Alias "LookupPrivilegeValueA" (ByVal lpSystemName As String, ByVal lpName As String, ByRef lpLuid As Long)
        Private Declare Function AdjustTokenPrivileges Lib "advapi32.dll" (ByVal TokenHandle As IntPtr, ByVal DisableAllPrivileges As Boolean, ByRef NewState As LUID, ByVal BufferLength As Int32, ByVal PreviousState As IntPtr, ByVal ReturnLength As IntPtr) As Boolean
    
        <StructLayout(LayoutKind.Sequential, Pack:=1)> _
        Friend Structure LUID
            Public Count As Integer
            Public LUID As Long
            Public Attribute As Integer
        End Structure
    
        Public Enum Action
            LogOff = 0
            Shutdown = 1
            Restart = 2
            PowerOff = 8
        End Enum
    
        Public Sub ExitWindows(ByVal how As Action, Optional ByVal Forced As Boolean = True)
            Dim TokenPrivilege As LUID
            Dim hProcess As IntPtr = GetCurrentProcess()
            Dim hToken As IntPtr = IntPtr.Zero
            OpenProcessToken(hProcess, &H28, hToken)
            TokenPrivilege.Count = 1
            TokenPrivilege.LUID = 0
            TokenPrivilege.Attribute = 2
            LookupPrivilegeValue(Nothing, "SeShutdownPrivilege", TokenPrivilege.LUID)
            AdjustTokenPrivileges(hToken, False, TokenPrivilege, 0, IntPtr.Zero, IntPtr.Zero)
            If Forced Then
                ExitWindowsEx(how + 4, 0)
            Else
                ExitWindowsEx(how, 0)
            End If
        End Sub
    
    End Class
    


    You've taught me everything I know but not everything you know.

    Monday, May 20, 2013 3:44 AM
  • This works here, Win7 x86

    Option Strict On
    'Options: /s and -s seem to be the same here on Windows 7 32 bit
    '/i or -i        Display the graphical user interface (GUI). This must be the first option.
    '/l or -l        Log off. This cannot be used with /m or /d options.
    '/s or -s        Shutdown the computer.
    '/r or -r        Shutdown and restart the computer.
    '/p or -p        Turn off the local computer with no time-out or warning.
    '/h or -h        Hibernate the local computer.
    'shutdown.exe exists since XP according to Microsoft 
    
    Public Class Form1
        WithEvents GoBtn As New Button
        Private Sub GoBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles GoBtn.Click
            Dim sCommand As String = "shutdown"
            Dim sArgs As String = "-s -t 0"
            Dim psi As System.Diagnostics.ProcessStartInfo = New System.Diagnostics.ProcessStartInfo(sCommand, sArgs)
            psi.UseShellExecute = False
            psi.CreateNoWindow = True
            Dim proc As System.Diagnostics.Process = System.Diagnostics.Process.Start(psi)
        End Sub
        Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
            Me.Size = New Size(250, 100)
            With GoBtn
                .Size = New Size(100, 25)
                .Location = New Point(10, 10)
                .Text = "Shutdown"
            End With
            Me.Controls.Add(GoBtn)
        End Sub
    End Class
    

    Monday, May 20, 2013 3:47 AM
  • Why don't you use different buttons in place of a text box where user enters what operation he wants to do?

    Below is a simple form with 4 buttons doing operations - Shut Down, Restart, Log off, Exit:

    Public Class frmShutdown Private Sub btnShutdown_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnShutdown.Click System.Diagnostics.Process.Start("shutdown", "-s -t 00") End Sub Private Sub btnRestart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRestart.Click System.Diagnostics.Process.Start("shutdown", "-r -t 00") End Sub Private Sub btnLogOff_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLogOff.Click System.Diagnostics.Process.Start("shutdown", "-l -t 00") End Sub Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click End

    'This will make the program to terminate(end the program) End Sub End Class

    Isn't that simpler?


    Mark as answer if this solves your problem. ... Best Regards D. Chatterjee MCA, RERF

    Monday, May 20, 2013 9:38 AM
  • So is that working Titanium man11?

    Mark as answer if this solves your problem. ... Best Regards D. Chatterjee MCA, RERF

    Monday, May 20, 2013 2:06 PM