How do I resolve a domain name under which a user is logged in?

Please pardon my naivete, this is my first time using the Security API.

I am writing an application that must make a trusted connection to SQL Server.  Therefore, my app needs to pass a user id thus: [Domain name]\[User Name].  The app is running on NT Workstation 4.0  I can resolve the User name with the GetUserName API call, but when I use LookupAccountName to try to get the domain name, the call fails.  Am I using the right call?  If so, what is wrong with the code below that is causing LookupAccountName to fail?  200 points to (s)he who wields a clue.

Private Sub Command1_Click()
Const lBUFFER_SIZE = 254
Dim sSystemName As String
Dim sAccountName As String
Dim lSID As Long
Dim lSIDSize As Long
Dim sDomainName As String
Dim lDomainNameBufferSize As Long
Dim iPENameUse As Integer
Dim lRET As Long

    sSystemName = Chr(0)
    sAccountName = "gtrainer" & Chr(0)
    lSID = 0
    lSIDSize = lBUFFER_SIZE
    lDomainNameBufferSize = lBUFFER_SIZE
    sDomainName = Space(lDomainNameBufferSize - 1) & Chr(0)
    iPENameUse = 0
   
    lRET = LookupAccountName(sSystemName, sAccountName, 0, 0, sDomainName, lDomainNameBufferSize, iPENameUse)
    Debug.Print
    Debug.Print "-----------------------------------------------"
    Debug.Print "Return Code: " & Trim(CStr(lRET))
    Debug.Print "System Name: " & StripNullAndSpaces(sSystemName)
    Debug.Print "Account Name: " & StripNullAndSpaces(sAccountName)
    Debug.Print "Domain Name: " & StripNullAndSpaces(sDomainName)
    Debug.Print "-----------------------------------------------"
    Debug.Print

End Sub

Private Function StripNullAndSpaces(sInput As String) As String
Dim iIndex As Integer
Dim sOutput As String

    sOutput = ""
   
    For iIndex = 1 To Len(sInput)
        If Mid(sInput, iIndex, 1) = Chr(0) Then
            Exit For
        Else
            sOutput = sOutput & Mid(sInput, iIndex, 1)
        End If
    Next iIndex
   
    StripNullAndSpaces = Trim(sOutput)
   
End Function
gtrainerAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Erick37Commented:
From MS:
http://support.microsoft.com/support/kb/articles/q194/7/57.asp

' In order to use the SID
' of the current user account, call the LookupAccountName API
' twice. The first time is to get the required sizes of the SID
' and the DomainName string. The second call is to actually get
' the desired information.

lResult = LookupAccountName(vbNullString, sUserName, _
    bUserSid(0), 255, sDomainName, lDomainNameLength, _
    lSIDType)

' Now set the sDomainName string buffer to its proper size before
' calling the API again.
sDomainName = Space(lDomainNameLength)

' Call the LookupAccountName again to get the actual SID for user.
lResult = LookupAccountName(vbNullString, sUserName, _
    bUserSid(0), 255, sDomainName, lDomainNameLength, _
    lSIDType)
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
gtrainerAuthor Commented:
It worked beautifully!  Thanks for your help!  I have to ask this rhetorical question:

Why on earth does the first call to LookupAccountName give you the length of the domain name, but not the domain name?  Wouldn't it have to know the domain name in order to tell you long it was?  That's like introducing yourself by saying, "Nice to meet you, Jim!  My name has 7 letters in it!"  What an great way to abruptly end a conversation.

Thanks again for your help!


0
Erick37Commented:
:)
Yes it does, but it allows you to allocate space for the name before actually saving it.

Glad it worked!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.