即使调整我的权限令牌后,ExitWindowsEx 也会失败
我试图以编程方式关闭 Windows:
Function ExitWindows() As Integer
Declare Function GetCurrentProcess Lib "Kernel32" () As Integer
Declare Function OpenProcessToken Lib "AdvApi32" (handle As Integer, access As Integer, ByRef tHandle As Integer) As Boolean
Declare Function LookupPrivilegeValueW Lib "AdvApi32" (sysName As Ptr, privName As WString, Luid As Ptr) As Boolean
Declare Function AdjustTokenPrivileges Lib "AdvApi32" (tHandle As Integer, disableAllPrivs As Boolean, newState As Ptr, buffLength As Integer, prevPrivs As Ptr, ByRef retLen As Integer) As Boolean
Declare Function ExitWindowsEx Lib "User32" (flags As Integer, reason As Integer) As Boolean
Declare Function GetLastError Lib "Kernel32" () As Integer
Const SE_PRIVILEGE_ENABLED = &h00000002
Const TOKEN_QUERY = &h00000008
Const TOKEN_ADJUST_PRIVILEGES = &h00000020
Const SE_SHUTDOWN_NAME = "SeShutdownPrivilege"
Const EWX_SHUTDOWN = &h00000001
Dim pHandle As Integer = GetCurrentProcess() //a handle to the current process
Dim tHandle As Integer //a handle to the token
If OpenProcessToken(pHandle, TOKEN_ADJUST_PRIVILEGES Or TOKEN_QUERY, tHandle) Then
Dim mb As New MemoryBlock(8)
mb.UInt32Value(0) = 1
mb.Int32Value(4) = SE_PRIVILEGE_ENABLED
Dim pt As Ptr
If LookupPrivilegeValueW(Nil, "SeShutdownPrivilege", mb) Then
Dim z As Integer
If AdjustTokenPrivileges(tHandle, False, mb, mb.Size, pt, z) Then
If Not ExitWindowsEx(EWX_SHUTDOWN, 0) Then
Return GetLastError() //Returns 1314
End If
Else
Return GetLastError()
End If
Else
Return GetLastError()
End If
Else
Return GetLastError()
End If
End Function
除了 ExitWindowsEx 之外,每个函数调用都会成功,即使以管理员身份运行,它也总是会失败,并显示错误代码 1314(未保留权限)。重新启动有同样的问题,但注销有效。
我在这里做错了什么?
I'm trying to shutdown Windows programmatically:
Function ExitWindows() As Integer
Declare Function GetCurrentProcess Lib "Kernel32" () As Integer
Declare Function OpenProcessToken Lib "AdvApi32" (handle As Integer, access As Integer, ByRef tHandle As Integer) As Boolean
Declare Function LookupPrivilegeValueW Lib "AdvApi32" (sysName As Ptr, privName As WString, Luid As Ptr) As Boolean
Declare Function AdjustTokenPrivileges Lib "AdvApi32" (tHandle As Integer, disableAllPrivs As Boolean, newState As Ptr, buffLength As Integer, prevPrivs As Ptr, ByRef retLen As Integer) As Boolean
Declare Function ExitWindowsEx Lib "User32" (flags As Integer, reason As Integer) As Boolean
Declare Function GetLastError Lib "Kernel32" () As Integer
Const SE_PRIVILEGE_ENABLED = &h00000002
Const TOKEN_QUERY = &h00000008
Const TOKEN_ADJUST_PRIVILEGES = &h00000020
Const SE_SHUTDOWN_NAME = "SeShutdownPrivilege"
Const EWX_SHUTDOWN = &h00000001
Dim pHandle As Integer = GetCurrentProcess() //a handle to the current process
Dim tHandle As Integer //a handle to the token
If OpenProcessToken(pHandle, TOKEN_ADJUST_PRIVILEGES Or TOKEN_QUERY, tHandle) Then
Dim mb As New MemoryBlock(8)
mb.UInt32Value(0) = 1
mb.Int32Value(4) = SE_PRIVILEGE_ENABLED
Dim pt As Ptr
If LookupPrivilegeValueW(Nil, "SeShutdownPrivilege", mb) Then
Dim z As Integer
If AdjustTokenPrivileges(tHandle, False, mb, mb.Size, pt, z) Then
If Not ExitWindowsEx(EWX_SHUTDOWN, 0) Then
Return GetLastError() //Returns 1314
End If
Else
Return GetLastError()
End If
Else
Return GetLastError()
End If
Else
Return GetLastError()
End If
End Function
Each function call succeeds except for ExitWindowsEx, which invariably will fail with error code 1314 (Privilege not held) even when running as Admin. Reboot has the same problem but Logoff works.
What am I doing wrong here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您使用错误的 mb 调用 LookupPrivilegeValueW 并将错误的 mb 传递给 AdjustTokenPrivileges。
You are calling LookupPrivilegeValueW with a wrong mb and passing a wrong mb to AdjustTokenPrivileges.