gtrainer
asked on
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(lDomainNameBufferSiz e - 1) & Chr(0)
iPENameUse = 0
lRET = LookupAccountName(sSystemN ame, sAccountName, 0, 0, sDomainName, lDomainNameBufferSize, iPENameUse)
Debug.Print
Debug.Print "------------------------- ---------- ---------- --"
Debug.Print "Return Code: " & Trim(CStr(lRET))
Debug.Print "System Name: " & StripNullAndSpaces(sSystem Name)
Debug.Print "Account Name: " & StripNullAndSpaces(sAccoun tName)
Debug.Print "Domain Name: " & StripNullAndSpaces(sDomain Name)
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
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(lDomainNameBufferSiz
iPENameUse = 0
lRET = LookupAccountName(sSystemN
Debug.Print
Debug.Print "-------------------------
Debug.Print "Return Code: " & Trim(CStr(lRET))
Debug.Print "System Name: " & StripNullAndSpaces(sSystem
Debug.Print "Account Name: " & StripNullAndSpaces(sAccoun
Debug.Print "Domain Name: " & StripNullAndSpaces(sDomain
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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
:)
Yes it does, but it allows you to allocate space for the name before actually saving it.
Glad it worked!
Yes it does, but it allows you to allocate space for the name before actually saving it.
Glad it worked!
ASKER
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!