1. Win32Lib getUserName() problem

I have recently written a program which uses the Win32Lib getUserName() routine. This works fine, but if the user changes his name in Windows, the getUsername() routine still gets his original name. Now I guess that Windows - XP SP3 in my case - creates the initial user group with the original name, but then just hangs a string on it with the name. Initially the name created, but after that whatever the user has called himself.

Is there any way that anyone knows whereby I can get the current user's name, rather than the name the current account was created with?

Thanks in advance...

Andy

new topic     » topic index » view message » categorize

2. Re: Win32Lib getUserName() problem

AndyDrummond said...

I have recently written a program which uses the Win32Lib getUserName() routine. This works fine, but if the user changes his name in Windows, the getUsername() routine still gets his original name. Now I guess that Windows - XP SP3 in my case - creates the initial user group with the original name, but then just hangs a string on it with the name. Initially the name created, but after that whatever the user has called himself.

Is there any way that anyone knows whereby I can get the current user's name, rather than the name the current account was created with?

Thanks in advance...

Andy

Eh? How's he changing his name?

Does %USERNAME% or %USER% get updated after the change? If so, you can just use getenv() rather than trying to find a W32API function to detect this.

new topic     » goto parent     » topic index » view message » categorize

3. Re: Win32Lib getUserName() problem

AndyDrummond said...

Is there any way that anyone knows whereby I can get the current user's name, rather than the name the current account was created with?

I'm not sure what Win32Lib is doing, exactly, but you might want to take a look at GetUserNameEx. There appear to be many different ways to get a name:

typedef enum  { 
  NameUnknown            = 0, 
  NameFullyQualifiedDN   = 1, 
  NameSamCompatible      = 2, 
  NameDisplay            = 3, 
  NameUniqueId           = 6, 
  NameCanonical          = 7, 
  NameUserPrincipal      = 8, 
  NameCanonicalEx        = 9, 
  NameServicePrincipal   = 10, 
  NameDnsDomain          = 12  
} EXTENDED_NAME_FORMAT, *PEXTENDED_NAME_FORMAT; 

Matt

new topic     » goto parent     » topic index » view message » categorize

4. Re: Win32Lib getUserName() problem

jimcbrown said...

Eh? How's he changing his name?

Does %USERNAME% or %USER% get updated after the change? If so, you can just use getenv() rather than trying to find a W32API function to detect this.

I'll try that - he changes his name by going into Control Panel - User Accounts - Change Account ....

Certainly getenv("USERNAME") would be simpler and I'd know what is going on. Why didn't I think of that? Thanks.

new topic     » goto parent     » topic index » view message » categorize

5. Re: Win32Lib getUserName() problem

AndyDrummond said...
jimcbrown said...

Eh? How's he changing his name?

Does %USERNAME% or %USER% get updated after the change? If so, you can just use getenv() rather than trying to find a W32API function to detect this.

I'll try that - he changes his name by going into Control Panel - User Accounts - Change Account ....

Certainly getenv("USERNAME") would be simpler and I'd know what is going on. Why didn't I think of that? Thanks.

Nope - the name entered by control panel appears nowhere in the environment. I shall now try getUserNameEx ....

new topic     » goto parent     » topic index » view message » categorize

6. Re: Win32Lib getUserName() problem

mattlewis said...
AndyDrummond said...

Is there any way that anyone knows whereby I can get the current user's name, rather than the name the current account was created with?

I'm not sure what Win32Lib is doing, exactly, but you might want to take a look at GetUserNameEx. There appear to be many different ways to get a name:

typedef enum  { 
  NameUnknown            = 0, 
  NameFullyQualifiedDN   = 1, 
  NameSamCompatible      = 2, 
  NameDisplay            = 3, 
  NameUniqueId           = 6, 
  NameCanonical          = 7, 
  NameUserPrincipal      = 8, 
  NameCanonicalEx        = 9, 
  NameServicePrincipal   = 10, 
  NameDnsDomain          = 12  
} EXTENDED_NAME_FORMAT, *PEXTENDED_NAME_FORMAT; 

Matt

Matt,

I am having trouble with this. Can you cast your eye over this and see why it fails,please?

constant 
advapi32    = registerw32Library("advapi32.dll"),  -- common dialogs 
xGetUserNameEx = registerw32Function(advapi32, "GetUserNameExA", {C_INT, C_POINTER, C_POINTER}, C_INT) 
 
