Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

SID - LookUpAccountName-GetTextualSID

Posted on 1999-07-26
7
Medium Priority
?
1,807 Views
Last Modified: 2008-03-10
I need to see a VB function like this:

Function GetAccountSIDText(Username as String, Domain as String)

'Insert Genius Here
GetAccountSIDText = sResult
End Function

SID should be converted to text via MSKB - Q131320. Please show VB code only, don't compile the c sample and call it.
0
Comment
Question by:bshuler072699
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
7 Comments
 
LVL 10

Expert Comment

by:viktornet
ID: 1527492
could you give me the exact URL to that article or C code? Thanks!
0
 
LVL 10

Expert Comment

by:viktornet
ID: 1527493
I think I found it, but it says getTextualSid() is that what you need?
0
 
LVL 1

Author Comment

by:bshuler072699
ID: 1527494
Yes, the C prog has a function called getTextualSid(). I can get the LookUpAccountName to return success by pushing the sid into a byte array, but when I pass aSID(0) to IsValidSID it returns false. This makes me think my method might be suspect.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 10

Expert Comment

by:viktornet
ID: 1527495
I could convert it to VB without any declarations because i'm too lazy.. i can just convert the syntax and everything.... but not search for the constant numbers and stuff., if you're satisfied with that, please let me know and I will do it.,

..-=ViKtOr=-..
0
 
LVL 1

Author Comment

by:bshuler072699
ID: 1527496
To be honest, i am looking for someone who has already done it and can pull it from their archive. I would hate to see you have to write this for 200 pts on some web site.. Thanks for the interest though!
0
 
LVL 6

Accepted Solution

by:
setiawan earned 800 total points
ID: 1527497
'save as GetSid.frm
VERSION 5.00
Begin VB.Form frmLookAcct
   BorderStyle     =   1  'Fixed Single
   ClientHeight    =   4050
   ClientLeft      =   45
   ClientTop       =   330
   ClientWidth     =   4935
   LinkTopic       =   "Form1"
   MaxButton       =   0   'False
   MinButton       =   0   'False
   ScaleHeight     =   4050
   ScaleWidth      =   4935
   StartUpPosition =   2  'CenterScreen
   Begin VB.Frame Frame2
      Caption         =   " API Results "
      Enabled         =   0   'False
      Height          =   1815
      Left            =   120
      TabIndex        =   6
      Top             =   1560
      Width           =   4695
      Begin VB.TextBox txtLookupAccountName
         Height          =   325
         Left            =   120
         Locked          =   -1  'True
         TabIndex        =   10
         Top             =   1200
         Width           =   4455
      End
      Begin VB.TextBox txtLookupAccountSid
         Height          =   325
         Left            =   120
         Locked          =   -1  'True
         TabIndex        =   9
         Top             =   600
         Width           =   4455
      End
      Begin VB.Label Label2
         BackStyle       =   0  'Transparent
         Caption         =   "LookupAccountSid:"
         Height          =   255
         Left            =   120
         TabIndex        =   8
         Top             =   960
         Width           =   1695
      End
      Begin VB.Label Label1
         BackStyle       =   0  'Transparent
         Caption         =   "LookupAccountName:"
         Height          =   255
         Left            =   120
         TabIndex        =   7
         Top             =   360
         Width           =   1575
      End
   End
   Begin VB.Frame Frame1
      Caption         =   "Submit any account. eg. users, groups, etc."
      Height          =   1335
      Left            =   120
      TabIndex        =   1
      Top             =   120
      Width           =   4695
      Begin VB.TextBox txtDomain
         Height          =   325
         Left            =   1440
         TabIndex        =   3
         Top             =   720
         Width           =   2295
      End
      Begin VB.TextBox txtAccount
         Height          =   325
         Left            =   1440
         TabIndex        =   2
         Top             =   360
         Width           =   2295
      End
      Begin VB.Label lblDomainName
         Alignment       =   1  'Right Justify
         BackStyle       =   0  'Transparent
         Caption         =   "Domain: "
         Height          =   255
         Left            =   480
         TabIndex        =   5
         Top             =   780
         Width           =   855
      End
      Begin VB.Label lblAccount
         Alignment       =   1  'Right Justify
         BackStyle       =   0  'Transparent
         Caption         =   "Account: "
         Height          =   255
         Left            =   480
         TabIndex        =   4
         Top             =   420
         Width           =   855
      End
   End
   Begin VB.CommandButton cmdOK
      Caption         =   "Get the SID"
      Height          =   495
      Left            =   1320
      TabIndex        =   0
      Top             =   3480
      Width           =   2295
   End
