CheckTokenMembership在VB6 - 崩溃FreeSID在Windows 7和Windows 2008 [英] CheckTokenMembership in VB6 - Crashing on FreeSID on Windows 7 and Windows 2008
问题描述
我使用 CheckToken会员 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
崩溃是间歇性的。
有人遇到这个问题吗?
谢谢!
编辑:
我刚刚重新检查了我的代码,我发现我声明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?
推荐答案
在a中分隔 pvIsAdmin
完全独立的模块和复制函数声明从代码片段。在 AllocateAndInitializeSid
lpPSid是ByRef。在 FreeSid
param是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.
这篇关于CheckTokenMembership在VB6 - 崩溃FreeSID在Windows 7和Windows 2008的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!