global function getUserNameEx(integer format) 
    sequence lUserId 
    atom lBufferAddress, lResultCode 
 
    lBufferAddress = w32acquire_mem(0, 4 + 256 + 1) 
    poke4(lBufferAddress, 256) 
    lResultCode = w32Func(xGetUserNameEx, {format, lBufferAddress+4, lBufferAddress}) 
     
    if lResultCode then 
        lUserId = w32peek_string(lBufferAddress+4) 
    else 
        lUserId = {} 
    end if 
    w32release_mem(lBufferAddress) 
    return lUserId 
end function 

I tried C_LONG rather than C_INT just in case; same "klonk!" and failure when the w32Func call is made. I copied the code from Win32Lib's getUserName() with enhancements. The value of xGetUserNameEx is valid looking. I called with format equal to 1.....

Win32 interfacing is definitely NOT my stromg point.

Andy

new topic     » goto parent     » topic index » view message » categorize

7. Re: Win32Lib getUserName() problem

AndyDrummond said...

I tried C_LONG rather than C_INT just in case; same "klonk!" and failure when the w32Func call is made. I copied the code from Win32Lib's getUserName() with enhancements. The value of xGetUserNameEx is valid looking. I called with format equal to 1.....

Your code looks ok to me. Have you tried any of the other formats? If it fails, you might try calling GetLastError to see if there's anything interesting there.

Matt

new topic     » goto parent     » topic index » view message » categorize

8. Re: Win32Lib getUserName() problem

mattlewis said...

Your code looks ok to me. Have you tried any of the other formats? If it fails, you might try calling GetLastError to see if there's anything interesting there.

Matt

The failure is a terminal one - I shall have to put getLastError() into the crash file. We will see what we will see then ...

Andy

new topic     » goto parent     » topic index » view message » categorize

9. Re: Win32Lib getUserName() problem

mattlewis said...

Your code looks ok to me. Have you tried any of the other formats? If it fails, you might try calling GetLastError to see if there's anything interesting there.

Matt

Ah. This is a problem. First - the error is:

Error 464 - Couldn't link to function GetUserNameExA 
 
But more serious is that looking at the MS website it seems this is only available on Win XP Professional - and that is really a limitation I can't enforce. I suppose I shall have to ask the user to enter his name when I first run the program, and save that in an ini fle for that user so it uses the same name in future. A silly business - one feels that M$'s left hand and right hand are on opposite sides of Redmond! Oh - they are!

Thanks for your help and ideas, anyway. Any others - well, I still look at the forum....

Andy

new topic     » goto parent     » topic index » view message » categorize

10. Re: Win32Lib getUserName() problem

AndyDrummond said...
mattlewis said...

Your code looks ok to me. Have you tried any of the other formats? If it fails, you might try calling GetLastError to see if there's anything interesting there.

Matt

The failure is a terminal one - I shall have to put getLastError() into the crash file. We will see what we will see then ...

Ah. That's different, then. In this sort of situation I often try allocating a much larger buffer than I need, just in case there's something strange going on there, or I misread what I'm supposed to be doing. And a closer reading of the documentation shows this

GetUserNameEx said...
  • lpnSize This parameter receives the required buffer size, in Unicode characters (whether or not Unicode is being used), including the terminating null character.

So it looks like you may need to allocate double the size buffer (so 518 bytes total in your code). That's what I'd try.

Matt

new topic     » goto parent     » topic index » view message » categorize

11. Re: Win32Lib getUserName() problem

AndyDrummond said...

Ah. This is a problem. First - the error is:

Error 464 - Couldn't link to function GetUserNameExA 
 
But more serious is that looking at the MS website it seems this is only available on Win XP Professional - and that is really a limitation I can't enforce.

Actually, that's not true, according to: http://support.microsoft.com/kb/222440

It references secur32.dll all the way back to Win95. However, I noticed you were trying to get it from advapi32.dll, which is incorrect. Try changing that to Secur32.dll (which is what the docs for GetUserNameEx reference).

Matt

new topic     » goto parent     » topic index » view message » categorize

12. Re: Win32Lib getUserName() problem

mattlewis said...

Actually, that's not true, according to: http://support.microsoft.com/kb/222440

It references secur32.dll all the way back to Win95. However, I noticed you were trying to get it from advapi32.dll, which is incorrect. Try changing that to Secur32.dll (which is what the docs for GetUserNameEx reference).

Matt

