|
-
May 24th, 2002, 12:54 PM
#1
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?
-
May 24th, 2002, 01:55 PM
#2
Black Cat
Hack, this works fine on my W2K box:
VB Code:
Private Declare Function GetUserName Lib "advapi32.dll" Alias "GetUserNameA" (ByVal lpbuffer As String, nsize As Long) As Long
Private Enum EXTENDED_NAME_FORMAT
NameUnknown = 0 '???
NameFullyQualifiedDN = 1 'Full Active Directory style name
NameSamCompatible = 2 'NT 4 style DOMAIN\user
NameDisplay = 3 'Friendly Name - FirstName LastName format
NameUniqueId = 6 'user id - {dfsfsd} format
NameCanonical = 7
NameUserPrincipal = 8
NameCanonicalEx = 9
NameServicePrincipal = 10
End Enum
Private Declare Function GetUserNameEx Lib "secur32.dll" Alias "GetUserNameExA" _
(ByVal NameFormat As EXTENDED_NAME_FORMAT, ByVal lpbuffer As String, nsize As Long) _
As Long
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
Private Sub Command2_Click()
Dim username As String 'string buffer that gets User's Name
Dim slength As Long 'length of the string buffer
Dim retval As Long 'return value, 0 if error
username = Space(255)
slength = 255
retval = GetUserNameEx(NameDisplay, username, slength)
username = Left(username, slength - 1)
Text2.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.
-
May 24th, 2002, 01:59 PM
#3
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.
-
May 24th, 2002, 02:19 PM
#4
-
May 24th, 2002, 02:24 PM
#5
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.
-
May 24th, 2002, 04:21 PM
#6
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
|