Results 1 to 8 of 8

Thread: How to shut down...

  1. #1

    Thread Starter
    New Member
    Join Date
    Oct 2008
    Posts
    1

    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.

  2. #2
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: How to shut down...

    Moved to VB.NET

  3. #3
    Hyperactive Member .NetNinja's Avatar
    Join Date
    Oct 2008
    Location
    USA
    Posts
    281

    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

  4. #4
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    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)
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  5. #5
    Hyperactive Member .NetNinja's Avatar
    Join Date
    Oct 2008
    Location
    USA
    Posts
    281

    Re: How to shut down...

    Yeah - you could do that too.

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: How to shut down...

    Quote 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:
    1. Process.Start("shutdown", "-s -t 5")
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: How to shut down...

    noted - it was a modify / test / copy / paste of something else.
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width