Click to See Complete Forum and Search --> : Who's Logged On?
grandhigh
Apr 19th, 2001, 08:49 AM
On a NT Domain is there an API call That will tell me the NT username of the user logged on to a particular machine. :(
chrisf
Apr 19th, 2001, 02:00 PM
Try
Declare Function GetUserName Lib"advapi32.dll" Alias "GetUserNameA"_
(ByVal lpBuffer As String, nSize As Long) As Long
grandhigh
Apr 20th, 2001, 09:21 AM
Sorry but i can't seem to get this function to work got any examples of how it should be called? :confused:
JoshT
Apr 20th, 2001, 10:55 AM
This was probably originally from one of the vb api sites.
Private Sub Command1_Click()
' Display the name of the user currently logged on.
Dim username As String ' receives name of the user
Dim slength As Long ' length of the string
Dim retval As Long ' return value
' Create room in the buffer to receive the returned string.
username = Space(255) ' room for 255 characters
slength = 255 ' initialize the size of the string
' Get the user's name and display it.
retval = GetUserName(username, slength) ' slength is now the length of the returned string
username = Left(username, slength - 1) ' extract the returned info from the buffer
' (We subtracted one because we don't want the null character in the trimmed string.)
Text1.Text = username
End Sub
AKA
Jun 29th, 2001, 04:28 AM
Hi !
Can you get the domain name also ? I was thinking of using it as security in my program and I want to make sure that noone makes a accont on local machin with same name as in the domain and therefore get the domain users rights.
/AKA
JoshT
Jun 29th, 2001, 06:16 AM
The GetUserNameEx API call gets more info, including the full username (DOMAIN\username), but it only works with Windows 2000.
AKA
Jun 29th, 2001, 07:37 AM
I am using NT so I can not use the GetUserNameEx API. Any other good ideas.
/AKA
Megatron
Jun 29th, 2001, 10:14 AM
Try the LookupAccountName() API function.
AKA
Jul 2nd, 2001, 02:20 AM
Hi !
I tried LookUpAccountName last week and never got it working. Do you have an example ? I am now trying to rewrite some C code using LookUpAccountSid. I think that I have got all parts working except the call to LookUpAccountSid.
Note, I dont include the API declarations in the code.
Private Sub ShowUserName()
Const TOKEN_QUERY = &H8
Dim retVal As Long
Dim tUser As TOKEN_USER
Dim Leng As Long
Dim LengNeeded As Long
Dim strAccountName As String * 1000
Dim strDomainName As String * 1000
Dim lngAccountLength As Long
Dim lngDomainLength As Long
Dim lngSIDTypeReturned As Integer
Dim hdlProcessHandle As Long
Dim hdlToken As Long
'Set the error code of the last thread to zero using the
'SetLast Error function. Do this so that the GetLastError
'function does not return a value other than zero for no
'apparent reason.
SetLastError 0
'Use the GetCurrentProcess function to set the hdlProcessHandle
'variable.
hdlProcessHandle = GetCurrentProcess()
If GetLastError <> 0 Then
MsgBox "GetCurrentProcess error==" & GetLastError
End If
OpenProcessToken hdlProcessHandle, _
(TOKEN_QUERY), hdlToken
If GetLastError <> 0 Then
MsgBox "OpenProcessToken error==" & GetLastError
End If
Leng = 1
ReDim Token(0)
' this should fail, because the buffer is to small
retVal = GetTokenInformation(hdlToken, TokenUser, Token(0), Leng, LengNeeded)
If retVal = 0 Then
Leng = LengNeeded
ReDim Token(Leng - 1)
' now it should succeed
retVal = GetTokenInformation(hdlToken, TokenUser, Token(0), Leng, LengNeeded)
If retVal <> 0 Then
' copy the first 8 bytes to the TOKEN_USER structure.
Call CopyMem(tUser, Token(0), Len(tUser))
' we have a pointer to the sid in the TOKEN_USER
' first check is the sid is valid
retVal = IsValidSid(tUser.User.Sid)
If retVal = 0 Then
MsgBox "Sid is not valid"
End If
lngAccountLength = 1000
lngDomainLength = 1000
lngSIDTypeReturned = 1
retVal = LookupAccountSid(vbNullChar, tUser.User.Sid, strAccountName, lngAccountLength, strDomainName, lngDomainLength, lngSIDTypeReturned)
If retVal = 0 Then
MsgBox "LookUpAccountSid faild"
End If
End If
End If
End Sub
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.