VB6 中的 CheckTokenMembership - 在 Windows 7 和 Windows 2008 上的 FreeSID 崩溃 [英] CheckTokenMembership in VB6 - Crashing on FreeSID on Windows 7 and Windows 2008

查看:19
本文介绍了VB6 中的 CheckTokenMembership - 在 Windows 7 和 Windows 2008 上的 FreeSID 崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 CheckTokenMembership用于检查用户是否为管理员的 Windows API.

I am using the CheckTokenMembership Windows API to check if the user is an Administrator.

代码如下:

Option Explicit

Private Const SECURITY_BUILTIN_DOMAIN_RID       As Long = &H20
Private Const DOMAIN_ALIAS_RID_ADMINS           As Long = &H220

Private Declare Function AllocateAndInitializeSid Lib "advapi32.dll" (pIdentifierAuthority As Any, ByVal nSubAuthorityCount As Byte, ByVal nSubAuthority0 As Long, ByVal nSubAuthority1 As Long, ByVal nSubAuthority2 As Long, ByVal nSubAuthority3 As Long, ByVal nSubAuthority4 As Long, ByVal nSubAuthority5 As Long, ByVal nSubAuthority6 As Long, ByVal nSubAuthority7 As Long, lpPSid As Long) As Long
Private Declare Sub FreeSid Lib "advapi32.dll" (ByVal pSid As Long)
Private Declare Function CheckTokenMembership Lib "advapi32.dll" (ByVal hToken As Long, ByVal pSidToCheck As Long, pbIsMember As Long) As Long

Private Type SID_IDENTIFIER_AUTHORITY
    Value(0 To 5)            As Byte
End Type

Private Function pvIsAdmin() As Boolean
    Dim uAuthNt         As SID_IDENTIFIER_AUTHORITY
    Dim pSidAdmins      As Long
    Dim lResult         As Long

    uAuthNt.Value(5) = 5
    If AllocateAndInitializeSid(uAuthNt, 2, SECURITY_BUILTIN_DOMAIN_RID, DOMAIN_ALIAS_RID_ADMINS, 0, 0, 0, 0, 0, 0, pSidAdmins) <> 0 Then
        If CheckTokenMembership(0, pSidAdmins, lResult) <> 0 Then
            pvIsAdmin = (lResult <> 0)
        End If
        Call FreeSid(pSidAdmins)
    End If
End Function

问题是在 Windows 7 和 Windows 2008 SP2 上,对 FreeSID 的调用导致应用程序崩溃.崩溃是间歇性的.

Problem is that on Windows 7 and Windows 2008 SP2, the call to FreeSID is causing the app to crash. The crash is intermittent.

有人遇到过这个问题吗?

Has anyone encountered this problem?

谢谢!

我刚刚重新检查了我的代码,发现我这样声明了 FreeSID:

I just rechecked my code and I found out that I declared FreeSID as such:

Private Declare Sub FreeSid Lib "advapi32.dll" (pSid As Long)

与上面的代码相比,这里的pSid参数没有被标记为ByVal.我添加了 ByVal 标志,问题不再存在.不知何故,我不相信这能解决问题.这可能解决了问题吗?

As compared to the above code, the pSid parameter here is not flagged as ByVal. I added the ByVal flag and the problem is no longer present. Somehow, I am not convinced that this fixed the problem. Can this possibly have fixed the problem?

推荐答案

pvIsAdmin 分离到一个完全独立的模块中,并从代码片段中逐字复制函数声明.在 AllocateAndInitializeSid 中,lpPSid 是 ByRef.在 FreeSid 中,参数是 ByVal.

Separate pvIsAdmin in a completely separate module and copy function declarations verbatim from the snippet. In AllocateAndInitializeSid lpPSid is ByRef. In FreeSid param is ByVal.

这篇关于VB6 中的 CheckTokenMembership - 在 Windows 7 和 Windows 2008 上的 FreeSID 崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