retrieving real username

Talk about computers, hardware, applications, and consumer electronics.
Ice Cold
Posts: 202
Joined: 2008.09.26 (11:49)
Location: Australia
Contact:

Postby mattk210 » 2008.12.17 (11:24)

I'm creating a chat program for my school (flash assisted with SWF studio, but I'd be grateful with help from any other language), and I need to be able to access the user's username (the one that's at the top of the start menu). Using the "environment" values (type SET in cmd), I can get a username, but it is the name of the user directory and not the actual nickname. (If I change my nickname in the control panel my user directory will not change). This is important because at school, the user directory names are just numbers whereas the start menu displays the actual name of the user.

Any ideas?

Plus (Size) Member
Posts: 42
Joined: 2008.09.27 (02:56)

Postby taaveti » 2008.12.18 (03:58)

As you mentioned the Start menu, I'm going to assume you're on Windows. In that case, there's the GetUserName(char *, unsigned long *) API function, for which Flash may or may not provide a wrapper. It's in Advapi32.dll, if you can access DLLs.

Ice Cold
Posts: 202
Joined: 2008.09.26 (11:49)
Location: Australia
Contact:

Postby mattk210 » 2008.12.18 (04:35)

unfortunately that has the same problem.. to clarify, the name of my user data directory is c:/documents and settings/matt but somewhere along the line I used the control panel to change it to matthew, and it appears as such on the windows welcome screen and the start menu. Now I want to access "matthew" somehow.

Plus (Size) Member
Posts: 42
Joined: 2008.09.27 (02:56)

Postby taaveti » 2008.12.18 (06:02)

Hmm... maybe you'll have better luck than I did with GetUserNameEx; I can only get it to work with NameSamCompatible (which gives the fully qualified "COMPUTER_NAME/user_name", where user_name is probably the account name you don't want).

User avatar
Albany, New York
Posts: 521
Joined: 2008.09.28 (02:00)
MBTI Type: INTJ
Location: Inner SE Portland, OR
Contact:

Postby jean-luc » 2008.12.18 (16:17)

I'm thinking you might have to read that straight from the registry (I always use Business versions because I hate problems like that that home editions seem to incur). I'm not sure where in the registry to find it. You could try changing your nickname to something unique (like '1941ntbao') and then searching the registry for that string. If you need to search, be sure to use regedt32, not regedit. the 32 bit version searches much faster (I think XP still has both. It seems that they got rid of the 16bit editor and made regedit a shortcut to regedt32 in Vista).
-- I might be stupid, but that's a risk we're going to have to take. --
Image
Website! Photography! Robots! Facebook!
The latest computers from Japan can also perform magical operations.

Ice Cold
Posts: 202
Joined: 2008.09.26 (11:49)
Location: Australia
Contact:

Postby mattk210 » 2008.12.18 (22:35)

I did browse through the registry manually but was unsuccessful. I had no idea there was a find option, but it doesn't seem to return anything (regedit32 doesn't work). maybe it doesn't format it as a string?

User avatar
Albany, New York
Posts: 521
Joined: 2008.09.28 (02:00)
MBTI Type: INTJ
Location: Inner SE Portland, OR
Contact:

Postby jean-luc » 2008.12.19 (06:25)

it's regedt32, with no i. for whatever reason, despite being a 32 bit application, it still conforms to the 8.3 naming convention (8 character name, 3 character extension) from DOS.

It is possible it's stored binary, but I don't see why that would be the case. hmm.
-- I might be stupid, but that's a risk we're going to have to take. --
Image
Website! Photography! Robots! Facebook!
The latest computers from Japan can also perform magical operations.

Plus (Size) Member
Posts: 42
Joined: 2008.09.27 (02:56)

Postby taaveti » 2008.12.20 (05:27)

Man did it take a lot of time on MSDN to get this one...

Here it is in C:

Code: Select all

#include <windows.h> //GetUserName
#include <lm.h> //NetUserGetInfo
#include <stdlib.h> //mbstowcs, wcstombs
#include <string.h> //strcpy

//Get the name of the current user; store in nameBuf.
//Return 0 on success, nonzero on failure.
int getRealUserName(char *nameBuf){
 wchar_t wbuf[1024];
 char buf[1024];
 unsigned long buflen=1024;
 USER_INFO_2 *uinfo;

 //get the base user name
 if(GetUserName(buf, &buflen) == 0){ return 1; }

 //widen user name
 if(mbstowcs(wbuf, buf, 1024) <= 0){ return 2; }

 //get user info
 if(NetUserGetInfo(NULL, wbuf, 2, (unsigned char **)(&uinfo)) != NERR_Success){ return 3; }

 //compress full name
 if(wcstombs(nameBuf, uinfo->usri2_full_name, 1024) <= 0){ return 4; }

 //success
 return 0;
}
Last edited by taaveti on 2008.12.20 (23:41), edited 1 time in total.

Ice Cold
Posts: 202
Joined: 2008.09.26 (11:49)
Location: Australia
Contact:

Postby mattk210 » 2008.12.20 (10:41)

sorry, I'm not really a c programmer. At first I got an error with the datatype of argument 4 of netusergetinfo, which I managed to fix, but now it claims

Code: Select all

[Linker error] undefined reference to `NetUserGetInfo@16' 
I did include lm.h

I'm on Dev c++. Is this something stupid? (I couldn't get any other implementations of netusergetinfo to work either..)

Plus (Size) Member
Posts: 42
Joined: 2008.09.27 (02:56)

Postby taaveti » 2008.12.21 (00:01)

I forgot to cast uinfo to the right type in the NetUserGetInfo call. It's fixed now.

As for the linker error, dev-c++ uses gcc, which may or may not have the appropriate libraries (although it seems odd that it would provide windows.h and lm.h without the associated libraries). It's also possible that you just need to provide an appropriate linker option (maybe "-lnetapi32"?). Sorry I'm not much help in that department, I haven't used gcc in years, and only once tried to use the Windows version.
If gcc is too much trouble, Borland's free compiler (you can probably find it by googling "borland free command line tools" or something similar) can compile what I posted above (plus a simple main which calls getRealUserName and prints the name) without any trouble.

Ice Cold
Posts: 202
Joined: 2008.09.26 (11:49)
Location: Australia
Contact:

Postby mattk210 » 2008.12.21 (23:01)

Thanks for your help. I'm using visual c++ now.


Who is online

Users browsing this forum: No registered users and 13 guests