Yigang Wu
Yigang Wu

Reputation: 3592

What's wrong with GetUserName Win32 API?

I'm using GetUserName Win32 API to get the user name of my computer, but I found the user name is different (uppercase vs. lowercase only) when using my VPN connection into work when I was at home. I’m wondering if the VPN client or other software could be affecting the username?

Upvotes: 3

Views: 7688

Answers (3)

esac
esac

Reputation: 24685

This API returns the name as typed by the user when logging on to the computer. So if my username is 'esac', but I type 'Esac', this API will return 'Esac'. Subsequently if I type 'ESAC' that is what it will return as well.

Upvotes: 3

Shane Powell
Shane Powell

Reputation: 14138

The GetUserName API states:

Retrieves the name of the user associated with the current thread.

Use the GetUserNameEx function to retrieve the user name in a specified format. Additional information is provided by the IADsADSystemInfo interface.

So it looks like that GetUserName uses IADsADSystemInfo to get it's information.

If you look at the IADsADSystemInfo interface you see it has the method:

get_UserName
Retrieves the Active Directory distinguished name of the current user, which is the logged-on user or the user impersonated by the calling thread.

So when your connected via VPN to a domain login you will most likely get Active Directory distinguished name of the current user and when you aren't, you most likely get the user name that you typed in (in whatever case you typed in) to log onto the computer with.

Upvotes: 3

Ana Betts
Ana Betts

Reputation: 74654

Not 100% sure, but I suspect that GetUserName will end up talking to the DC when you're connected to your domain network, whereas it will use the local answer otherwise

Upvotes: 0

Related Questions