首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >即使在调整我的权限令牌之后,ExitWindowsEx也会失败

即使在调整我的权限令牌之后,ExitWindowsEx也会失败
EN

Stack Overflow用户
提问于 2011-10-25 14:37:41
回答 1查看 939关注 0票数 0

我正在尝试以编程方式关闭Windows:

代码语言:javascript
复制
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 (未持有特权)。重新启动有相同的问题,但注销工作。

我在这里做错了什么?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-11-18 18:25:51

您正在使用错误的mb调用LookupPrivilegeValueW,并将错误的mb传递给AdjustTokenPrivileges。

代码语言:javascript
复制
Dim luid As New MemoryBlock(8)
If LookupPrivilegeValueW(Nil, "SeShutdownPrivilege", luid) Then     
   Dim mb As New MemoryBlock(16)
   mb.UInt32Value(0) = 1
   mb.UInt32Value(4) = luid.UInt32Value(0)
   mb.UInt32Value(8) = luid.UInt32Value(4)
   mb.UInt32Value(12) = SE_PRIVILEGE_ENABLED
   Dim z As Integer
   If AdjustTokenPrivileges(tHandle, False, mb, mb.Size, pt, z) Then
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7885507

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档