End
Attribute VB_Name = "frmLookAcct"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
' Developed for you by Elvio Serrao
' Elvio.Serrao@nrma.com.au

Option Explicit

Private CurrentUser     As String

Private Sub cmdOK_Click()
    Dim DCName          As String
    Dim SIDText         As String
    Dim UserSID         As String
   
    If Len(txtAccount) = 0 Then
        txtAccount.SetFocus
        MsgBox "No account type has been supplied.", vbExclamation, App.Title
        Exit Sub
    End If
    If Len(txtDomain) = 0 Then
        txtDomain.SetFocus
        MsgBox "No domain name has been supplied.", vbExclamation, App.Title
        Exit Sub
    End If
    DCName = GetPrimaryDCName(vbNullString, txtDomain)
    SIDText = GetSID(DCName, txtDomain, txtAccount)
    If Len(SIDText) > 0 Then UserSID = GetAccount()
    txtLookupAccountSid = SIDText
    txtLookupAccountName = UserSID
    txtAccount.SetFocus
    Frame2.Enabled = True
End Sub

Private Sub Form_Load()
    Dim lSize           As Long
    Dim sBuffer         As String
   
    Me.Caption = App.Title
    sBuffer = Space$(255)
    lSize = Len(sBuffer)
    Call GetUserName(sBuffer, lSize)
    If lSize > 0 Then CurrentUser = Left(sBuffer, InStr(sBuffer, Chr(0)) - 1) ' Remove the end null character to keep it in a string format
    txtAccount = CurrentUser
    txtDomain = GetDomainName
End Sub

Private Sub txtDomain_KeyPress(KeyAscii As Integer)
    KeyAscii = Upper(KeyAscii)
    If Len(txtDomain) >= LM20_DNLEN And KeyAscii <> vbKeyBack Then KeyAscii = 0
End Sub


'save as GetSid.bas
Attribute VB_Name = "modLookAcct"
' Developed for you by Elvio Serrao
' Elvio.Serrao@nrma.com.au

'=====================================================================================
' The following sample code displays the textual representation of the SID associated
' with the current user. It demonstrates the use of the LookupAccountSid API and the
' LookupAccountName API which can be applied to Win32 API development.

' It may be useful to convert a binary SID (security identifier) to a readable,
' textual form, for display or manipulation purposes.

' One example of an application that makes use of SIDs in textual form is the Windows
' NT Event Viewer. If the Event Viewer cannot look up the name associated with the SID
' of a logged event, the Event Viewer displays a textual representation of the SID.
' Windows NT also makes use of textual SIDs when loading user configuration hives into
' the HKEY_USERS registry key. This can then be used identify a user's profile path
'=====================================================================================

Option Explicit

Public Const LM20_CNLEN = 15                    ' LM 2.0 Computer name length
Public Const LM20_DNLEN = LM20_CNLEN            ' LM 2.0 Maximum domain name length

 
Private Const SID_REVISION = (1)                       ' Current revision level
Private Const SID_MAX_SUB_AUTHORITIES = (15)
Private Const SID_RECOMMENDED_SUB_AUTHORITIES = (1)    ' Will change to around 6 in a future release.

Private Type WKSTA_INFO_100
    wki100_platform_id  As Long
    wki100_computername As Long
    wki100_langroup     As Long
    wki100_ver_major    As Long
    wki100_ver_minor    As Long
End Type

Type SID_IDENTIFIER_AUTHORITY
'    Value(6) As Byte
    Value(0 To 5) As Byte
End Type

Private Declare Function NetAPIBufferFree Lib "Netapi32" Alias _
    "NetApiBufferFree" (ByVal Ptr As Long) As Long