Yes, maybe it would have helped if I'd noticed that. Yes, it works then. However ... there's always a catch, isn't there? ... only GetUserNameEx(NameSamCompatible) works. According to the M$ info,

If the user account is not in a domain, only NameSamCompatible is supported.

I'm not even sure what that means except that I am clearly not in a domain. That returns AndysLaptop/Andrew to me, which contains my original user name and not my revised log-on name. So again there is a whole range of situations where I'm not going to get the users log-on name. So we are back to square one. Oh well. Any ideas welcome, but don't spend long on this...

andy

new topic     » goto parent     » topic index » view message » categorize

13. Re: Win32Lib getUserName() problem

I know this is not the preferred way of dealing with it, but maybe you can pipe the output from a SET command to a text file (or memory) and interrogate the lines until you find "USERNAME=".

new topic     » goto parent     » topic index » view message » categorize

14. Re: Win32Lib getUserName() problem

Mike777 said...

I know this is not the preferred way of dealing with it, but maybe you can pipe the output from a SET command to a text file (or memory) and interrogate the lines until you find "USERNAME=".

If that would have worked, so would have getenv("USERNAME").

I am wondering if he simply needs to log out and then log back in to see the updated username. I'm also wondering if its really the username being queried here (i.e. jsmith472 or Compaq_Owner) or a Full Name/Real Name field (i.e. "John Smith" or "Compaq Owner").

new topic     » goto parent     » topic index » view message » categorize

15. Re: Win32Lib getUserName() problem

jimcbrown said...
Mike777 said...

I know this is not the preferred way of dealing with it, but maybe you can pipe the output from a SET command to a text file (or memory) and interrogate the lines until you find "USERNAME=".

If that would have worked, so would have getenv("USERNAME").

I am wondering if he simply needs to log out and then log back in to see the updated username. I'm also wondering if its really the username being queried here (i.e. jsmith472 or Compaq_Owner) or a Full Name/Real Name field (i.e. "John Smith" or "Compaq Owner").

The environment variable USERNAME is essentially the name of the folder below "C:\Documents and Settings", which is not changed when you go into Control Panel & Users and so on to change your name. That name is used when you are logging in, and is presumably held in a config file of some kind by Windows. Maybe I could search for such a file with the new name in to see where it is, but the Windows search algorithm seems to lack something fundamental. Logging off and on has no effect; because so many programs remember paths, Windows doesn't change its own internal pathname & user name, just stores a copy someplace for display purposes. I dare say *nix must do something similar because once a working environment is set up, you don't want to start changng the almost-root folder name or you'd foul most programs up.

new topic     » goto parent     » topic index » view message » categorize

16. Re: Win32Lib getUserName() problem

AndyDrummond said...

That name is used when you are logging in, and is presumably held in a config file of some kind by Windows. Maybe I could search for such a file with the new name in to see where it is, but the Windows search algorithm seems to lack something fundamental

That's not the Windows way. It will be somewhere in the Registry.

Matt

new topic     » goto parent     » topic index » view message » categorize

17. Re: Win32Lib getUserName() problem

Well, I've got:

My Computer\HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\Logon User Name which has my username. I would change username and see what name shows up there.

new topic     » goto parent     » topic index » view message » categorize

18. Re: Win32Lib getUserName() problem

AndyDrummond said...

The environment variable USERNAME is essentially the name of the folder below "C:\Documents and Settings", which is not changed when you go into Control Panel & Users and so on to change your name. That name is used when you are logging in, and is presumably held in a config file of some kind by Windows.

This is the username.

AndyDrummond said...

Maybe I could search for such a file with the new name in to see where it is, but the Windows search algorithm seems to lack something fundamental. Logging off and on has no effect; because so many programs remember paths, Windows doesn't change its own internal pathname & user name, just stores a copy someplace for display purposes.

