Results 1 to 9 of 9

Thread: Who's Logged On?

  1. #1

    Thread Starter
    New Member
    Join Date
    Apr 2001
    Location
    A Very Weird Place
    Posts
    15

    Unhappy

    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

  2. #2
    Addicted Member
    Join Date
    Mar 2001
    Posts
    157
    Try

    Declare Function GetUserName Lib"advapi32.dll" Alias "GetUserNameA"_
    (ByVal lpBuffer As String, nSize As Long) As Long

  3. #3

    Thread Starter
    New Member
    Join Date
    Apr 2001
    Location
    A Very Weird Place
    Posts
    15

    Unhappy 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

  4. #4
    Black Cat JoshT's Avatar
    Join Date
    Nov 2000
    Location
    WNY, USA
    Posts
    4,032
    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.

  5. #5
    Lively Member
    Join Date
    Jul 2000
    Location
    Stockholm, Sweden
    Posts
    83

    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.

  6. #6
    Black Cat JoshT's Avatar
    Join Date
    Nov 2000
    Location
    WNY, USA
    Posts
    4,032
    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.

  7. #7
    Lively Member
    Join Date
    Jul 2000
    Location
    Stockholm, Sweden
    Posts
    83
    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.

  8. #8
    Megatron
    Guest
    Try the LookupAccountName() API function.

  9. #9
    Lively Member
    Join Date
    Jul 2000
    Location
    Stockholm, Sweden
    Posts
    83
    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
  •  



Click Here to Expand Forum to Full Width