Private Declare Function LookupAccountName Lib "advapi32.dll" Alias _
    "LookupAccountNameA" (ByVal lpSystemName As String, _
    ByVal lpAccountName As String, Sid As Long, cbSid As Long, _
    ByVal ReferencedDomainName As String, cbReferencedDomainName As Long, _
    peUse As Integer) As Long

Private Declare Function LookupAccountSid Lib "Advapi32" Alias _
    "LookupAccountSidA" (ByVal lpSystemName As String, Sid As Any, _
    ByVal name As String, cbName As Long, ByVal ReferencedDomainName As String, _
    cbReferencedDomainName As Long, peUse As Integer) As Long

'Private Declare Function GetSidIdentifierAuthority Lib "Advapi32" ( _
'    pSid As Any) As SID_IDENTIFIER_AUTHORITY

Private Declare Function GetSidIdentifierAuthority Lib "Advapi32" ( _
    pSid As Any) As Long

Declare Function IsValidSid Lib "Advapi32" (pSid As Any) As Long

Private Declare Function NetWkstaGetInfo Lib "Netapi32" ( _
    ServerName As Any, ByVal lLevel As Long, vBuffer As Any) As Long

Public Declare Function GetUserName Lib "Advapi32" Alias "GetUserNameA" ( _
    ByVal lpBuffer As String, nSize As Long) As Long

Private Declare Function NetGetDCName Lib "Netapi32" ( _
    ServerName As Byte, DomainName As Byte, DCNPtr As Long) As Long

Private Declare Sub RtlMoveMemory Lib "Kernel32" ( _
    vDest As Any, vSrc As Any, ByVal lSize As Long)

Private Declare Function lstrlenW Lib "Kernel32" (ByVal lpString As Long) As Long

Private Declare Sub lstrcpyW Lib "Kernel32" (vDest As Any, ByVal vSrc As Any)

Public SidBuffer()          As Long

Function GetSID(SName As String, DName As String, UName As String) As String
    Dim InfoBuffer()        As Long ' Allocations should be dynamic
    Dim lResult             As Long
    Dim AllocSize           As Long
    Dim BufferSize          As Long ' Required/provided TextualSid buffersize
    Dim DNameLength         As Long
    Dim I                   As Integer
    Dim X                   As Integer
    Dim pSid                As Long
    Dim SidId               As Long
    Dim dwSubAuthorities    As Long
    Dim Sid                 As String
    Dim SIDType             As Integer
    Dim bSidIdAuth          As Boolean
    Dim tpSid               As SID_IDENTIFIER_AUTHORITY

    SIDType = 0
    BufferSize = 0
    DNameLength = Len(DName) + 1
    SName = SName & vbNullChar
    UName = UName & vbNullChar
    lResult = LookupAccountName(SName, UName, 0, BufferSize, DName, DNameLength, SIDType) ' Determine required buffer with failed result
    DName = Space$(255)
    DNameLength = Len(DName) + 1
    AllocSize = (BufferSize \ 4) - 1
    If AllocSize > 0 Then
        ReDim InfoBuffer(AllocSize)
        lResult = LookupAccountName(SName, UName, InfoBuffer(0), BufferSize, DName, DNameLength, SIDType)
        If lResult <> 1 Then GoTo GetSID_Exit
       
        ' Test to see if the SID is valid
        If IsValidSid(InfoBuffer(0)) Then
            For I = 0 To AllocSize
                If I = 0 Then
               
                    ' Prepare the SID Revision
                    Sid = "S-" + CStr(SID_REVISION)
               
                    ' Retrieve the SID Identifier Authority
                    SidId = GetSidIdentifierAuthority(InfoBuffer(I))
                    Call RtlMoveMemory(tpSid, ByVal SidId, LenB(tpSid))
                    For X = 0 To UBound(tpSid.Value)
                   
                        ' Determine which form the textual sid will take
                        If tpSid.Value(X) <> 0 And SidId < (2 ^ 32) Then
                            bSidIdAuth = True
                            Sid = Sid + "-" + CStr(tpSid.Value(X))
                        End If
                    Next
                ElseIf I = 1 Then
               
                    ' If the IdentifierAuthority value is greater than 2^32
                    ' then the IdentifierAuthority will be in Hex format
                    If Not bSidIdAuth Then Sid = Sid + "-" + CStr(Hex(InfoBuffer(I)))
                Else
                    Sid = Sid + "-" + CStr(InfoBuffer(I))
                End If
                ReDim Preserve SidBuffer(I)
                SidBuffer(I) = InfoBuffer(I)
            Next
        End If
    End If

