Results 1 to 13 of 13

Thread: [resolved]forcing a computer to shutdown

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Apr 2002
    Location
    Blue Mountains NSW Australia
    Posts
    160

    [resolved]forcing a computer to shutdown

    i have a mate writing a program which controls 4 fans within his computer case he needs and option where the computer shutdown if temperatures exceed a certain level.. any suggestions?
    Last edited by Jack Daniels; Jul 31st, 2002 at 06:03 AM.
    Jack Daniels
    ICQ: 72074030
    VB 6.0

  2. #2
    Addicted Member
    Join Date
    Jul 2002
    Location
    UK
    Posts
    147
    Try this:
    Code:
    Option Explicit
    Private Const EWX_SHUTDOWN As Long = 1
    Private Const EWX_REBOOT As Long = 2
    Private Const EWX_FORCE As Long = 4
    Private Const EWX_POWEROFF As Long = 8
    'The ExitWindowsEx function either logs off, shuts down, or shuts
    'down and restarts the system.
    
    Private Declare Function ExitWindowsEx Lib "user32" _
        (ByVal dwOptions As Long, _
        ByVal dwReserved As Long) As Long
    'The GetLastError function returns the calling thread's last-error
    'code value. The last-error code is maintained on a per-thread basis.
    'Multiple threads do not overwrite each other's last-error code.
    Private Declare Function GetLastError Lib "kernel32" () As Long
    Private Const mlngWindows95 = 0
    Private Const mlngWindowsNT = 1
    Public glngWhichWindows32 As Long
    'The GetVersion function returns the operating system in use.
    Private Declare Function GetVersion Lib "kernel32" () As Long
    Private Type LUID
        UsedPart As Long
        IgnoredForNowHigh32BitPart As Long
    End Type
    Private Type LUID_AND_ATTRIBUTES
        TheLuid As LUID
        Attributes As Long
    End Type
    Private Type TOKEN_PRIVILEGES
        PrivilegeCount As Long
        TheLuid As LUID
        Attributes As Long
    End Type
    'The GetCurrentProcess function returns a pseudohandle for the
    'current process.
    Private Declare Function GetCurrentProcess Lib "kernel32" () As Long
    'The OpenProcessToken function opens the access token associated with
    'a process.
    Private Declare Function OpenProcessToken Lib "advapi32" _
        (ByVal ProcessHandle As Long, _
        ByVal DesiredAccess As Long, _
        TokenHandle As Long) As Long
    'The LookupPrivilegeValue function retrieves the locally unique
    'identifier (LUID) used on a specified system to locally represent
    'the specified privilege name.
    Private Declare Function LookupPrivilegeValue Lib "advapi32" _
        Alias "LookupPrivilegeValueA" _
        (ByVal lpSystemName As String, _
        ByVal lpName As String, _
        lpLuid As LUID) As Long
    'The AdjustTokenPrivileges function enables or disables privileges
    'in the specified access token. Enabling or disabling privileges
    'in an access token requires TOKEN_ADJUST_PRIVILEGES access.
    Private Declare Function AdjustTokenPrivileges Lib "advapi32" _
        (ByVal TokenHandle As Long, _
        ByVal DisableAllPrivileges As Long, _
        NewState As TOKEN_PRIVILEGES, _
        ByVal BufferLength As Long, _
        PreviousState As TOKEN_PRIVILEGES, _
        ReturnLength As Long) As Long
    Private Declare Sub SetLastError Lib "kernel32" (ByVal dwErrCode As Long)
    
    Private Sub AdjustToken()
    '********************************************************************
    '* This procedure sets the proper privileges to allow a log off or a
    '* shut down to occur under Windows NT.
    '********************************************************************
    Const TOKEN_ADJUST_PRIVILEGES = &H20
    Const TOKEN_QUERY = &H8
    Const SE_PRIVILEGE_ENABLED = &H2
    Dim hdlProcessHandle As Long
    Dim hdlTokenHandle As Long
    Dim tmpLuid As LUID
    Dim tkp As TOKEN_PRIVILEGES
    Dim tkpNewButIgnored As TOKEN_PRIVILEGES
    Dim lBufferNeeded As Long
    'Set the error code of the last thread to zero using the
    'SetLast Error function. Do this so that the GetLastError
    'function does not return a value other than zero for no
    'apparent reason.
        SetLastError 0
    'Use the GetCurrentProcess function to set the hdlProcessHandle
    'variable.
        hdlProcessHandle = GetCurrentProcess()
    '    If GetLastError <> 0 Then
    '        systemMessage.AddItem "GetCurrentProcess Error==" & GetLastError
    '    End If
        OpenProcessToken hdlProcessHandle, _
            (TOKEN_ADJUST_PRIVILEGES Or TOKEN_QUERY), hdlTokenHandle
    '    If GetLastError <> 0 Then
    '        systemMessage.AddItem "OpenProcessToken Error==" & GetLastError
    '    End If
    'Get the LUID for shutdown privilege
        LookupPrivilegeValue "", "SeShutdownPrivilege", tmpLuid
    '    If GetLastError <> 0 Then
    '        systemMessage.AddItem "LookupPrivilegeValue Error==" & GetLastError
    '    End If
        tkp.PrivilegeCount = 1    ' One privilege to set
        tkp.TheLuid = tmpLuid
        tkp.Attributes = SE_PRIVILEGE_ENABLED
    'Enable the shutdown privilege in the access token of this process
        AdjustTokenPrivileges hdlTokenHandle, _
        False, _
        tkp, _
        Len(tkpNewButIgnored), _
        tkpNewButIgnored, _
        lBufferNeeded
    '    If GetLastError <> 0 Then
    '        systemMessage.AddItem "AdjustTokenPrivileges Error==" & GetLastError
    '    End If
    End Sub
    
    Private Sub cmdShutdown_Click()
        If glngWhichWindows32 = mlngWindowsNT Then
            AdjustToken
    '        systemMessage.AddItem "Post-AdjustToken GetLastError " & GetLastError
        End If
        ExitWindowsEx (EWX_SHUTDOWN), &HFFFF
    '    systemMessage.AddItem "ExitWindowsEx's GetLastError " & GetLastError
    End Sub
    
    Private Sub cmdForceShutdown_Click()
        If glngWhichWindows32 = mlngWindowsNT Then
            AdjustToken
    '        systemMessage.AddItem "Post-AdjustToken GetLastError " & GetLastError
        End If
        ExitWindowsEx (EWX_SHUTDOWN Or EWX_FORCE), &HFFFF
    '    systemMessage.AddItem "ExitWindowsEx's GetLastError " & GetLastError
    End Sub
    
    Public Sub Main()
    '********************************************************************
    '* When the project starts, check the operating system used by
    '* calling the GetVersion function.
    '********************************************************************
    Dim lngVersion As Long
        lngVersion = GetVersion()
        If ((lngVersion And &H80000000) = 0) Then
            glngWhichWindows32 = mlngWindowsNT
    '        systemType.Text = "Windows NT"
        Else
            glngWhichWindows32 = mlngWindows95
    '        systemType.Text = "Windows 95"
        End If
    Call cmdForceShutdown_Click
    End
    End Sub

  3. #3
    -= B u g S l a y e r =- peet's Avatar
    Join Date
    Aug 2000
    Posts
    9,629
    -= a peet post =-

  4. #4
    Addicted Member reznor's Avatar
    Join Date
    May 2001
    Location
    Netherlands
    Posts
    151
    Use the ExitWindowsEx API. Explanation on this site:
    http://216.26.168.92/vbapi/ref/e/exitwindowsex.html
    "Computers are incredibly fast, accurate and stupid. Human beings are incredibly slow, inaccurate and brilliant. Together they are powerful beyond imagination." - Albert Einstein

  5. #5
    New Member
    Join Date
    Jul 2002
    Location
    Bucharest, Romania
    Posts
    1

    Wink forcing a computer to shutdown

    There are some simple APIs that can do that for you. All you have to do is call one of them. For example:




    public sub blah blah....()etc.
    Call ExitWindowsEx(EWX_SHUTDOWN, 0)
    end sub




    Hope it helps
    Bogdan

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Apr 2002
    Location
    Blue Mountains NSW Australia
    Posts
    160

    wow :O

    thanks heaps fellas :-D
    Jack Daniels
    ICQ: 72074030
    VB 6.0

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Apr 2002
    Location
    Blue Mountains NSW Australia
    Posts
    160

    Unhappy sorry

    i know i edited it to resolved, and it works but on winxp, it shutdown to logon menu thingy only? this doesn't matter muh but if someone knows how to shut down the computer completely with XP im open to suggestions thanks for all the help.
    Jack Daniels
    ICQ: 72074030
    VB 6.0

  8. #8
    -= B u g S l a y e r =- peet's Avatar
    Join Date
    Aug 2000
    Posts
    9,629
    you used the EWX_FORCE ?
    -= a peet post =-

  9. #9

    Thread Starter
    Addicted Member
    Join Date
    Apr 2002
    Location
    Blue Mountains NSW Australia
    Posts
    160
    yeh i tried that and the value 4 which is the same thing isn't it??

    i think it's got something to do with XP cause it worked on 98...
    Jack Daniels
    ICQ: 72074030
    VB 6.0

  10. #10
    -= B u g S l a y e r =- peet's Avatar
    Join Date
    Aug 2000
    Posts
    9,629
    mmm....

    what happens if you add

    Private Const EWX_POWEROFF As Long = 8

    and uses that together with EWX_SHUTDOWN and EWX_FORCE

    like this

    EWX_POWEROFF Or EWX_SHUTDOWN Or EWX_FORCE
    -= a peet post =-

  11. #11

    Thread Starter
    Addicted Member
    Join Date
    Apr 2002
    Location
    Blue Mountains NSW Australia
    Posts
    160
    hmmm no go :-( um there was a varible reserved for future versions of windows, which XP is, do you think thats been employed or something?
    Jack Daniels
    ICQ: 72074030
    VB 6.0

  12. #12
    -= B u g S l a y e r =- peet's Avatar
    Join Date
    Aug 2000
    Posts
    9,629
    hmmm don't know... never used this meself

    found this in the MSDN... might be worth a reading

    MSDN

    PRB: ExitWindowsEx API Does Not Reboot Windows NT

    Q176695


    --------------------------------------------------------------------------------
    The information in this article applies to:

    Microsoft Visual Basic Learning, Professional, and Enterprise Editions for Windows, versions 5.0, 6.0
    Microsoft Visual Basic Standard, Professional, and Enterprise Editions, 32-bit only, for Windows, version 4.0

    --------------------------------------------------------------------------------


    SYMPTOMS
    When you use the ExitWindowsEx API to reboot the system under Windows NT and Windows 2000, the machine does not reboot.



    CAUSE
    In order to programmatically reboot a Windows NT or Windows 2000 system, the process requires the SE_SHUTDOWN_NAME privilege. By default, Visual Basic applications do not have this privilege and therefore will not reboot the machine.



    RESOLUTION
    In order to get ExitWindowsEx API to reboot the system under Windows NT or Windows 2000, the SE_SHUTDOWN_NAME privilege must be set. The following steps describe how to get the ExitWindowsEx API to work under Windows NT and Windows 2000.


    Step By Step Example
    Create a new standard EXE in Visual Basic. Form1 is created by default.


    View the code for Form1. In the Declarations section, add the following code:


    VB Code:
    1. Private Type LUID
    2.          UsedPart As Long
    3.          IgnoredForNowHigh32BitPart As Long
    4.       End Type
    5.  
    6.       Private Type TOKEN_PRIVILEGES
    7.         PrivilegeCount As Long
    8.         TheLuid As LUID
    9.         Attributes As Long
    10.       End Type
    11.  
    12.       Private Const EWX_SHUTDOWN As Long = 1
    13.       Private Const EWX_FORCE As Long = 4
    14.       Private Const EWX_REBOOT = 2
    15.  
    16.       Private Declare Function ExitWindowsEx Lib "user32" (ByVal _
    17.            dwOptions As Long, ByVal dwReserved As Long) As Long
    18.  
    19.       Private Declare Function GetCurrentProcess Lib "kernel32" () As Long
    20.       Private Declare Function OpenProcessToken Lib "advapi32" (ByVal _
    21.          ProcessHandle As Long, _
    22.          ByVal DesiredAccess As Long, TokenHandle As Long) As Long
    23.       Private Declare Function LookupPrivilegeValue Lib "advapi32" _
    24.          Alias "LookupPrivilegeValueA" _
    25.          (ByVal lpSystemName As String, ByVal lpName As String, lpLuid _
    26.          As LUID) As Long
    27.       Private Declare Function AdjustTokenPrivileges Lib "advapi32" _
    28.          (ByVal TokenHandle As Long, _
    29.          ByVal DisableAllPrivileges As Long, NewState As TOKEN_PRIVILEGES _
    30.          , ByVal BufferLength As Long, _
    31.       PreviousState As TOKEN_PRIVILEGES, ReturnLength As Long) As Long
    32.  
    33. 'Add a procedure called AdjustToken with the following code:
    34.  
    35.  
    36.  
    37.       Private Sub AdjustToken()
    38.          Const TOKEN_ADJUST_PRIVILEGES = &H20
    39.          Const TOKEN_QUERY = &H8
    40.          Const SE_PRIVILEGE_ENABLED = &H2
    41.          Dim hdlProcessHandle As Long
    42.          Dim hdlTokenHandle As Long
    43.          Dim tmpLuid As LUID
    44.          Dim tkp As TOKEN_PRIVILEGES
    45.          Dim tkpNewButIgnored As TOKEN_PRIVILEGES
    46.          Dim lBufferNeeded As Long
    47.  
    48.          hdlProcessHandle = GetCurrentProcess()
    49.          OpenProcessToken hdlProcessHandle, (TOKEN_ADJUST_PRIVILEGES Or _
    50.             TOKEN_QUERY), hdlTokenHandle
    51.  
    52.       ' Get the LUID for shutdown privilege.
    53.          LookupPrivilegeValue "", "SeShutdownPrivilege", tmpLuid
    54.  
    55.          tkp.PrivilegeCount = 1    ' One privilege to set
    56.          tkp.TheLuid = tmpLuid
    57.          tkp.Attributes = SE_PRIVILEGE_ENABLED
    58.  
    59.      ' Enable the shutdown privilege in the access token of this process.
    60.          AdjustTokenPrivileges hdlTokenHandle, False, _
    61.          tkp, Len(tkpNewButIgnored), tkpNewButIgnored, lBufferNeeded
    62.  
    63.      End Sub
    64.  
    65. 'Add a CommandButton to the form. In the Click event, add the following code:
    66.  
    67.  
    68.  
    69.       Private Sub Command1_Click()
    70.          AdjustToken
    71.          ExitWindowsEx (EWX_SHUTDOWN Or EWX_FORCE Or EWX_REBOOT), &HFFFF
    72.       End Sub
    Save the project and build an executable. When you run the executable and click the CommandButton the computer will reboot as expected.
    -= a peet post =-

  13. #13

    Thread Starter
    Addicted Member
    Join Date
    Apr 2002
    Location
    Blue Mountains NSW Australia
    Posts
    160
    ah thankyou muchly :-) um i think that was included in what the first guys reply said but it makes so much more sense now.

    very annoying function to debug ! esp. when your chatting with 10 ppl or so.
    Jack Daniels
    ICQ: 72074030
    VB 6.0

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