PDA

View Full Version : Real user name



johnny44
04-11-2006, 01:16 PM
hello,
is it possible to determine the windows(XP) log in name ?

Bob Phillips
04-11-2006, 01:42 PM
MsgBox Environ("UserName")

johnny44
04-11-2006, 01:44 PM
MsgBox Environ("USERNAME")
I get the user name.
However I changed the user name in control panel/users to another name.
How can I get the name that is now displayed and not thhe origional user name ?
Is this possible ?

Bob Phillips
04-11-2006, 01:49 PM
I am not about to change my username like that to test this, but you could try it


Private Declare Function GetUserName Lib "advapi32.dll" Alias "GetUserNameA"
(ByVal lpBuffer As String, nSize As Long) As Long
Function UserNameWindows()
Dim lngLen As Long
Dim strBuffer As String
Const dhcMaxUserName = 255
strBuffer = Space(dhcMaxUserName)
lngLen = dhcMaxUserName
If CBool(GetUserName(strBuffer, lngLen)) Then
UserNameWindows = Left$(strBuffer, lngLen - 1)
Else
UserNameWindows = ""
End If
End Function

johnny44
04-11-2006, 02:00 PM
Same result :(

Ken Puls
04-11-2006, 03:18 PM
How about this article from the KB?

Function to return various Environment Names

Bob Phillips
04-11-2006, 04:02 PM
Same result :(

That suggests to me that the OS doesn't know about it even, and won't until next login, by which time the Environ option will work.

johnny44
04-12-2006, 05:48 AM
This seems to do it...
Application.UserName
:)

Bob Phillips
04-12-2006, 06:25 AM
This seems to do it...
Application.UserName
:)

That is not the Windows (XP) login name as you originally said, it is internal to Excel and can be changed by the user at any time.

mvidas
04-12-2006, 06:35 AM
Exactly.. the entry:
http://vbaexpress.com/kb/getarticle.php?kb_id=768

That shows the different methods available, and what it is pulling. Since they pull from the windows login, they won't change until the user logs off and logs back on. The Application.Username is the office name, and as Bob says the user can change that easily by many ways.

Matt

johnny44
04-12-2006, 08:57 AM
Well noted...
So how could I get the user XP log in name that shows on the XP login Screen ?

the reg key seems to be this one :
HKEY_CURRENT_USER\Software\Microsoft\Windows NT\CurrentVersion\Windows Messaging Subsystem\Profiles\Johnny

Ken Puls
04-12-2006, 08:57 AM
MsgBox Environ("USERNAME")
I get the user name.
However I changed the user name in control panel/users to another name.
How can I get the name that is now displayed and not thhe origional user name ?
Is this possible ?

What happens when you log off and log in again? I'd bet it's updated to the new name then? If it is, how often do you expect this to happen?

johnny44
04-12-2006, 09:05 AM
I dont want to create new XP users, So I changed the names on the various xp stations to reflect the current name of the user.

So if a profile was created as "john" and the the name is changed via control panel/user accounts/change account/change my name to "wilma"

I would need to capture the wilma value from my xp stations.

So far I can only get the john even though it has been changed to wilma.
I think I read that the only way is to re create the account. But I figured if XP shows wilma there must be a way to get that same value...

Bob Phillips
04-12-2006, 10:16 AM
Well, here is another attempt (not holding my breath if the API version doesn't work).


Private Declare Function GetEnvironmentVariable Lib "kernel32" _
Alias "GetEnvironmentVariableA" _
(ByVal lpName As String, _
ByVal lpBuffer As String, _
ByVal nSize As Long) As Long

Sub xx()
MsgBox GetEnvironmentVar("UserName")
End Sub

Function GetEnvironmentVar(Name As String) As String
GetEnvironmentVar = String(255, 0)
GetEnvironmentVariable Name, GetEnvironmentVar, Len(GetEnvironmentVar)
GetEnvironmentVar = TrimNull(GetEnvironmentVar)
End Function

Private Function TrimNull(item As String)
Dim iPos As Long
iPos = InStr(item, vbNullChar)
TrimNull = IIf(iPos > 0, Left$(item, iPos - 1), item)
End Function

johnny44
04-12-2006, 10:42 AM
Sory...did not work...still gave ne the "origional" name of account :(

johnny44
04-12-2006, 10:43 AM
I think the only way is to get from registrie ?

Ken Puls
04-12-2006, 10:59 AM
Sory...did not work...still gave ne the "origional" name of account :(

Can you just confirm? You definately logged out and back in under the new name, correct?

johnny44
04-12-2006, 11:32 AM
Yes I re booted and I the sub still returns the "Origional" name :banghead:

Bob Phillips
04-12-2006, 12:01 PM
Yes I re booted and I the sub still returns the "Origional" name :banghead:

I find that hard to believe. That is akin to saying that logon as another user will give you the original user, which is impossible (I think :dunno).

Bob Phillips
04-12-2006, 12:09 PM
Bloody hell. I just tried it (just login, not re-boot), and he is right, it still returns my original name. What is going on?

mvidas
04-12-2006, 12:56 PM
If you want to read the registry, you can do that through vb. Though there are other ways to do so, you could always import the attached .bas module, then use:MsgBox RegRead("Software\Microsoft\Windows NT\CurrentVersion\Windows Messaging Subsystem\Profiles\", "Johnny")Matt

johnny44
04-12-2006, 01:02 PM
This link explains why...
http://www.pcanswers.co.uk/tips/default.asp?pagetypeid=2&articleid=38320&subsectionid=616

but I still cant get how to get the "changed to" name insted....:whistle:

Ken Puls
04-12-2006, 01:53 PM
I'm sure that somewhere there is an API or a reg entry (if Matt hasn't given us that alread) that can pull the new name.

I guess though, after looking at the link above, I'm curious why you don't just follow the procedures listed there. It seems like it would be cleaner in the long run. While I appreciate that it is a PITA to do, I wonder if the up front time investment might be a better bet. Who knows what other quirks might rear their ugly heads later on down the line...

Just my thought. :dunno