GetSID_Exit:
    If lResult <> 1 Then MsgBox "Error: " & lResult, vbCritical, App.Title
    GetSID = Sid
End Function

Public Function GetAccount() As String
    Dim lpSid               As Long
    Dim lResult             As Long
    Dim lUName              As Long
    Dim lDName              As Long
    Dim UName               As String
    Dim DName               As String
    Dim SName               As String
    Dim SIDType             As Integer

    SIDType = 0
    UName = Space$(255)
    lUName = Len(UName) + 1
    DName = Space$(255)
    lDName = Len(DName) + 1
    lResult = LookupAccountSid(SName, SidBuffer(0), UName, lUName, DName, lDName, SIDType)
    If lResult <> 0 Then GoTo GetAccount_Exit
    If Len(LTrim$(UName)) > 1 Then UName = Left$(UName, InStr(UName, Chr$(0)) - 1)

GetAccount_Exit:
    If lResult <> 1 Then MsgBox "Error: " & lResult, vbCritical, App.Title
    GetAccount = UName
End Function

Function GetPrimaryDCName(ByVal MName As String, ByVal DName As String) As String
    Dim lResult         As Long
    Dim DCNPtr          As Long
    Dim DNArray()       As Byte
    Dim MNArray()       As Byte
    Dim DCNArray(100)   As Byte
    Dim DCName          As String
       
    MNArray = MName & vbNullChar
    DNArray = DName & vbNullChar
    lResult = NetGetDCName(MNArray(0), DNArray(0), DCNPtr)
    If lResult <> 0 Then GoTo GetPrimaryDCName_Exit
    Call lstrcpyW(DCNArray(0), DCNPtr)
    DCName = DCNArray()
    If Len(LTrim$(DCName)) > 1 Then DCName = Left(DCName, InStr(DCName, Chr(0)) - 1) ' Remove the end null character to keep it in a string format
    GetPrimaryDCName = DCName

GetPrimaryDCName_Exit:
    If lResult <> 0 Then MsgBox "Error: " & lResult, vbCritical, App.Title
    If DCNPtr Then Call NetAPIBufferFree(DCNPtr)          ' Don't leak memory
End Function

Function Upper(KeyAscii As Integer)
    If KeyAscii > 96 And KeyAscii < 123 Then KeyAscii = KeyAscii - 32
    Upper = KeyAscii
End Function

Public Function GetDomainName() As String
    Dim lResult         As Long
    Dim pWkstaInfo      As Long
    Dim bBuffer(512)    As Byte
    Dim DName           As String
    Dim I               As Integer
    Dim tpWkstaInfo     As WKSTA_INFO_100
   
    lResult = NetWkstaGetInfo(ByVal 0&, 100, pWkstaInfo)
    If lResult = 0 Then
        Call RtlMoveMemory(tpWkstaInfo, ByVal pWkstaInfo, Len(tpWkstaInfo))
        GetDomainName = PointerToStringW(tpWkstaInfo.wki100_langroup)
    End If
    If lResult <> 0 Then MsgBox "Error: " & lResult, vbCritical, App.Title
    If pWkstaInfo Then Call NetAPIBufferFree(pWkstaInfo)
End Function

Private Function PointerToStringW(lpStringW As Long) As String
    Dim buffer()            As Byte
    Dim nLen                As Long
   
    If lpStringW Then
        nLen = lstrlenW(lpStringW) * 2
        If nLen Then
            ReDim buffer(0 To (nLen - 1)) As Byte
            Call RtlMoveMemory(buffer(0), ByVal lpStringW, nLen)
            PointerToStringW = buffer
        End If
    End If
End Function

0
 
LVL 1

Author Comment

by:bshuler072699
ID: 1527498
This is great. You did an excelent job. It seems that there is one task left to the user (me). When querying a domain other than the current, I must get rights some how, otherwise it fails... any Ideas?
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Suggested Courses

661 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question