Simple LookupAccountName (working)

T

Tomas Nilsson

Working code below (paying back to the groups :)

/Tomas


Class SidFunctions

Enum SID_NAME_USE
SidTypeUser = 1
SidTypeGroup
SidTypeDomain
SidTypeAlias
SidTypeWellKnownGroup
SidTypeDeletedAccount
SidTypeInvalid
SidTypeUnknown
SidTypeComputer
End Enum 'SID_NAME_USE

Declare Function LookupAccountName Lib "advapi32.dll" Alias
"LookupAccountNameA" (ByVal lpSystemName As String, ByVal
lpAccountName As String,
<System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.LPArray)>
ByVal Sid() As Byte, ByRef cbSid As System.UInt32, ByVal
ReferencedDomainName As System.Text.StringBuilder, ByRef
cchReferencedDomainName As System.UInt32, ByRef peUse As SID_NAME_USE)
As Boolean

Public Function ConvertUsernameToSidString(ByVal i_AccountName As
String, Optional ByVal i_Domain As String = "") As String

'*********************************************************************
'* Source of example:
http://pinvoke.net/default.aspx/advapi32.LookupAccountName
'*********************************************************************

Dim resultAsByte As Byte() = Nothing
Dim result As String
Dim m_resultAsByteCapacity As System.UInt32 =
UInt32.Parse("0")
Dim m_StringBuilder As New System.Text.StringBuilder
Dim m_StringBuilderCapacity As System.UInt32 =
UInt32.Parse(m_StringBuilder.Capacity)
Dim m_SID_NAME_USE As SID_NAME_USE

'*********************************************************************
'* Lookup
'*********************************************************************

Call LookupAccountName(i_Domain, i_AccountName, resultAsByte,
m_resultAsByteCapacity, m_StringBuilder, m_StringBuilderCapacity,
m_SID_NAME_USE)

'*********************************************************************
'* We should get ERROR_INSUFFICIENT_BUFFER
'*********************************************************************

If System.Runtime.InteropServices.Marshal.GetLastWin32Error =
122 Then 'ERROR_INSUFFICIENT_BUFFER

'*********************************************************************
'* Allocate the exact amount of needed space
'*********************************************************************

resultAsByte = New
Byte(CLng(m_resultAsByteCapacity.ToString)) {}
m_StringBuilder.EnsureCapacity(CLng(m_StringBuilderCapacity.ToString))

'*********************************************************************
'* And fetch the information
'*********************************************************************

If LookupAccountName(i_Domain, i_AccountName,
resultAsByte, m_resultAsByteCapacity, m_StringBuilder,
m_StringBuilderCapacity, m_SID_NAME_USE) = True Then

'*********************************************************************
'* Store result (all OK)
'*********************************************************************

result = BitConverter.ToString(resultAsByte)
'Call ConvertSidFromByteToString(resultAsByte, result)
Debug.WriteLine("Type=" & m_SID_NAME_USE.ToString &
vbTab & "SID=" & result)

End If

End If

Return result

End Function

End Class
 
G

Guest

You're a VB 6 coder I see & not a VB.NET one.

You can look up account name in a few lines of code using WMI, which means
there is no need to use the API in this instance.

Also, I see you are using the 'Call' Keyword. Call was only used in VB 6
when you wanted to enclose everthing in brackets '()'. Without that keyword,
you just miss out the opening & end brackets.

You could also shorten your code by using 2 import statements that I have
noticed:

Imports System.Runtime.InteropServices ' For MarshalAs
Imports System.Text ' For StringBuilder

Lastly, why are you posting a solution here when no one has asked for it?
Rather pointless to me I think

Nice try though - 3/10
 
C

Cor Ligthert

Crouchie,

I disagree and agree with you.

In past I have given messages as you do now, because then it was often more
that you were looking to Win32 coding than VBNet coding in this newsgroup.
(And really very much more than Thomas does now).

However the used code is basicly VBNet where it seems (I did not any check
about that however you tell) if not is tried to use Net code however the
Win32 API's, while there are such nice pages on MSDN for that.

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dndotnet/html/win32map.asp

(I changed such a Win32 thing yesterday completly what was about the
shutdowntime, it is in this newsgroup somewhere)

However it is not VB6 code, using Win32 API's can in almost every windows
programming language.

Cor
 
T

Tomas Nilsson

You're a VB 6 coder I see & not a VB.NET one.

Please, don't try to confuse people with bad replys. The code is
indeed .NET, using some techniques from VB6.
You can look up account name in a few lines of code using WMI, which means
there is no need to use the API in this instance.

No, WMI doesn't work in all cases. For example, I belive fetching a
local account on a remote machine doesn't work. Also, there is quite
some examples using WMI, and few using the API.
Also, I see you are using the 'Call' Keyword. Call was only used in VB 6
when you wanted to enclose everthing in brackets '()'. Without that keyword,
you just miss out the opening & end brackets.

Different programmer styles. I like to use call, so let me. I belive
it doesn't change the generated MSIL.
You could also shorten your code by using 2 import statements that I have
noticed:
Imports System.Runtime.InteropServices ' For MarshalAs
Imports System.Text ' For StringBuilder

This is basically the main reason why most examples on the Internet
fails to compile. Coding without imports clearly shows where the call
is originating (which leads to better developers), and it ensures
working code even if you forget to post the imports (which is very
important).
Lastly, why are you posting a solution here when no one has asked for it?

As I wrote, "Paying back to the groups". Researching for the solution
took me a while, so I thought I should save some time for someone
else.
Rather pointless to me I think

Maybe, but I belive *your* reply was indeed pointless. It didn't give
anything to the group, except information of how bad developer you
think I am (which the world probably cares less about), and the
information about the code being VB6 (which was faulty information).
Now, in the future, please stop wasting my and other peoples time by
replying with nonsense information.

/Tomas - Who hesitated a lot to reply at all.. (Don't feed the trolls)
 
G

GL

Well, I thank you Tomas.
Nice to see people taking the time to help others off their own back.

Adrian
 
C

Cor Ligthert

Thomas,
Please, don't try to confuse people with bad replys. The code is
indeed .NET, using some techniques from VB6.
In my opinion are as well confusing people.

Your code is not .Net it is VB.Net where partially is used Net, however not
everywhere.

With what I don't say it is wrong when you have no .Net alternative.

Just my thought,

Cor
 
C

Cor Ligthert

Thomas,

However nobody thanked you for sharing this with us, so with this message.
Thanks,

Cor
 

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