From: vm on
We are in a multiple trusted domain environment and I am looking to determine
which domain the logged in user is logged into. People can have accounts with
the same SID in multiple domains. I have seen the post here about using the:
Environ$("USERDOMAIN") to extract the domain name. I am wondering if there is
another way to check. I can extract the domain the workstation is in, but
have had little luck finding something on finding the domain of the currently
logged in user on a workstation.

Thanks
From: Karl E. Peterson on
vm wrote:
> extract the domain the workstation is in, but have had little luck
> finding something on finding the domain of the currently logged in
> user on a workstation.

Hmmm, I never noticed the distinction before. The USER_INFO_3 structure returns the
user's logon server, which would presumably belong to the same domain as the user,
right?
--
Working Without a .NET?
http://classicvb.org/petition


From: Paul Clement on
On Thu, 16 Jun 2005 11:47:02 -0700, "vm" <vm(a)discussions.microsoft.com> wrote:

ý We are in a multiple trusted domain environment and I am looking to determine
ý which domain the logged in user is logged into. People can have accounts with
ý the same SID in multiple domains. I have seen the post here about using the:
ý Environ$("USERDOMAIN") to extract the domain name. I am wondering if there is
ý another way to check. I can extract the domain the workstation is in, but
ý have had little luck finding something on finding the domain of the currently
ý logged in user on a workstation.

You can also use ADSI:

Dim objSystemInfo As Object
Set objSystemInfo = CreateObject("ADSystemInfo")
MsgBox "The current domain is: " & objSystemInfo.DomainShortName
Set objSystemInfo = Nothing


Paul
~~~~
Microsoft MVP (Visual Basic)
From: Karl E. Peterson on
Paul Clement wrote:
> On Thu, 16 Jun 2005 11:47:02 -0700, "vm"
> <vm(a)discussions.microsoft.com> wrote:
>
> ý We are in a multiple trusted domain environment and I am looking to
> determine ý which domain the logged in user is logged into. People
> can have accounts with ý the same SID in multiple domains. I have
> seen the post here about using the: ý Environ$("USERDOMAIN") to
> extract the domain name. I am wondering if there is ý another way to
> check. I can extract the domain the workstation is in, but
> ý have had little luck finding something on finding the domain of the
> currently ý logged in user on a workstation.
>
> You can also use ADSI:
>
> Dim objSystemInfo As Object
> Set objSystemInfo = CreateObject("ADSystemInfo")
> MsgBox "The current domain is: " & objSystemInfo.DomainShortName
> Set objSystemInfo = Nothing

I don't know a lot about ADSI. Is that returning user or workstation domain?
SystemInfo sounds very workstation-y, eh?
--
Working Without a .NET?
http://classicvb.org/petition


From: Bob Butler on
"Karl E. Peterson" <karl(a)mvps.org> wrote in message
news:eEyyZLscFHA.456(a)TK2MSFTNGP09.phx.gbl

Private Const UNLEN = 256
Private Const TokenUser = 1
Private Const TOKEN_QUERY = 8
Private Declare Function GetCurrentProcess Lib "kernel32" () As Long
Private Declare Function GetCurrentThread Lib "kernel32" () As Long
Private Declare Function GetTokenInformation Lib "advapi32.dll" _
(ByVal TokenHandle As Long, ByVal TokenClass As Long, _
ByRef TokenInformation As Any, ByVal TokenInformationLength As Long, _
ByRef ReturnLength As Long) As Long
Private Declare Function OpenThreadToken Lib "advapi32.dll" _
(ByVal ThreadHandle As Long, ByVal DesiredAccess As Long, _
ByVal OpenAsSelf As Long, ByRef TokenHandle As Long) As Long
Private Declare Function OpenProcessToken Lib "advapi32.dll" _
(ByVal ProcessHandle As Long, ByVal DesiredAccess As Long, _
ByRef TokenHandle As Long) As Long
Private Declare Function CloseHandle Lib "kernel32" _
(ByVal hObject As Long) As Long
Private Declare Function LookupAccountSid Lib "advapi32.dll" _
Alias "LookupAccountSidA" (ByVal SystemName As String, _
ByRef PSid As Byte, ByVal UserName As String, _
ByRef cbUser As Long, ByVal DomainName As String, _
ByRef cbDomain As Long, ByRef puUse As Long) As Long

Sub Main()
MsgBox UserName, vbOKOnly, "User With Domain"
End Sub

Public Function UserName(Optional ByVal SystemName As String) As String
Dim bBuff(1 To 512) As Byte
Dim bSID() As Byte
Dim x As Long
Dim lSize As Long
Dim lToken As Long
If OpenThreadToken(GetCurrentThread(), TOKEN_QUERY, True, lToken) = 0 Then
' attempt to open the process token, since no thread token was found
If OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, lToken) = 0 Then
Exit Function
End If
lSize = UBound(bBuff)
If GetTokenInformation(lToken, TokenUser, bBuff(1), lSize, lSize) = 0 Then
Call CloseHandle(lToken)
Exit Function
End If
Call CloseHandle(lToken)
If lSize > 8 Then
' skip dword at front (?)
ReDim bSID(1 To lSize - 8)
For x = 9 To lSize
bSID(x - 8) = bBuff(x)
Next x
UserName = UserNameFromSID(bSID, SystemName)
End If
End Function

Public Function UserNameFromSID(ByRef Sid() As Byte, _
Optional SystemName As String) As String
Dim sUser As String
Dim sDomain As String
Dim lUSize As Long
Dim lDSize As Long
Dim lSNU As Long
Dim lResult As Long
sUser = String$(UNLEN + 1, vbNullChar): lUSize = UNLEN
sDomain = String$(UNLEN + 1, vbNullChar): lDSize = UNLEN
If Len(SystemName) > 0 Then
lResult = LookupAccountSid(SystemName, Sid(LBound(Sid)), _
sUser, lUSize, sDomain, lDSize, lSNU)
Else
lResult = LookupAccountSid(vbNullString, Sid(LBound(Sid)), _
sUser, lUSize, sDomain, lDSize, lSNU)
End If
If lResult <> 0 Then
sUser = Left$(sUser, InStr(1, sUser, vbNullChar) - 1)
sDomain = Left$(sDomain, InStr(1, sDomain, vbNullChar) - 1)
UserNameFromSID = sDomain & "\" & sUser
Else
UserNameFromSID = ""
End If
End Function



--
Reply to the group so all can participate
VB.Net: "Fool me once..."