VB6 中的 CheckTokenMembership

发布于 2024-08-14 03:15:26 字数 1299 浏览 3 评论 0原文

我很难将这个 C++ 代码转换为 VB6 代码。我在网上搜索过,但没有找到任何东西。 PInvoke.net 仅引用了 VB.NET 代码。以下是来自 MSDN 的代码:

BOOL IsUserAdmin(VOID)
/*++ 
Routine Description: This routine returns TRUE if the caller's
process is a member of the Administrators local group. Caller is NOT
expected to be impersonating anyone and is expected to be able to
open its own process and process token. 
Arguments: None. 
Return Value: 
   TRUE - Caller has Administrators local group. 
   FALSE - Caller does not have Administrators local group. --
*/
{
    BOOL b;
    SID_IDENTIFIER_AUTHORITY NtAuthority = SECURITY_NT_AUTHORITY;
    PSID AdministratorsGroup; 
    b = AllocateAndInitializeSid(
        &NtAuthority,
        2,
        SECURITY_BUILTIN_DOMAIN_RID,
        DOMAIN_ALIAS_RID_ADMINS,
        0, 0, 0, 0, 0, 0,
        &AdministratorsGroup); 
    if(b) 
    {
        if (!CheckTokenMembership( NULL, AdministratorsGroup, &b)) 
        {
             b = FALSE;
        } 
        FreeSid(AdministratorsGroup); 
    }
    
    return(b);
}

如果有人能帮忙将其转换为 VB6 代码,那就太好了。

谢谢!

编辑:
我原本打算使用该功能,但 MSDN 说:

此函数是 CheckTokenMembership 的包装器。建议直接调用该函数来确定管理员组状态,而不是调用 IsUserAnAdmin

I'm having a hard time converting this C++ code to VB6 code. I've search the net and haven't found anything. PInvoke.net only has reference to VB.NET code. Here's the code from MSDN:

BOOL IsUserAdmin(VOID)
/*++ 
Routine Description: This routine returns TRUE if the caller's
process is a member of the Administrators local group. Caller is NOT
expected to be impersonating anyone and is expected to be able to
open its own process and process token. 
Arguments: None. 
Return Value: 
   TRUE - Caller has Administrators local group. 
   FALSE - Caller does not have Administrators local group. --
*/
{
    BOOL b;
    SID_IDENTIFIER_AUTHORITY NtAuthority = SECURITY_NT_AUTHORITY;
    PSID AdministratorsGroup; 
    b = AllocateAndInitializeSid(
        &NtAuthority,
        2,
        SECURITY_BUILTIN_DOMAIN_RID,
        DOMAIN_ALIAS_RID_ADMINS,
        0, 0, 0, 0, 0, 0,
        &AdministratorsGroup); 
    if(b) 
    {
        if (!CheckTokenMembership( NULL, AdministratorsGroup, &b)) 
        {
             b = FALSE;
        } 
        FreeSid(AdministratorsGroup); 
    }
    
    return(b);
}

It would be great if somebody can help out in converting this to VB6 code.

Thanks!

EDIT:
I was originally going to use that function but MSDN says:

This function is a wrapper for CheckTokenMembership. It is recommended to call that function directly to determine Administrator group status rather than calling IsUserAnAdmin.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

情愿 2024-08-21 03:15:26

试试这个

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

Try this

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
墨洒年华 2024-08-21 03:15:26

您已发布 CheckTokenMembership 的 MSDN 示例代码 - 它使用 CheckTokenMembership 来确定用户是否是管理员。

在 VB6 中,使用 IsUserAnAdmin 更容易,它是 CheckTokenMembership 的包装器。 MSDN 文档 IsUserAnAdmin 已弃用,但它比 CheckTokenMembership 更容易调用。

Private Declare Function IsUserAnAdmin Lib "Shell32" Alias "#680" () As Integer
If IsUserAnAdmin() = 0 Then 
  MsgBox "Not admin" 
Else 
  MsgBox "Admin" 
End If

You've posted the MSDN sample code for CheckTokenMembership - it uses CheckTokenMembership to determine whether the user is an administrator.

In VB6 it's easier to use IsUserAnAdmin, which is a wrapper for CheckTokenMembership. The MSDN docs do say IsUserAnAdmin is deprecated, but it's so much easier to call than CheckTokenMembership.

Private Declare Function IsUserAnAdmin Lib "Shell32" Alias "#680" () As Integer
If IsUserAnAdmin() = 0 Then 
  MsgBox "Not admin" 
Else 
  MsgBox "Admin" 
End If
尽揽少女心 2024-08-21 03:15:26

除非有原因需要转换代码,否则请使用 API

Private Declare Function IsUserAdmin Lib "Shell32" Alias "#680" () As Boolean

Private Sub Form_Load()
   If IsUserAdmin Then MsgBox "User is Admin"
End Sub

Unless there is a reason to convert the code, use the API

Private Declare Function IsUserAdmin Lib "Shell32" Alias "#680" () As Boolean

Private Sub Form_Load()
   If IsUserAdmin Then MsgBox "User is Admin"
End Sub
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文