Help Me - LookupAccountSid

G

Giovanni pepe

LookupAccountSid work only first time... Why?

This is my code:

'----------------------------------------------------------------------------------------------------------------------------

Private Declare Function ConvertSidToStringSid Lib "advapi32.dll" Alias
"ConvertSidToStringSidA" (ByVal pSID As IntPtr, ByRef strSID As IntPtr) As
Integer

Declare Function ConvertStringSidToSid Lib "advapi32.dll" Alias
"ConvertStringSidToSidA" (ByVal lpBuffer As String, ByRef lpVal As IntPtr)
As Boolean

Private Declare Sub LocalFree Lib "kernel32.dll" (ByVal hMemHandle As
Integer)

Private Declare Function LookupAccountSid Lib "advapi32.dll" Alias
"LookupAccountSidA" ( _
ByVal lpSystemName As String, _
ByVal pSID As Integer, _
ByVal pName As IntPtr, _
ByRef cchName As Integer, _
ByVal pRefDomain As IntPtr, _
ByRef cchRefDomain As Integer, _
ByRef SidType As Integer) As Integer

'----------------------------------------------------------------------------------------------------------------------------

Public Function AutorityNameBySid(ByVal StrSid As String) As String
Dim pSID As IntPtr
Dim pDomain As IntPtr
Dim ccDomain As Integer
Dim pUserID As IntPtr
Dim ccUserID As Integer
Dim bVal As Integer
Dim sidType As Integer
Dim DomainName As String
Dim AccountName As String
bVal = ConvertStringSidToSid(StrSid, pSID)
If (bVal <> 0) Then
bVal = LookupAccountSid(vbNullString, pSID.ToInt32, pUserID.Zero, ccUserID,
pDomain.Zero, ccDomain, sidType)
bVal = Err.LastDllError
If (bVal = 122) Then
pDomain = Marshal.AllocHGlobal(ccDomain)
pUserID = Marshal.AllocHGlobal(ccUserID)
bVal = LookupAccountSid(vbNullString, pSID.ToInt32, pUserID, ccUserID,
pDomain, ccDomain, sidType)
Dim strBld As New StringBuilder
If (bVal <> 0) Then
strBld.AppendFormat("Target : {0}{1}Domain : {2}{1}Sam Account Name:
{3}{1}", "Local System", vbCrLf, Marshal.PtrToStringAnsi(pDomain),
Marshal.PtrToStringAnsi(pUserID))
Dim cbPtr As Integer = GetLengthSid(pSID)
Dim j As Integer
Dim hexString As New StringBuilder
For j = 0 To cbPtr - 1
hexString.AppendFormat("{0:X2}", Marshal.ReadByte(pSID, j))
Next
DomainName = Marshal.PtrToStringAnsi(pDomain)
AccountName = Marshal.PtrToStringAnsi(pUserID)
'
' Clear Variables
'
Marshal.FreeHGlobal(pDomain)
Marshal.FreeHGlobal(pUserID)
pDomain = pDomain.Zero
pUserID = pUserID.Zero

LocalFree(pSID.ToInt32)
pSID = pSID.Zero
End If
End If
End If
Return DomainName & "\" & AccountName
End Function
 
J

JohnFol

You missed a line of codein the example

Declare Function GetLengthSid Lib "advapi32.dll" (ByRef pSid As Object) As
Integer
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top