我的代码如下:
Private Declare Function ExitWindowsEx Lib "user32" (ByVal uFlags As Long, ByVal dwReserved As Long) As LongPrivate Sub Command1_Click()
Dim popo As Long
If Option1.Value Then
  End
End If
If Option2.Value Then
  popo = ExitWindowsEx(4 Or 2, 0)'重启计算机
End If
If Option3.Value Then
  popo = ExitWindowsEx(1, 0)'关闭计算机
End If
End Sub
运行程序,按钮无效,无法重启或关闭计算机
操作系统为winXP

解决方案 »

  1.   

    用AdjustTokenPrivileges
    获得SE_SHUTDOWN_NAME 
    权限才行
      

  2.   

    '----------------  通过 VB 关机、重启  ------------------------
    '                                         Yechat 2006-6-8
    '--------------------------------------------------------------
    '       根据MSDN:Displaying the Shutdown Dialog Box 的C程序改写
    '每个进程,除了优先权,还有特权。
    '特权是指:一个进程可以做哪些事,哪些事不能做,比如,一般的进程不允许KILL关键进程;不允许关机重启;不允许中止关键服务等。
    '          如果想干啥事儿,就得取得相应的特权。Option Explicit
    Public Declare Function GetLastError Lib "kernel32" () As Long
    Public Declare Function InitiateSystemShutdown Lib "advapi32.dll" Alias "InitiateSystemShutdownA" (ByVal lpMachineName As String, ByVal lpMessage As String, ByVal dwTimeout As Long, ByVal bForceAppsClosed As Long, ByVal bRebootAfterShutdown As Long) As Long
    Public Declare Function GetCurrentProcess Lib "kernel32" () As Long
    Public Declare Function OpenProcessToken Lib "advapi32.dll" (ByVal ProcessHandle As Long, ByVal DesiredAccess As Long, TokenHandle As Long) As Long
    Public Declare Function LookupPrivilegeValue Lib "advapi32.dll" Alias "LookupPrivilegeValueA" (ByVal lpSystemName As String, ByVal lpName As String, lpLuid As LUID) As Long
    Public 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
    Public Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
    Public Const ERROR_SUCCESS = 0&
    Public Const SE_PRIVILEGE_ENABLED = &H2
    Public Const SE_SHUTDOWN_NAME = "SeShutdownPrivilege"
    Public Const ANYSIZE_ARRAY = 1'需修改
    Public Const TOKEN_ADJUST_PRIVILEGES = &H20
    Public Const TOKEN_QUERY = &H8
    Public Type LUID
      lowpart As Long
      highpart As Long
    End TypePublic Type LUID_AND_ATTRIBUTES
            pLuid As LUID
            Attributes As Long
    End TypePublic Type TOKEN_PRIVILEGES
      PrivilegeCount As Long
      Privileges(ANYSIZE_ARRAY) As LUID_AND_ATTRIBUTES
    End Type
    Sub Main()  Dim hToken&                         ' handle to process token
      Dim tkp As TOKEN_PRIVILEGES         ' pointer to token structure
      
      Dim fResult As Boolean              ' system shutdown flag
      
      Dim Prv As TOKEN_PRIVILEGES
     
      Sleep 5000
      
      'If MsgBox("你真要关机?", vbQuestion + vbYesNo + vbDefaultButton2) = vbNo Then Exit Sub
      
      ' Get the current process token handle so we can get shutdown
      ' privilege.
       
      If Not CBool(OpenProcessToken(GetCurrentProcess, (TOKEN_ADJUST_PRIVILEGES Or TOKEN_QUERY), hToken&)) Then MsgBox ("OpenProcessToken failed.")
       
      ' Get the LUID for shutdown privilege.
       
      Call LookupPrivilegeValue(vbNullString, SE_SHUTDOWN_NAME, tkp.Privileges(0).pLuid)
       
      tkp.PrivilegeCount = 1  ' one privilege to set
      tkp.Privileges(0).Attributes = SE_PRIVILEGE_ENABLED
       
      ' Get shutdown privilege for this process.ad
       
      Call AdjustTokenPrivileges(hToken&, False, tkp, Len(Prv), Prv, 0)
       
      ' Cannot test the return value of AdjustTokenPrivileges.
      If (GetLastError() <> ERROR_SUCCESS) Then MsgBox ("AdjustTokenPrivileges enable failed.")
       
      ' Display the shutdown dialog box and start the time-out countdown.
       
      fResult = InitiateSystemShutdown(vbNullString, "此关机是由 " & App.Title & " 发起的.", 20, False, True)
       
      If Not fResult Then MsgBox ("InitiateSystemShutdown failed.")
       
      ' Disable shutdown privilege.
       
      tkp.Privileges(0).Attributes = 0
      Call AdjustTokenPrivileges(hToken&, False, tkp, Len(Prv), Prv, 0)
       
      If (GetLastError() <> ERROR_SUCCESS) Then MsgBox ("AdjustTokenPrivileges disable failed.")
     
    End Sub