Results 1 to 6 of 6

Thread: GetUserName And GetComputerName On Win2K

  1. #1

    Thread Starter
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    GetUserName And GetComputerName On Win2K

    Color me dumbfounded!

    I just got (yesterday) a Windows 2000 workstation because my company will be converting from Win98 to 2000 later this year. I now have two PCs. My Win98 box is my development box. I have nine applications currently in production, and one of the things I need to do is ensure that all nine apps will work on Windows 2000.

    Gknuth posted a question asking how to get the logged in UserName, and I responded. He came back and said that the GetUserName API didn't work on Win2K. I said to myself "yeah, right…what did you do wrong?" I went to my Win2K box, opened a new project, and pasted the GetUserName code onto a form, ran it, and it came up blank. To say I was shocked is an understatement, and here is why.

    I ran each of the nine apps I have in production. All nine use GetUserName and GetComputerName. In all instances, the logged in UserAccount (mine) was properly returned and the ComputerName (mine) was also properly returned. Each of the applications were developed and compiled on a Win98 box, and ported to Windows 2000.

    So, it seems that if an application is developed on Win98, and ported to Windows 2000 these two APIs work, but if you try to use them in a New Project started on a Windows 2000 box, they don't.

    Does anyone know why?

  2. #2
    Black Cat JoshT's Avatar
    Join Date
    Nov 2000
    Location
    WNY, USA
    Posts
    4,032
    Hack, this works fine on my W2K box:

    VB Code:
    1. Private Declare Function GetUserName Lib "advapi32.dll" Alias "GetUserNameA" (ByVal lpbuffer As String, nsize As Long) As Long
    2.  
    3. Private Enum EXTENDED_NAME_FORMAT
    4.     NameUnknown = 0 '???
    5.     NameFullyQualifiedDN = 1 'Full Active Directory style name
    6.     NameSamCompatible = 2 'NT 4 style DOMAIN\user
    7.     NameDisplay = 3 'Friendly Name - FirstName LastName format
    8.     NameUniqueId = 6 'user id - {dfsfsd} format
    9.     NameCanonical = 7
    10.     NameUserPrincipal = 8
    11.     NameCanonicalEx = 9
    12.     NameServicePrincipal = 10
    13. End Enum
    14.  
    15. Private Declare Function GetUserNameEx Lib "secur32.dll" Alias "GetUserNameExA" _
    16.     (ByVal NameFormat As EXTENDED_NAME_FORMAT, ByVal lpbuffer As String, nsize As Long) _
    17.     As Long
    18.  
    19. Private Sub Command1_Click()
    20.     ' Display the name of the user currently logged on.
    21.     Dim username As String  ' receives name of the user
    22.     Dim slength As Long  ' length of the string
    23.     Dim retval As Long  ' return value
    24.    
    25.     ' Create room in the buffer to receive the returned string.
    26.     username = Space(255)  ' room for 255 characters
    27.     slength = 255  ' initialize the size of the string
    28.     ' Get the user's name and display it.
    29.     retval = GetUserName(username, slength)  ' slength is now the length of the returned string
    30.     username = Left(username, slength - 1)  ' extract the returned info from the buffer
    31.     ' (We subtracted one because we don't want the null character in the trimmed string.)
    32.     Text1.Text = username
    33. End Sub
    34.  
    35. Private Sub Command2_Click()
    36.     Dim username As String 'string buffer that gets User's Name
    37.     Dim slength As Long 'length of the string buffer
    38.     Dim retval As Long 'return value, 0 if error
    39.    
    40.     username = Space(255)
    41.     slength = 255
    42.     retval = GetUserNameEx(NameDisplay, username, slength)
    43.     username = Left(username, slength - 1)
    44.     Text2.Text = username
    45. 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.

  3. #3

    Thread Starter
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333
    GetUserNameEx seems to be the way to go for both XP and Win2K, but my question remains.

    Why does GetUserName not work in a New Project on Win2K, but does work in a compiled project created on a Win98 box? That is a complete mystery to me.

  4. #4
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687
    Good question Hack.
    Unfortunatly, I don't have the answer. The code JoshT posted is the came code we use in our app since the very begining back in 98. It's been running just fine on WinNT, Win95, Win98, Win2K, and even WinXP.
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  5. #5

    Thread Starter
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333
    Are you using GetUserName or GetUserNameEx?

    I ask, because GetUserNameEx is in the secur32.dll library, and I don't have that DLL on my Win98 box.

  6. #6
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687
    GetUserName
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

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