|
-
Apr 19th, 2001, 08:49 AM
#1
Thread Starter
New Member
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.
http://www.weirdocult.co.uk 
-
Apr 19th, 2001, 02:00 PM
#2
Addicted Member
Try
Declare Function GetUserName Lib"advapi32.dll" Alias "GetUserNameA"_
(ByVal lpBuffer As String, nSize As Long) As Long
-
Apr 20th, 2001, 09:21 AM
#3
Thread Starter
New Member
Can't Get It To Work
Sorry but i can't seem to get this function to work got any examples of how it should be called?
http://www.weirdocult.co.uk 
-
Apr 20th, 2001, 10:55 AM
#4
Black Cat
This was probably originally from one of the vb api sites.
Code:
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
Josh
Get these: Mozilla Opera OpenBSD
I have books for sale: "MCSD in a Nutshell" and "VB Distributed Exam Cram" - PM me for details. Will also trade for a decent ATX Pentium 2 MB/CPU/RAM combo.
-
Jun 29th, 2001, 04:28 AM
#5
Lively Member
But the domain name ?
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
Yesterday, all my troubles seemed so far away...
Help, I need somebody, Help...
Now MCSD and still locking for intresting job in the south parts of Stockholm, Sweden.
-
Jun 29th, 2001, 06:16 AM
#6
Black Cat
The GetUserNameEx API call gets more info, including the full username (DOMAIN\username), but it only works with Windows 2000.
Josh
Get these: Mozilla Opera OpenBSD
I have books for sale: "MCSD in a Nutshell" and "VB Distributed Exam Cram" - PM me for details. Will also trade for a decent ATX Pentium 2 MB/CPU/RAM combo.
-
Jun 29th, 2001, 07:37 AM
#7
Lively Member
I am using NT so I can not use the GetUserNameEx API. Any other good ideas.
/AKA
Yesterday, all my troubles seemed so far away...
Help, I need somebody, Help...
Now MCSD and still locking for intresting job in the south parts of Stockholm, Sweden.
-
Jun 29th, 2001, 10:14 AM
#8
Try the LookupAccountName() API function.
-
Jul 2nd, 2001, 02:20 AM
#9
Lively Member
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.
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
Yesterday, all my troubles seemed so far away...
Help, I need somebody, Help...
Now MCSD and still locking for intresting job in the south parts of Stockholm, Sweden.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|