Public Const EWX_LOGOFF = 0 
Public Const EWX_SHUTDOWN = 1 
Public Const EWX_REBOOT = 2 
Public Const EWX_FORCE = 4 
Declare Function ExitWindowsEx Lib "user32" Alias _
"ExitWindowsEx" (ByVal uFlags As Long, ByVal dwReserved _
As Long) As Long 如果想关闭计算机,函数可以这样使用:
t& = ExitWindowsEx(EWX_FORCE OR EWX_SHUTDOWN, 0)

解决方案 »

  1.   

    Private Declare Function GetCurrentProcess Lib "kernel32" () As Long
    Private Declare Function LookupPrivilegeValue Lib "advapi32.dll" Alias "LookupPrivilegeValueA" (ByVal lpSystemName As String, ByVal lpName As String, lpLuid As LARGE_INTEGER) As Long
    Private Declare Function AdjustTokenPrivileges Lib "advapi32.dll" (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 Function GetComputerName Lib "kernel32" Alias "GetComputerNameA" (ByVal lpBuffer As String, nSize As Long) As Long
    Private Declare Function GetLastError Lib "kernel32" () As Long
    Public Function InitiateShutdownMachine(ByVal Machine As String, Optional Force As Variant, Optional Restart As Variant, Optional AllowLocalShutdown As Variant, Optional Delay As Variant, Optional Message As Variant) As Boolean
        Dim hProc As Long
        Dim OldTokenStuff As TOKEN_PRIVILEGES
        Dim OldTokenStuffLen As Long
        Dim NewTokenStuff As TOKEN_PRIVILEGES
        Dim NewTokenStuffLen As Long
        Dim pSize As Long
        If IsMissing(Force) Then Force = False
        If IsMissing(Restart) Then Restart = True
        If IsMissing(AllowLocalShutdown) Then AllowLocalShutdown = False
        If IsMissing(Delay) Then Delay = 0
        If IsMissing(Message) Then Message = ""
        'Make sure the Machine-name doesn't start with '\\'
        If InStr(Machine, "\\") = 1 Then
            Machine = Right(Machine, Len(Machine) - 2)
        End If
        'check if it's the local machine that's going to be shutdown
        If (LCase(GetMyMachineName) = LCase(Machine)) Then
            'may we shut this computer down?
            If AllowLocalShutdown = False Then Exit Function
            'open access token
            If OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES Or TOKEN_QUERY, hProc) = 0 Then
                MsgBox "OpenProcessToken Error: " & GetLastError()
                Exit Function
            End If
            'retrieve the locally unique identifier to represent the Shutdown-privilege name
            If LookupPrivilegeValue(vbNullString, SE_SHUTDOWN_NAME, OldTokenStuff.Privileges(0).pLuid) = 0 Then
                MsgBox "LookupPrivilegeValue Error: " & GetLastError()
                Exit Function
            End If
            NewTokenStuff = OldTokenStuff
            NewTokenStuff.PrivilegeCount = 1
            NewTokenStuff.Privileges(0).Attributes = SE_PRIVILEGE_ENABLED
            NewTokenStuffLen = Len(NewTokenStuff)
            pSize = Len(NewTokenStuff)
            'Enable shutdown-privilege
            If AdjustTokenPrivileges(hProc, False, NewTokenStuff, NewTokenStuffLen, OldTokenStuff, OldTokenStuffLen) = 0 Then
                MsgBox "AdjustTokenPrivileges Error: " & GetLastError()
                Exit Function
            End If
            'initiate the system shutdown
            If InitiateSystemShutdown("\\" & Machine, Message, Delay, Force, Restart) = 0 Then
                Exit Function
            End If
            NewTokenStuff.Privileges(0).Attributes = 0
            'Disable shutdown-privilege
            If AdjustTokenPrivileges(hProc, False, NewTokenStuff, Len(NewTokenStuff), OldTokenStuff, Len(OldTokenStuff)) = 0 Then
                Exit Function
            End If
        Else
            'initiate the system shutdown
            If InitiateSystemShutdown("\\" & Machine, Message, Delay, Force, Restart) = 0 Then
                Exit Function
            End If
        End If
        InitiateShutdownMachine = True
    End Function
    Function GetMyMachineName() As String
        Dim sLen As Long
        GetMyMachineName = Space(100)
        sLen = 100
       If GetComputerName(GetMyMachineName, sLen) Then
            GetMyMachineName = Left(GetMyMachineName, sLen)
        End If
    End Function
    Private Sub Form_Load()
    InitiateShutdownMachine GetMyMachineName, True, True, True, 60, "关机"
    End Sub
      

  2.   

    看看这个个Step By Step Example1.Create a new standard EXE in Visual Basic. Form1 is created by default. 2.View the code for Form1. In the Declarations section, add the following code:      Private Type LUID
             UsedPart As Long
             IgnoredForNowHigh32BitPart As Long
          End Type      Private Type TOKEN_PRIVILEGES
            PrivilegeCount As Long
            TheLuid As LUID
            Attributes As Long
          End Type      Private Const EWX_SHUTDOWN As Long = 1
          Private Const EWX_FORCE As Long = 4
          Private Const EWX_REBOOT = 2      Private Declare Function ExitWindowsEx Lib "user32" (ByVal _
               dwOptions As Long, ByVal dwReserved As Long) As Long      Private Declare Function GetCurrentProcess Lib "kernel32" () As Long
          Private Declare Function OpenProcessToken Lib "advapi32" (ByVal _
             ProcessHandle As Long, _
             ByVal DesiredAccess As Long, TokenHandle As Long) As Long
          Private Declare Function LookupPrivilegeValue Lib "advapi32" _
             Alias "LookupPrivilegeValueA" _
             (ByVal lpSystemName As String, ByVal lpName As String, lpLuid _
             As LUID) As Long
          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
     
    3. Add a procedure called AdjustToken with the following code:
          Private Sub AdjustToken()
             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         hdlProcessHandle = GetCurrentProcess()
             OpenProcessToken hdlProcessHandle, (TOKEN_ADJUST_PRIVILEGES Or _
                TOKEN_QUERY), hdlTokenHandle      ' Get the LUID for shutdown privilege.
             LookupPrivilegeValue "", "SeShutdownPrivilege", tmpLuid         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     End Sub
     
    4.Add a CommandButton to the form. In the Click event, add the following code:      Private Sub Command1_Click()
             AdjustToken
             ExitWindowsEx (EWX_SHUTDOWN Or EWX_FORCE Or EWX_REBOOT), &HFFFF
          End Sub
      

  3.   

    to:山人
    不行阿,我修改后的代码
    Const EWX_LOGOFF = 0
     Const EWX_SHUTDOWN = 1
    Const EWX_REBOOT = 2
    Const EWX_FORCE = 4
    Private Declare Function ExitWindowsEx Lib "user32" (ByVal uFlags As Long, ByVal dwReserved As Long) As LongPrivate Sub Form_Load()
    '如果想关闭计算机,函数可以这样使用:
    t& = ExitWindowsEx(EWX_FORCE Or EWX_SHUTDOWN, 0)
    End Sub我得系统是2000Profession
      

  4.   

    Shell "rundll.exe user.exe,exitwindows", vbHide
      

  5.   

    to:海军少校在2000Profession里是找不到那个程序地