Call LookupAccountSidW on VB.net

  • Thread starter Microsoft Communities
  • Start date
M

Microsoft Communities

Hi all,

I call Win32 function: LookupAccountSidW on VB.NET:

Declare Unicode Function GetSIDStr Lib "advapi32.dll" Alias
"LookupAccountNameW" ( _
ByVal lpSystemName As String, ByVal lpAccountName As String _
, ByRef Sid As IntPtr, ByRef cbSid As Integer _
, ByRef ReferencedDomainName As IntPtr _
, ByRef cchReferencedDomainName As Integer _
, ByRef peUse As Integer) As Boolean

'Execute GetSIDStr function:
Private Shared Sub ProcessAPIFunction()
Dim lpSystemName As String = ""
Dim lpAccountName As String = "username"
Dim lpSID As IntPtr
Dim cbSID As Integer = 0
Dim lpRefDomainName As IntPtr
Dim cchRefDomainName As Integer = 255
Dim peUse As Integer = SID_NAME_USE.SidTypeUser
Dim ret As Boolean = GetSIDStr(lpSystemName, lpAccountName, lpSID,
cbSID, lpRefDomainName, cchRefDomainName, peUse)
End Sub

After execute GetSIDStr, ret's value is False, and I cannot get the return
value in lpSID, lpRefDomainName.parameter. I can get value in cbSID (e.g:
8), cchRefDomainName (e.g: 25)

Can anyone help me?
Thanks
 
A

Armin Zingler

Microsoft said:
Hi all,

I call Win32 function: LookupAccountSidW on VB.NET:

Declare Unicode Function GetSIDStr Lib "advapi32.dll" Alias
"LookupAccountNameW" ( _
ByVal lpSystemName As String, ByVal lpAccountName As String _
, ByRef Sid As IntPtr, ByRef cbSid As Integer _
, ByRef ReferencedDomainName As IntPtr _
, ByRef cchReferencedDomainName As Integer _
, ByRef peUse As Integer) As Boolean

'Execute GetSIDStr function:
Private Shared Sub ProcessAPIFunction()
Dim lpSystemName As String = ""
Dim lpAccountName As String = "username"
Dim lpSID As IntPtr
Dim cbSID As Integer = 0
Dim lpRefDomainName As IntPtr
Dim cchRefDomainName As Integer = 255
Dim peUse As Integer = SID_NAME_USE.SidTypeUser
Dim ret As Boolean = GetSIDStr(lpSystemName, lpAccountName, lpSID,
cbSID, lpRefDomainName, cchRefDomainName, peUse)
End Sub

After execute GetSIDStr, ret's value is False, and I cannot get the return
value in lpSID, lpRefDomainName.parameter. I can get value in cbSID (e.g:
8), cchRefDomainName (e.g: 25)

Can anyone help me?
Thanks


Isn't it "BYVAL Sid as Intptr"? If you want to get the SID, you have to
pass a pointer to a buffer. Currently you are passing a pointer to a pointer.
If cbSid is zero, you only receive the length (8) in cbSid. Afterwards, knowing
the required buffer size, you have to make another call to the function
passing a prepared buffer as the lpSID parameter. You can create such a
buffer using System.Runtime.InteropServices.Marshal.AllocHGlobal.
See also the documentation on the SID structure (linked in the
linked topic below) that states that it's a variable length structure
and how to handle it.

cbSid should be UInteger (but that's not a real problem). Same with
cchReferencedDomainName.

If you declare ReferencedDomainName As INTPTR, you also must declare it BYVAL.
You'd have to supply a buffer, too. Afterwards you'd have to use the Marshal
class to copy from the buffer into a String. But, probably declaring it As String
and using the MarshalAs-Attribute (if required) is easier.

I guess you have access to the documentation, otherwise:
http://msdn.microsoft.com/en-us/library/aa379159(VS.85).aspx
 
M

Microsoft Communities

Dear Armin,

I modify source code as below but it doesn't work. Is there any error?

Declare Unicode Function GetSIDStr Lib "advapi32.dll" Alias
"LookupAccountNameW" ( _
ByVal lpSystemName As String _
, ByVal lpAccountName As String _
, <MarshalAsAttribute(UnmanagedType.AsAny)> ByVal msg As Object _
, ByRef cbSid As UInteger _
, <MarshalAsAttribute(UnmanagedType.LPWStr)> ByVal msg As String
_
, ByRef cchReferencedDomainName As UInteger _
, ByRef peUse As Integer) As Boolean

'Execute GetSIDStr function:
Private Shared Sub ProcessAPIFunction()
Dim lpSystemName As String = ""
Dim lpAccountName As String = "tuyen-db"
Dim lpSID As String = ""
Dim cbSID As UInteger = 0
Dim lpRefDomainName As String = ""
Dim cchRefDomainName As UInteger = 0
Dim peUse As Integer = SID_NAME_USE.SidTypeUser
Dim ret As Boolean = GetSIDStr(lpSystemName, lpAccountName, lpSID,
cbSID, lpRefDomainName, cchRefDomainName, peUse)
End Sub

Return value is same as previous result. Please help me.

Thanks
 
A

Armin Zingler

Microsoft said:
Dear Armin,

I modify source code as below but it doesn't work. Is there any error?

Declare Unicode Function GetSIDStr Lib "advapi32.dll" Alias
"LookupAccountNameW" ( _
ByVal lpSystemName As String _
, ByVal lpAccountName As String _
, <MarshalAsAttribute(UnmanagedType.AsAny)> ByVal msg As Object _
, ByRef cbSid As UInteger _
, <MarshalAsAttribute(UnmanagedType.LPWStr)> ByVal msg As String
_
, ByRef cchReferencedDomainName As UInteger _
, ByRef peUse As Integer) As Boolean

'Execute GetSIDStr function:
Private Shared Sub ProcessAPIFunction()
Dim lpSystemName As String = ""
Dim lpAccountName As String = "tuyen-db"
Dim lpSID As String = ""
Dim cbSID As UInteger = 0
Dim lpRefDomainName As String = ""
Dim cchRefDomainName As UInteger = 0
Dim peUse As Integer = SID_NAME_USE.SidTypeUser
Dim ret As Boolean = GetSIDStr(lpSystemName, lpAccountName, lpSID,
cbSID, lpRefDomainName, cchRefDomainName, peUse)
End Sub

Return value is same as previous result. Please help me.

This is really a tough one for me. Currently I am not able to give an answer.
 

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