Results 1 to 14 of 14

Thread: Obtaining the current users NAME?

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Sep 2001
    Location
    UK
    Posts
    92

    Question Obtaining the current users NAME?

    Hi All,

    I have done much web searching (and searched these forums) but I can't seem to find the solution to this;

    I am wanting to resolve the current signed on users Name. I have the code to work out their ID number, but I want their actual name from this.

    Is this possible please? A friend has passed me the perl code equivelent and its only a few lines.

    Here is my current VB code;

    VB Code:
    1. Private Declare Function GetUserNameAPI Lib "advapi32.dll" Alias "GetUserNameA" (ByVal lpBuffer As String, _
    2.                                                             nSize As Long) As Long
    3.  
    4. Public Function GetUserName() As String
    5.     Dim sBuffer As String
    6.     Dim lSize As Long
    7.     Dim lRV As Long
    8.  
    9.     lSize = 255
    10.     sBuffer = String(lSize, &H0)
    11.      
    12.     lRV = GetUserNameAPI(sBuffer, lSize)
    13.    
    14.     If lRV <> 0 Then
    15.         GetUserName = Left(sBuffer, lSize - 1) ' -1 to remove trailing null character
    16.     Else
    17.         GetUserName = ""
    18.     End If
    19.    
    20. End Function
    21.  
    22.  
    23. Private Sub Command1_Click()
    24.  
    25. MsgBox GetUserName
    26.  
    27. End Sub

    Any ideas?

    Cheers!

    Paul.
    Paul

  2. #2
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333
    VB Code:
    1. Private Declare Function GetUserName Lib "advapi32.dll" Alias "GetUserNameA" (ByVal lpBuffer As String, nSize As Long) As Long
    2.  
    3. Private UserAccount As String
    4.  
    5. Private Sub GetUserAccount()
    6.  
    7.     Dim sBuffer As String
    8.     Dim lSize As Long
    9.    
    10.     sBuffer = Space$(255)
    11.     lSize = Len(sBuffer)
    12.     Call GetUserName(sBuffer, lSize)
    13.     If lSize > 0 Then
    14.         UserAccount = Left$(sBuffer, lSize - 1)
    15.     Else
    16.         UserAccount = vbNullString
    17.     End If

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Sep 2001
    Location
    UK
    Posts
    92
    Thanks Hack,

    This seems to only get the users ID though, not their actual name.

    Is it possible to get the name of the user at all?

    Cheers,

    Paul.
    Paul

  4. #4
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333
    Where is the actual name of a person stored?

  5. #5
    Fanatic Member Matt_T_hat's Avatar
    Join Date
    Dec 2001
    Location
    '76 Male Body Evil-Errors: 666
    Posts
    774
    Originally posted by Paul O
    Thanks Hack,

    This seems to only get the users ID though, not their actual name.

    Is it possible to get the name of the user at all?

    Cheers,

    Paul.
    Forgive me forbeing dense but surely you are dealing with a dataset or database? So surely you just lookup by number the user name???
    ?
    'What's this bit for anyway?
    For Jono

  6. #6
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333
    Matt: I think what Paul O is saying is that he wants the person name, not their account.

    My NT account is RGray24, but he doesn't want that returned. He wants my full first and last name. Thats fine, but my question is, other than my head, where, on a computer, is my full first and last name stored?

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Sep 2001
    Location
    UK
    Posts
    92
    Hi,

    Sorry for not making it very clear.

    Hack is correct -- I am trying to locate the users sign-on from Windows NT. The user signs on using a log-on ID. From this, I want to know what the users name is.

    Here is an example of perl code if it helps?

    Win32API::Net::GetDCName("",Win32:omainName(),$DC);
    Win32API::Net::UserGetInfo($DC,Win32::LoginName(),10,\%USER);

    Returns a hash containing all the user's attributes (%USER)


    Cheers,

    Paul.
    Paul

  8. #8
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333
    Oh...Ok. I get what you mean now (I think)

    When an NT account is created, there is a spot on the Add User form for real name (not sure if that is what is it called), and thats what you want retrieved, correct?

    Even if I whipped something together, I couldn't test it here. Real names are not stored with Ids at my company. This is a hard and fast policy. Even the property page from Outlook doesn't have real names on it. Another hard and fast policy.

    Sorry...

  9. #9
    Fanatic Member Matt_T_hat's Avatar
    Join Date
    Dec 2001
    Location
    '76 Male Body Evil-Errors: 666
    Posts
    774
    Originally posted by Paul O
    Sorry for not making it very clear.

    Hack is correct -- I am trying to locate the users sign-on from Windows NT. The user signs on using a log-on ID. From this, I want to know what the users name is.
    Oh I C - NT question. Sorry.

    Trying to remember location c:\..... erm oh bugger

    Sorry I'll P**s of now.
    ?
    'What's this bit for anyway?
    For Jono

  10. #10

    Thread Starter
    Lively Member
    Join Date
    Sep 2001
    Location
    UK
    Posts
    92
    Not to worry hack. Thanks for trying.

    Anyone else got any ideas?

    Cheers,
    Paul

  11. #11
    Fanatic Member Matt_T_hat's Avatar
    Join Date
    Dec 2001
    Location
    '76 Male Body Evil-Errors: 666
    Posts
    774
    Under 2000 (bult on NT tech) users info is stored in a folder at the C: root called documents and settings (or similer)

    inside should be a list of users created. Admins and the user are the only ones with access to the details.

    I beleave the file is called NTUSER.DAT

    Does that help?
    ?
    'What's this bit for anyway?
    For Jono

  12. #12
    Frenzied Member
    Join Date
    Jan 2001
    Location
    Newbury, UK
    Posts
    1,878
    Is it in the Registry anywhere??

    My full name is under:
    HKEY_CURRENT_USER\Software\Microsoft\MS Setup (ACME)\User Info\DefName

    (I am currently logged into an NT4 Domain, on a Win2000 PC).

  13. #13

    Thread Starter
    Lively Member
    Join Date
    Sep 2001
    Location
    UK
    Posts
    92
    Thanks for the suggestions.

    I have had a look in the registry, but to no avail here and I am unable to decipher the .dat file

    However! I have gone for a cheap and chearful method! As it is for an application which links to a database, I have created a new table with all the current user IDs in, and added the names to these.

    I then use the function above to check the current user ID and match it to a user name.

    A bit pants, but it works rather splendidly!

    Thanks for your help everyone!



    Paul.
    Paul

  14. #14
    Frenzied Member oh1mie's Avatar
    Join Date
    Sep 2001
    Location
    Finland
    Posts
    1,043
    Here is all tools what you need

    VB Code:
    1. Private Sub Command1_Click()
    2.    
    3.     Dim Temp As String
    4.     subGetComputerUsersInfo
    5.     Temp = ComputerUsers.username
    6.     'ect...
    7.    
    8. End Sub
    9.  
    10. ' Module Code
    11. Option Explicit
    12.  
    13. 'by oh1mie
    14.  
    15. Private Const ERROR_SUCCESS As Long = 0&
    16. Private Const MAX_COMPUTERNAME As Long = 15
    17. Private Const MAX_USERNAME As Long = 256
    18. Private Const FILTER_NORMAL_ACCOUNT  As Long = &H2
    19.  
    20. Private Type USER_INFO_10
    21.    usr10_name          As Long
    22.    usr10_comment       As Long
    23.    usr10_usr_comment   As Long
    24.    usr10_full_name     As Long
    25. End Type
    26.  
    27. Private Type USER_INFO
    28.    name          As String
    29.    fullname      As String
    30.    comment       As String
    31.    usrcomment    As String
    32. End Type
    33.  
    34. Private Type ComputerUsers
    35.    username                    As String
    36.    Computername                As String
    37.    usrname                     As String
    38.    usrfullname                 As String
    39.    usrcomment                  As String
    40.    usrusrcomment               As String
    41.    USER_INFO()                 As USER_INFO
    42. End Type
    43.  
    44. Private Declare Function GetUserName Lib "advapi32.dll" Alias "GetUserNameA" _
    45.         (ByVal lpBuffer As String, nSize As Long) As Long
    46.  
    47. Private Declare Function GetComputerName Lib "kernel32" Alias "GetComputerNameA" _
    48.         (ByVal lpBuffer As String, nSize As Long) As Long
    49.  
    50. Private Declare Function NetUserGetInfo Lib "Netapi32" _
    51.         (lpServer As Byte, username As Byte, _
    52.         ByVal level As Long, lpBuffer As Long) As Long
    53.  
    54. Private Declare Sub CopyMemory Lib "kernel32" _
    55.         Alias "RtlMoveMemory" (Destination As Any, Source As Any, _
    56.         ByVal Length As Long)
    57.  
    58. Private Declare Function NetApiBufferFree Lib "Netapi32" _
    59.         (ByVal Buffer As Long) As Long
    60.  
    61. Private Declare Function lstrlenW Lib "kernel32" _
    62.        (ByVal lpString As Long) As Long
    63.  
    64. Private Declare Function NetUserEnum Lib "Netapi32" _
    65.        (servername As Byte, ByVal level As Long, _
    66.        ByVal filter As Long, buff As Long, _
    67.        ByVal buffsize As Long, entriesread As Long, _
    68.        totalentries As Long, resumehandle As Long) As Long
    69.  
    70. Public ComputerUsers  As ComputerUsers
    71. Public Sub subGetComputerUsersInfo()
    72.  
    73.    On Local Error Resume Next
    74.  
    75.    Dim tmp  As String
    76.    Dim LCID As Long
    77.    
    78.    With ComputerUsers
    79.    .username = fcnrgbGetUserName()
    80.    .Computername = fcnrgbGetComputerName()
    81.    
    82.    Dim usr As USER_INFO
    83.    Dim bUsername() As Byte
    84.    Dim bServername() As Byte
    85.    If Len(.username) And Len(.Computername) Then
    86.       bUsername = .username & Chr(0)
    87.       tmp = .Computername
    88.       If Len(tmp) Then
    89.          If InStr(tmp, "\\") Then
    90.                bServername = tmp & Chr(0)
    91.          Else: bServername = "\\" & tmp & Chr(0)
    92.          End If
    93.       End If
    94.       usr = fcnGetUserNetworkInfo(bServername(), bUsername())
    95.       .usrname = usr.name
    96.       .usrfullname = usr.fullname
    97.       .usrcomment = usr.comment
    98.       .usrusrcomment = usr.usrcomment
    99.    End If
    100.  
    101.    Call fcnGetUserEnumInfo(bServername())
    102.  
    103.    End With
    104.    
    105. End Sub
    106. Private Function fcnrgbGetUserName() As String
    107.  
    108.    On Local Error Resume Next
    109.  
    110.   'return the name of the user
    111.    Dim tmp As String
    112.    
    113.    tmp = Space(MAX_USERNAME)
    114.    
    115.    If GetUserName(tmp, Len(tmp)) Then
    116.       fcnrgbGetUserName = fcnTrimNull(tmp)
    117.    End If
    118.  
    119. End Function
    120. Private Function fcnGetUserNetworkInfo(bServername() As Byte, _
    121.                                     bUsername() As Byte) As USER_INFO
    122.    On Local Error Resume Next
    123.    
    124.    Dim usrapi As USER_INFO_10
    125.    Dim buff As Long
    126.    
    127.    If NetUserGetInfo(bServername(0), bUsername(0), 10, buff) = ERROR_SUCCESS Then
    128.      
    129.      'copy the data from buff into the
    130.      'API user_10 structure
    131.       CopyMemory usrapi, ByVal buff, Len(usrapi)
    132.      
    133.      'extract each member and return
    134.      'as members of the UDT
    135.       fcnGetUserNetworkInfo.name = fcnGetPointerToByteStringW(usrapi.usr10_name)
    136.       fcnGetUserNetworkInfo.fullname = fcnGetPointerToByteStringW(usrapi.usr10_full_name)
    137.       fcnGetUserNetworkInfo.comment = fcnGetPointerToByteStringW(usrapi.usr10_comment)
    138.       fcnGetUserNetworkInfo.usrcomment = fcnGetPointerToByteStringW(usrapi.usr10_usr_comment)
    139.    
    140.       NetApiBufferFree buff
    141.    
    142.    End If
    143.    
    144. End Function
    145. Private Function fcnGetPointerToByteStringW(lpString As Long) As String
    146.  
    147.    On Local Error Resume Next
    148.  
    149.    Dim buff() As Byte
    150.    Dim nSize As Long
    151.    
    152.    If lpString Then
    153.    
    154.      'its Unicode, so mult. by 2
    155.       nSize = lstrlenW(lpString) * 2
    156.      
    157.       If nSize Then
    158.          ReDim buff(0 To (nSize - 1)) As Byte
    159.          CopyMemory buff(0), ByVal lpString, nSize
    160.          fcnGetPointerToByteStringW = buff
    161.      End If
    162.      
    163.    End If
    164.    
    165. End Function
    166. Private Function fcnGetUserEnumInfo(bServername() As Byte)
    167.    
    168.    On Local Error Resume Next
    169.    
    170.    Dim users()      As Long
    171.    Dim buff         As Long
    172.    Dim buffsize     As Long
    173.    Dim entriesread  As Long
    174.    Dim totalentries As Long
    175.    Dim cnt          As Integer
    176.    Dim StrX(10)     As String
    177.    Dim tmp          As String
    178.    Dim usr          As USER_INFO
    179.    Dim bUsername()  As Byte
    180.    
    181.    buffsize = 255
    182.    tmp = ComputerUsers.Computername
    183.    If Len(tmp) Then
    184.       If InStr(tmp, "\\") Then
    185.            bServername = tmp & Chr(0)
    186.       Else: bServername = "\\" & tmp & Chr(0)
    187.       End If
    188.    End If
    189.    
    190.    If NetUserEnum(bServername(0), 0, _
    191.                    FILTER_NORMAL_ACCOUNT, _
    192.                    buff, buffsize, _
    193.                    entriesread, _
    194.                    totalentries, 0&) = ERROR_SUCCESS Then
    195.    
    196.       ReDim users(0 To entriesread - 1) As Long
    197.       CopyMemory users(0), ByVal buff, entriesread * 4
    198.      
    199.       For cnt = 0 To entriesread - 1
    200.          StrX(cnt) = fcnGetPointerToByteStringW(users(cnt))
    201.          bUsername = StrX(cnt) & Chr(0)
    202.          usr = fcnGetUserNetworkInfo(bServername(), bUsername())
    203.          ReDim Preserve ComputerUsers.USER_INFO(cnt)
    204.          ComputerUsers.USER_INFO(cnt).comment = usr.comment
    205.          ComputerUsers.USER_INFO(cnt).fullname = usr.fullname
    206.          ComputerUsers.USER_INFO(cnt).name = usr.name
    207.          ComputerUsers.USER_INFO(cnt).usrcomment = usr.usrcomment
    208.       Next cnt
    209.      
    210.       NetApiBufferFree buff
    211.    
    212.    End If
    213.  
    214. End Function
    215. Private Function fcnrgbGetComputerName() As String
    216.    
    217.    On Local Error Resume Next
    218.  
    219.   'return the name of the computer
    220.    Dim tmp As String
    221.    
    222.    tmp = Space(MAX_COMPUTERNAME + 1)
    223.    If GetComputerName(tmp, Len(tmp)) <> 0 Then
    224.       fcnrgbGetComputerName = fcnTrimNull(tmp)
    225.    End If
    226.    
    227. End Function
    228. Private Function fcnTrimNull(Item As String)
    229.  
    230.    On Local Error Resume Next
    231.  
    232.    Dim pos As Integer
    233.    
    234.    pos = InStr(Item, Chr(0))
    235.    
    236.    If pos Then
    237.          fcnTrimNull = Left(Item, pos - 1)
    238.    Else: fcnTrimNull = Item
    239.    End If
    240.    
    241. End Function
    oh1mie/Vic


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