PtrToStringAnsi returns error while using LookupAccountSid functi

G

Guest

Hi,
i want to enumerate the name of the user from a SID string. So I use the
LConvertStringookupAccountSID function.

It looks good up to the converting the name pointer to a string. I receive a
Null reference error. Why????

Here my code:

Declare Function ConvertSidToStringSid Lib "advapi32.dll" Alias
"ConvertSidToStringSidA" ( ByVal pSID() As Byte, ByRef strSID As IntPtr) As
Integer
Declare Function LookupAccountSid Lib "advapi32.dll" Alias
"LookupAccountSidA" ( _
ByVal systemName As String, _
ByVal psid As IntPtr, _
ByRef accountName As IntPtr, _
ByRef cbAccount As Integer, _
ByRef domainName As IntPtr, _
ByRef cbDomainName As Integer, _
ByRef use As Integer) As Integer

Public Shared Function ConvertSidToName(ByVal PC As String, ByVal StringSID
As String) As String
Dim Ret, pNameLength, pDomainLength, pUse As Integer
Dim pSID, pName, pDomain As IntPtr
Dim UserName As String
Dim DomainName As String
'Converting a String SID to a SID object
If ConvertStringSidToSid(StringSID, pSID) <> 0 Then
'first time lookup to enumerate the size of the Name and the domain
Ret = LookupAccountSid(PC, pSID, pName.Zero, pNameLength,
pDomain.Zero, pDomainLength, pUse)
'do it
Ret = LookupAccountSid(PC, pSID, pName, pNameLength,
pDomain, pDomainLength, pUse)
If Ret = 0 Then

Console.WriteLine(Runtime.InteropServices.Marshal.GetLastWin32Error)
Return "<unknown account>"
Else
' -> At this point I receive the Null Reference error
UserName =
Runtime.InteropServices.Marshal.PtrToStringAnsi(pName)
DomainName =
Runtime.InteropServices.Marshal.PtrToStringAnsi(pDomain)
Return DomainName & "\" & UserName
End If
Else

Console.WriteLine(Runtime.InteropServices.Marshal.GetLastWin32Error)
Return "<invalid SID>"
End If
End Function

Has anyone a idea what's wrong?

greetings AndyL
 
M

Mattias Sjögren

Declare Function LookupAccountSid Lib "advapi32.dll" Alias
"LookupAccountSidA" ( _
ByVal systemName As String, _
ByVal psid As IntPtr, _
ByRef accountName As IntPtr, _
ByRef cbAccount As Integer, _
ByRef domainName As IntPtr, _
ByRef cbDomainName As Integer, _
ByRef use As Integer) As Integer

Try it like this instead

Declare Auto Function LookupAccountSid Lib "advapi32.dll" ( _
ByVal systemName As String, _
ByVal psid As IntPtr, _
ByVal accountName As StirngBuilder, _
ByRef cbAccount As Integer, _
ByVal domainName As StrignBuilder, _
ByRef cbDomainName As Integer, _
ByRef use As Integer) As Boolean

And don't forget to free the SID memory ConvertStringSidToSid returns
with Marshal.FreeHGlobal once you're done.


Mattias
 
G

Guest

Hi Matthias,

thanks for your answer. Maybe I'm wrong If i declare the LookupAccountSID as
you say i receive a Null reference error ??????

Here is my new code
If ConvertStringSidToSid(StringSID, pSID) <> 0 Then
Ret = LookupAccountSid(PC, pSID, Nothing, pNameLength, Nothing,
pDomainLength, pUse)
pName = New Text.StringBuilder(pNameLength)
pName = New Text.StringBuilder(pDomainLength)
Ret = LookupAccountSid(PC, pSID, pName, pNameLength, pDomain,
pDomainLength, pUse) '<-- At this point I receive the null reference error
but why??
end if
 
G

Guest

AndyL said:
Hi Matthias,

thanks for your answer. Maybe I'm wrong If i declare the LookupAccountSID as
you say i receive a Null reference error ??????

Here is my new code
If ConvertStringSidToSid(StringSID, pSID) <> 0 Then
Ret = LookupAccountSid(PC, pSID, Nothing, pNameLength, Nothing,
pDomainLength, pUse)
pName = New Text.StringBuilder(pNameLength)
pName = New Text.StringBuilder(pDomainLength)
Ret = LookupAccountSid(PC, pSID, pName, pNameLength, pDomain,
pDomainLength, pUse) '<-- At this point I receive the null reference error
but why??
end if
 
G

Guest

Hi Matthias,

you are right.
The error war in my declaration. I've declared the AccountName and the
DomainName parameters as ByRef instead of ByVal
Now it works fine

Thanks

Andy L
 

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