如何在VB6中获取当前用户的SID?

发布于 2024-08-08 14:04:51 字数 84 浏览 1 评论 0 原文

我有一些必须在 VB6 中维护的旧代码。我们需要添加查找当前用户的 SID 的功能。任何人都可以向我指出一些显示如何执行此操作的代码吗?预先感谢您的帮助!

I have some old code that we have to maintain in VB6. We need to add the ability for it to look up the current user's SID. Can anyone point me to some code that shows how to do that? Thanks in advance for your help!

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

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

发布评论

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

评论(4

梦里人 2024-08-15 14:04:51

试试这个

Option Explicit

'--- for OpenProcessToken
Private Const TOKEN_READ                    As Long = &H20008

Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (pTo As Any, uFrom As Any, ByVal lSize As Long)
Private Declare Function GetCurrentProcess Lib "kernel32" () As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
Private Declare Function OpenProcessToken Lib "advapi32.dll" (ByVal ProcessHandle As Long, ByVal DesiredAccess As Long, TokenHandle As Long) As Long
Private Declare Function GetTokenInformation Lib "advapi32.dll" (ByVal TokenHandle As Long, ByVal TokenInformationClass As Long, TokenInformation As Any, ByVal TokenInformationLength As Long, ReturnLength As Long) As Long
Private Declare Function ConvertSidToStringSid Lib "advapi32.dll" Alias "ConvertSidToStringSidA" (ByVal lpSid As Long, lpString As Long) As Long
Private Declare Function LocalFree Lib "kernel32" (ByVal hMem As Long) As Long
Private Declare Function lstrlen Lib "kernel32" Alias "lstrlenA" (ByVal lpString As Long) As Long

Public Function GetCurrentUserSid() As String
    Dim hProcessID      As Long
    Dim hToken          As Long
    Dim lNeeded         As Long
    Dim baBuffer()      As Byte
    Dim sBuffer         As String
    Dim lpSid           As Long
    Dim lpString        As Long

    hProcessID = GetCurrentProcess()
    If hProcessID <> 0 Then
        If OpenProcessToken(hProcessID, TOKEN_READ, hToken) = 1 Then
            Call GetTokenInformation(hToken, 1, ByVal 0, 0, lNeeded)
            ReDim baBuffer(0 To lNeeded)
            '--- enum TokenInformationClass { TokenUser = 1, TokenGroups = 2, ... }
            If GetTokenInformation(hToken, 1, baBuffer(0), UBound(baBuffer), lNeeded) = 1 Then
                Call CopyMemory(lpSid, baBuffer(0), 4)
                If ConvertSidToStringSid(lpSid, lpString) Then
                    sBuffer = Space(lstrlen(lpString))
                    Call CopyMemory(ByVal sBuffer, ByVal lpString, Len(sBuffer))
                    Call LocalFree(lpString)
                    GetCurrentUserSid = sBuffer
                End If
            End If
            Call CloseHandle(hToken)
        End If
        Call CloseHandle(hProcessID)
    End If
End Function

Try this

Option Explicit

'--- for OpenProcessToken
Private Const TOKEN_READ                    As Long = &H20008

Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (pTo As Any, uFrom As Any, ByVal lSize As Long)
Private Declare Function GetCurrentProcess Lib "kernel32" () As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
Private Declare Function OpenProcessToken Lib "advapi32.dll" (ByVal ProcessHandle As Long, ByVal DesiredAccess As Long, TokenHandle As Long) As Long
Private Declare Function GetTokenInformation Lib "advapi32.dll" (ByVal TokenHandle As Long, ByVal TokenInformationClass As Long, TokenInformation As Any, ByVal TokenInformationLength As Long, ReturnLength As Long) As Long
Private Declare Function ConvertSidToStringSid Lib "advapi32.dll" Alias "ConvertSidToStringSidA" (ByVal lpSid As Long, lpString As Long) As Long
Private Declare Function LocalFree Lib "kernel32" (ByVal hMem As Long) As Long
Private Declare Function lstrlen Lib "kernel32" Alias "lstrlenA" (ByVal lpString As Long) As Long

Public Function GetCurrentUserSid() As String
    Dim hProcessID      As Long
    Dim hToken          As Long
    Dim lNeeded         As Long
    Dim baBuffer()      As Byte
    Dim sBuffer         As String
    Dim lpSid           As Long
    Dim lpString        As Long

    hProcessID = GetCurrentProcess()
    If hProcessID <> 0 Then
        If OpenProcessToken(hProcessID, TOKEN_READ, hToken) = 1 Then
            Call GetTokenInformation(hToken, 1, ByVal 0, 0, lNeeded)
            ReDim baBuffer(0 To lNeeded)
            '--- enum TokenInformationClass { TokenUser = 1, TokenGroups = 2, ... }
            If GetTokenInformation(hToken, 1, baBuffer(0), UBound(baBuffer), lNeeded) = 1 Then
                Call CopyMemory(lpSid, baBuffer(0), 4)
                If ConvertSidToStringSid(lpSid, lpString) Then
                    sBuffer = Space(lstrlen(lpString))
                    Call CopyMemory(ByVal sBuffer, ByVal lpString, Len(sBuffer))
                    Call LocalFree(lpString)
                    GetCurrentUserSid = sBuffer
                End If
            End If
            Call CloseHandle(hToken)
        End If
        Call CloseHandle(hProcessID)
    End If
End Function
喜你已久 2024-08-15 14:04:51

尝试实现此功能:

Declare Function LookupAccountSid Lib "advapi32.dll" _
      Alias "LookupAccountSidA" (ByVal lpSystemName As String, _
      ByVal Sid As Long, ByVal Name As String, cbName As Long, _
      ByVal ReferencedDomainName As String, _
      cbReferencedDomainName As Long, peUse As Integer) As Long

这是一个已实现的链接

Try to implement this function:

Declare Function LookupAccountSid Lib "advapi32.dll" _
      Alias "LookupAccountSidA" (ByVal lpSystemName As String, _
      ByVal Sid As Long, ByVal Name As String, cbName As Long, _
      ByVal ReferencedDomainName As String, _
      cbReferencedDomainName As Long, peUse As Integer) As Long

Here's a link to it implemented.

萌吟 2024-08-15 14:04:51

对 MSDN 的一点浏览表明 NetUserGetInfo 函数就是您正在寻找的函数(http://msdn.microsoft.com/en-us/library/aa370654%28VS.85%29.aspx)。 Microsoft 已经在这里为您编写了一个示例: http://support.microsoft.com/kb/151774

A little cruising of the MSDN suggests that the NetUserGetInfo function is the one you're looking for (http://msdn.microsoft.com/en-us/library/aa370654%28VS.85%29.aspx). And Microsoft has already written an example for you here: http://support.microsoft.com/kb/151774

我的痛♀有谁懂 2024-08-15 14:04:51

这是示例,来自名称混乱但值得信赖的VB NET 站点(在 Microsoft 发明 .NET 之前专门用于 VB6) )

这个演示展示了如何调用
获取当前进程,查找帐户Sid,
分配并初始化Sid,
OpenProcessToken,以及
获取TokenInformation以便
判断当前进程是否为
在管理员下执行
帐户。这通常意味着
用户是管理员组的成员,
但在某些情况下(例如
作为服务运行,或者可能
指定一个不同于
由 GetCurrentProcess 返回),
返回值表明是否
进程正在帐户下运行
具有管理员权限。 (注意
插图应该说“当前
“进程”而不是“用户”才是最重要的
准确。)

Here is a sample from the confusingly named but trustworthy VB NET site (which was devoted to VB6 before Microsoft invented .NET)

This demo shows how to call
GetCurrentProcess, LookupAccountSid,
AllocateAndInitializeSid,
OpenProcessToken, and
GetTokenInformation in order to
determine if the current process is
executing under an administrator
account. This typically means the
user is a member of the admin group,
but under certain circumstances (e.g.
running as a service, or possibly
specifying a process different than
that returned by GetCurrentProcess),
the return value indicates whether the
process is being run under an account
with admin rights. (Note the
illustration should say 'current
process' rather than 'user' to be most
accurate.)

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