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