Click to See Complete Forum and Search --> : Please Help (Info on SID)


January 6th, 2000, 01:29 PM
I am trying to return the SID on a specified user and domain.
Can someone please tell me what I am missing in my code....

I can succesfully get the lenght but am unable to return it.

Private Declare Function GetUserName Lib "advapi32.dll" Alias "GetUserNameA" (ByVal lpBuffer As String, nSize As Long) As Long

' ---------------------------------------------
' Used by private LookupSID()
' ---------------------------------------------
Private Declare Function LookupAccountName _
Lib "advapi32.dll" Alias "LookupAccountNameA" _
(lpSystemName As String, _
ByVal lpAccountName As String, _
SID As Any, cbSID As Long, _
ByVal ReferencedDomainName As String, _
cbReferencedDomainName As Long, _
peUse As Long) As Long

Public Enum enumSID_NAME_USE
SidTypeUser = 1&
SidTypeGroup = 2&
SidTypeDomain = 3&
SidTypeAlias = 4&
SidTypeWellKnownGroup = 5&
SidTypeDeletedAccount = 6&
SidTypeInvalid = 7&
SidTypeUnknown = 8&
End Enum
Sub Main()
Dim p_abytUserSID() As Byte
Dim p_strDomainName As String
Dim p_strName As String


Dim sBuffer As String
Dim lRet As Long
Dim sUserName As String
'Fill the buffer with char(0)
sBuffer = String$(255, vbNullChar)
lRet = GetUserName(sBuffer, 255)
If lRet = 1 Then
sUserName = Left$(sBuffer, InStr(sBuffer, vbNullChar) - 1)
Else
sUserName = "Unknown - error"
End If
p_strDomainName = "DOMAINNAME"
p_strUserName = sUserName

LookupSID xi_strUserID:=p_strUserName, _
xo_strDomainName:=p_strDomainName, _
xo_abytUserSID:=p_abytUserSID()


MsgBox sUserName + " SID # is "
End Sub

Public Sub LookupSID(ByVal xi_strUserID As String, ByRef xo_strDomainName As String, ByRef xo_abytUserSID() As Byte)
'On Error GoTo LookupSID_Err
Dim p_lngRtn As Long
Dim p_lngSID_Type As Long
Dim p_lngLenSid As Long
Dim p_lngLenDomainName As Long
Dim p_abytUserSID() As Byte

' ------------------------------------------
' Dimenison the byte array
' ------------------------------------------
ReDim xo_abytUserSID(0 To 255) As Byte
p_lngLenSid = 255

' ------------------------------------------
' The first call will just get the required
' sizes for SID and Domain name
' ------------------------------------------
p_lngRtn = LookupAccountName(lpSystemName:=vbNullString, _
lpAccountName:=xi_strUserID, _
SID:=xo_abytUserSID(0), _
cbSID:=p_lngLenSid, _
ReferencedDomainName:=xo_strDomainName, _
cbReferencedDomainName:=p_lngLenDomainName, _
peUse:=p_lngSID_Type)

' ------------------------------------------
' Now set the string for receiving the Domain
' name before calling API again
' ------------------------------------------
xo_strDomainName = Space$(p_lngLenDomainName)

' ------------------------------------------
' Make the API call again to get the actual SID
' and Domain name
' ------------------------------------------
p_lngRtn = LookupAccountName(lpSystemName:=vbNullString, _
lpAccountName:=xi_strUserID, _
SID:=xo_abytUserSID(0), _
cbSID:=p_lngLenSid, _
ReferencedDomainName:=xo_strDomainName, _
cbReferencedDomainName:=p_lngLenDomainName, _
peUse:=p_lngSID_Type)

' ------------------------------------------
' If the function succeeds, the return value
' is nonzero.
' ------------------------------------------
If p_lngRtn = 0 Then
xo_strDomainName = vbNullString
On Error GoTo 0
Err.Raise Number:=Err.LastDllError + g_ErrConstant, _
Description:="Could not lookup the Account Name.", _
Source:=constSource
Else

' ---------------------------------------
' Reset the sizes of the SID and the
' domain name to their actual sizes
' ---------------------------------------
ReDim Preserve xo_abytUserSID(0 To p_lngLenSid)
xo_strDomainName = Mid$(xo_strDomainName, 1, p_lngLenDomainName)
End If

Exit Sub
'LookupSID_Err:
'm_strErrStr = Err.Description
' m_strErrSource = constSource
' m_lngErrNum = Err.Number
' If Abs(g_ErrConstant) <= Abs(m_lngErrNum) Then
' m_lngErrNum = m_lngErrNum + g_ErrConstant
' End If
'Err.Raise Description:="Unexpected Error: " & Err.Description, _
' Number:=Err.Number, _
' Source:=constSource


End Sub