关于重启计算机的问题

luzhg008 2003-10-14 10:48:55
我用API函数新编了一段用于重新启动计算机程序的代码,可是运行时却丝毫不起反应,这是怎么回事?我把代码提供给大家,让诸位指点一下:
Private Declare Function ExitWindowsEx Lib "user32" (ByVal uFlags As Long, ByVal dwReserved As Long)As Long

Private Sub Command1_Click()
ExitWindowsEx &H43,0
End Sub

command1的Caption属性为“重新启动“
...全文
26 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
kmzs 2004-03-12
  • 打赏
  • 举报
回复
en
luzhg008 2004-03-12
  • 打赏
  • 举报
回复
谢谢
aha99 2003-10-14
  • 打赏
  • 举报
回复
'Showdown module
'ヨリニeboot False
'ケリサⅩhutdown False
'ヌソヨニヨリニeboot True
'ヌソヨニケリサⅩhutdown True
'ヌソヨニイサフ睫セア」エ・Shutdown false

Option Explicit

Declare Function ExitWindowsEx Lib "user32" (ByVal uFlags As Long, ByVal dwReserved As Long) As Long

Private Const EWX_POWEROFF As Long = 8&
Private Const EWX_FORCE As Long = 4&
Private Const EWX_REBOOT As Long = 2&
Private Const EWX_LOGOFF As Long = 0&
Private Const EWX_SHUTDOWN As Long = 1&

Private Const ERROR_SUCCESS As Long = 0&
Private Const ERROR_NOT_ALL_ASSIGNED As Long = 1300&

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 LookupPrivilegeValue Lib "advapi32.dll" Alias "LookupPrivilegeValueA" (ByVal lpSystemName As Any, ByVal lpName As String, lpLuid As LUID) As Long
Private Declare Function OpenProcessToken Lib "advapi32.dll" (ByVal ProcessHandle As Long, ByVal DesiredAccess As Long, TokenHandle As Long) As Long
Private Declare Function GetCurrentProcess Lib "kernel32" () As Long

Private Const TOKEN_QUERY As Long = &H8&
Private Const TOKEN_ADJUST_PRIVILEGES As Long = &H20&
Private Const SE_PRIVILEGE_ENABLED As Long = &H2

Private Type LUID
lowpart As Long
highpart As Long
End Type

Private Type LUID_AND_ATTRIBUTES
pLuid As LUID
Attributes As Long
End Type

Private Type TOKEN_PRIVILEGES
PrivilegeCount As Long
Privileges As LUID_AND_ATTRIBUTES
End Type

Public Sub LogOff()
Dim p_lngRtn As Long
Dim p_lngFlags As Long

p_lngFlags = EWX_LOGOFF
p_lngRtn = ExitWindowsEx(p_lngFlags, 0&)

End Sub

Public Sub Reboot(ByVal xi_blnForce As Boolean)
Dim p_lngRtn As Long
Dim p_lngFlags As Long
Dim p_lngToken As Long
Dim p_lngBufLen As Long
Dim p_lngLastErr As Long
Dim p_typLUID As LUID
Dim p_typTokenPriv As TOKEN_PRIVILEGES
Dim p_typPrevTokenPriv As TOKEN_PRIVILEGES

p_lngRtn = OpenProcessToken(GetCurrentProcess(), _
TOKEN_ADJUST_PRIVILEGES Or TOKEN_QUERY, _
p_lngToken)
If p_lngRtn = 0 Then
' Failed
Debug.Print ReturnApiErrString(Err.LastDllError)
Exit Sub
End If

p_lngRtn = LookupPrivilegeValue(0&, "SeShutdownPrivilege", p_typLUID)
If p_lngRtn = 0 Then
' Failed
Debug.Print ReturnApiErrString(Err.LastDllError)
Exit Sub
End If

p_typTokenPriv.PrivilegeCount = 1
p_typTokenPriv.Privileges.Attributes = SE_PRIVILEGE_ENABLED
p_typTokenPriv.Privileges.pLuid = p_typLUID

p_lngRtn = AdjustTokenPrivileges(p_lngToken, False, _
p_typTokenPriv, Len(p_typPrevTokenPriv), _
p_typPrevTokenPriv, p_lngBufLen)
If p_lngRtn = 0 Then
' Failed
Debug.Print Err.LastDllError, ReturnApiErrString(Err.LastDllError)
Exit Sub
Else
p_lngLastErr = Err.LastDllError
If p_lngLastErr = ERROR_SUCCESS Then
' Everything is OK
ElseIf p_lngLastErr = ERROR_NOT_ALL_ASSIGNED Then
Debug.Print "Not all privileges assigned."
Else
Debug.Print p_lngLastErr, ReturnApiErrString(p_lngLastErr)
End If
End If

If xi_blnForce = False Then
p_lngFlags = EWX_REBOOT
Else
p_lngFlags = EWX_REBOOT Or EWX_FORCE
End If

p_lngRtn = ExitWindowsEx(p_lngFlags, 0&)

End Sub

Public Sub Shutdown(ByVal xi_blnForce As Boolean)
Dim p_lngRtn As Long
Dim p_lngFlags As Long
Dim p_lngToken As Long
Dim p_lngBufLen As Long
Dim p_lngLastErr As Long
Dim p_typLUID As LUID
Dim p_typTokenPriv As TOKEN_PRIVILEGES
Dim p_typPrevTokenPriv As TOKEN_PRIVILEGES

p_lngRtn = OpenProcessToken(GetCurrentProcess(), _
TOKEN_ADJUST_PRIVILEGES Or TOKEN_QUERY, _
p_lngToken)
If p_lngRtn = 0 Then
' Failed
Debug.Print ReturnApiErrString(Err.LastDllError)
Exit Sub
End If

p_lngRtn = LookupPrivilegeValue(0&, "SeShutdownPrivilege", p_typLUID)
If p_lngRtn = 0 Then
' Failed
Debug.Print ReturnApiErrString(Err.LastDllError)
Exit Sub
End If

p_typTokenPriv.PrivilegeCount = 1
p_typTokenPriv.Privileges.Attributes = SE_PRIVILEGE_ENABLED
p_typTokenPriv.Privileges.pLuid = p_typLUID

p_lngRtn = AdjustTokenPrivileges(p_lngToken, False, _
p_typTokenPriv, Len(p_typPrevTokenPriv), _
p_typPrevTokenPriv, p_lngBufLen)
If p_lngRtn = 0 Then
' Failed
Debug.Print Err.LastDllError, ReturnApiErrString(Err.LastDllError)
Exit Sub
Else
p_lngLastErr = Err.LastDllError
If p_lngLastErr = ERROR_SUCCESS Then
' Everything is OK
ElseIf p_lngLastErr = ERROR_NOT_ALL_ASSIGNED Then
Debug.Print "Not all privileges assigned."
Else
Debug.Print p_lngLastErr, ReturnApiErrString(p_lngLastErr)
End If
End If

If xi_blnForce = False Then
p_lngFlags = EWX_SHUTDOWN Or EWX_POWEROFF
Else
p_lngFlags = EWX_SHUTDOWN Or EWX_POWEROFF Or EWX_FORCE
End If

p_lngRtn = ExitWindowsEx(p_lngFlags, 0&)
End Sub

真方天画戟 2003-10-14
  • 打赏
  • 举报
回复
你的程序的优先级不够
rainstormmaster 2003-10-14
  • 打赏
  • 举报
回复
在nt下,需要获得关机权限

1,486

社区成员

发帖
与我相关
我的任务
社区描述
VB API
社区管理员
  • API
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