Home All Groups Group Topic Archive Search About

Simple LookupAccountName (working)

Author
15 Mar 2005 7:21 AM
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

Author
15 Mar 2005 10:37 AM
Crouchie1998
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
Author
1 Apr 2005 9:30 AM
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)
Author
1 Apr 2005 9:35 AM
GL
Well, I thank you Tomas.
Nice to see people taking the time to help others off their own back.

Adrian

Show quoteHide quote
"Tomas Nilsson" <nono9***@hotmail.com> wrote in message
news:17421296.0504010130.7def211c@posting.google.com...
> > 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)
Author
1 Apr 2005 11:39 AM
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
Author
1 Apr 2005 1:08 PM
Cor Ligthert
Thomas,

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

Cor