There should not be anything internal regarding this. Once the appropriate registry entries are located, it should be possible to manually change all of this in concert, I should think. (Not saying it's a good idea, mind you.)

AndyDrummond said...

I dare say *nix must do something similar because once a working environment is set up, you don't want to start changng the almost-root folder name or you'd foul most programs up.

I'm not aware of a command that simply renames a user, but it's possible to do this maually. In most cases, one would rename the user's directory to match the new username as well, but it's possible to keep the old directory's name if that is necessary.

The full path of the user's home directory is set up in /etc/passwd

The user's Full Name is also in /etc/passwd in the GECOS field. Both these variables can be edited with the adduser or usermod commands.

new topic     » goto parent     » topic index » view message » categorize

19. Re: Win32Lib getUserName() problem

jimcbrown said...
AndyDrummond said...

The environment variable USERNAME is essentially the name of the folder below "C:\Documents and Settings", which is not changed when you go into Control Panel & Users and so on to change your name. That name is used when you are logging in, and is presumably held in a config file of some kind by Windows.

This is the username.

AndyDrummond said...

Maybe I could search for such a file with the new name in to see where it is, but the Windows search algorithm seems to lack something fundamental. Logging off and on has no effect; because so many programs remember paths, Windows doesn't change its own internal pathname & user name, just stores a copy someplace for display purposes.

There should not be anything internal regarding this. Once the appropriate registry entries are located, it should be possible to manually change all of this in concert, I should think. (Not saying it's a good idea, mind you.)

AndyDrummond said...

I dare say *nix must do something similar because once a working environment is set up, you don't want to start changng the almost-root folder name or you'd foul most programs up.

I'm not aware of a command that simply renames a user, but it's possible to do this maually. In most cases, one would rename the user's directory to match the new username as well, but it's possible to keep the old directory's name if that is necessary.

The full path of the user's home directory is set up in /etc/passwd

The user's Full Name is also in /etc/passwd in the GECOS field. Both these variables can be edited with the adduser or usermod commands.

In WinXP, if I create a new user, called FRED for the sake of argument, then a new folder C:\Documents and Settings\FRED is created, which contains all the odds and ends like Desktop, My Documents and so on. If I then go into Control Panel and Users, I can change the user's name to, lets say, GEORGE, this does not rename the folder C:\Documents and Settings\FRED, it leaves it alone. But if I try and log on, I get offered the new name GEORGE, and indeed this appears in almost all places where the user name is required. But nevertheless the folder where all the user's stuff is kept is still called FRED. This is all true. However, the environment still shows the username to be FRED.

My own user was created using the name Andrew, and that is still there. But I log on as Andy Drummond, log off as Andy Drummond etc. But the name Andy Drummond does not appear anywhere in the environment variables. In there, USERNAME=Andy Drummond.

Let's not chase this any further; I don't want to waste anyone's valuable time, and I certainly don't want to sound acrimonious. There are many more ways to skin a cat, and I now simply ask the user to enter his name and save it in an INI file for subsequent use. Seems OK.

Thank you all for your help. Back to useful stuff like Eu4 debugging...

Andy

new topic     » goto parent     » topic index » view message » categorize

20. Re: Win32Lib getUserName() problem

AndyDrummond said...
jimcbrown said...
AndyDrummond said...

The environment variable USERNAME is essentially the name of the folder below "C:\Documents and Settings", which is not changed when you go into Control Panel & Users and so on to change your name. That name is used when you are logging in, and is presumably held in a config file of some kind by Windows.

This is the username.

AndyDrummond said...

Maybe I could search for such a file with the new name in to see where it is, but the Windows search algorithm seems to lack something fundamental. Logging off and on has no effect; because so many programs remember paths, Windows doesn't change its own internal pathname & user name, just stores a copy someplace for display purposes.

There should not be anything internal regarding this. Once the appropriate registry entries are located, it should be possible to manually change all of this in concert, I should think. (Not saying it's a good idea, mind you.)

AndyDrummond said...

I dare say *nix must do something similar because once a working environment is set up, you don't want to start changng the almost-root folder name or you'd foul most programs up.

I'm not aware of a command that simply renames a user, but it's possible to do this maually. In most cases, one would rename the user's directory to match the new username as well, but it's possible to keep the old directory's name if that is necessary.

The full path of the user's home directory is set up in /etc/passwd

The user's Full Name is also in /etc/passwd in the GECOS field. Both these variables can be edited with the adduser or usermod commands.

In WinXP, if I create a new user, called FRED for the sake of argument, then a new folder C:\Documents and Settings\FRED is created, which contains all the odds and ends like Desktop, My Documents and so on. If I then go into Control Panel and Users, I can change the user's name to, lets say, GEORGE, this does not rename the folder C:\Documents and Settings\FRED, it leaves it alone. But if I try and log on, I get offered the new name GEORGE, and indeed this appears in almost all places where the user name is required. But nevertheless the folder where all the user's stuff is kept is still called FRED. This is all true. However, the environment still shows the username to be FRED.

My own user was created using the name Andrew, and that is still there. But I log on as Andy Drummond, log off as Andy Drummond etc. But the name Andy Drummond does not appear anywhere in the environment variables. In there, USERNAME=Andy Drummond.

Let's not chase this any further; I don't want to waste anyone's valuable time, and I certainly don't want to sound acrimonious. There are many more ways to skin a cat, and I now simply ask the user to enter his name and save it in an INI file for subsequent use. Seems OK.

Thank you all for your help. Back to useful stuff like Eu4 debugging...

Andy

Your postings appear to contradict themselves (and the latest post seems to contradict itself), which is very confusing. Additionally, I admit that I did not know that XP's usernames were allowed to contain spaces.

Although this won't help you, I'd still appreciate it if a W32 guru could explain how usernames in XP are suppose to work.

new topic     » goto parent     » topic index » view message » categorize

21. Re: Win32Lib getUserName() problem

jimcbrown said...

Your postings appear to contradict themselves (and the latest post seems to contradict itself), which is very confusing. Additionally, I admit that I did not know that XP's usernames were allowed to contain spaces.

Although this won't help you, I'd still appreciate it if a W32 guru could explain how usernames in XP are suppose to work.

Jim,

I'm sorry, but I don't see how my posts contradict themselves. Maybe if I summarise/summarize it will be clearer to me and/or you.

When Windows XP creates a user, it creates a folder inside which is everything that user has for himself. He also has a Name. For convenience that folder name is made equal to the first Name used. Thereafter if he changes his Name, the folder name is unchanged. You see I differentiate between the general word name and the word Name which is just, as in the old CB days, a handle. It is this handle I want(ed) to get. But the USERNAME as shown in the environment variables is the name of the folder. The folder name and any of the user Names may have spaces in if desired; I don't think that is an issue. And a user can change his Name as often as he likes.

This seems clear and consistent to me - but I wrote it! I know what I want to say. Everyone else has to read my words to get that information, and I just hope that they get the same information as I'm trying to convey.

If I have failed again then I will just retire from the fray as a failed communicator. It won't offend me, as long as I don't offend you. So Happy Christmas, or as I gather you say over the pond, Happy Holiday.

Andy

new topic     » goto parent     » topic index » view message » categorize

22. Re: Win32Lib getUserName() problem

AndyDrummond said...

Jim,

When Windows XP creates a user, it creates a folder inside which is everything that user has for himself. He also has a Name. For convenience that folder name is made equal to the first Name used. Thereafter if he changes his Name, the folder name is unchanged. You see I differentiate between the general word name and the word Name which is just, as in the old CB days, a handle. It is this handle I want(ed) to get. But the USERNAME as shown in the environment variables is the name of the folder. The folder name and any of the user Names may have spaces in if desired; I don't think that is an issue. And a user can change his Name as often as he likes.

This seems clear and consistent to me - but I wrote it! I know what I want to say. Everyone else has to read my words to get that information, and I just hope that they get the same information as I'm trying to convey.

If I have failed again then I will just retire from the fray as a failed communicator. It won't offend me, as long as I don't offend you. So Happy Christmas, or as I gather you say over the pond, Happy Holiday.

Andy

This summary not clears up the confusion from your previous posts, but it is also consistent with my understanding of XP's usernames, gleaned from http://support.microsoft.com/kb/283111 . What threw me off was that you said the name of the folder in Documents and Settings was Andrew but the USERNAME environment variable was set to Andy Drummond.

new topic     » goto parent     » topic index » view message » categorize

23. Re: Win32Lib getUserName() problem

jimcbrown said...

This summary not clears up the confusion from your previous posts, but it is also consistent with my understanding of XP's usernames, gleaned from http://support.microsoft.com/kb/283111 . What threw me off was that you said the name of the folder in Documents and Settings was Andrew but the USERNAME environment variable was set to Andy Drummond.

Oh, I did say that, didn't I? Sorry - I must read my own posts more carefully. I apologise unreservedly.

The M$ knowledge base article, which I'd not seen before, says the same things as I'm trying to say - but doesn't tell me how to find the Logon Name or Full Name or display name, as they call it. Must be held someplace, but they don't say where.

I have got around the problem anyway now, so this really is time to putt his to sleep. Thanks for your interest.

new topic     » goto parent     » topic index » view message » categorize

Search



Quick Links

User menu

Not signed in.

Misc Menu