5

我有一些我们必须在 VB6 中维护的旧代码。我们需要为其添加查找当前用户SID 的功能。谁能给我指出一些显示如何做到这一点的代码?在此先感谢您的帮助!

4

4 回答 4

9

尝试这个

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
于 2009-10-20T23:40:58.930 回答
1

尝试实现这个功能:

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

这是实现它的链接。

于 2009-10-20T21:13:46.290 回答
0

稍微浏览一下 MSDN 表明 NetUserGetInfo 函数是您正在寻找的函数 ( http://msdn.microsoft.com/en-us/library/aa370654%28VS.85%29.asp x)。微软已经在这里为你写了一个例子:http: //support.microsoft.com/kb/151774

于 2009-10-20T21:21:56.133 回答
0

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

该演示展示了如何调用 GetCurrentProcess、LookupAccountSid、AllocateAndInitializeSid、OpenProcessToken 和 GetTokenInformation 以确定当前进程是否在管理员帐户下执行。这通常意味着用户是 admin 组的成员,但在某些情况下(例如作为服务运行,或可能指定与 GetCurrentProcess 返回的进程不同的进程),返回值指示该进程是否在帐户下运行具有管理员权限。(请注意,最准确的说明应该是“当前进程”而不是“用户”。)

于 2009-10-22T08:05:23.930 回答