开发者

How can I get the current user's SID in VB6?

开发者 https://www.devze.com 2022-12-09 14:20 出处:网络
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 adva开发者

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 adva开发者_JAVA技巧nce for your help!


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


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.


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


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.)

0

精彩评论

暂无评论...
验证码 换一张
取 消