iDevGames Forums
Retrieving user name with carbon. - Printable Version

+- iDevGames Forums (http://www.idevgames.com/forums)
+-- Forum: Development Zone (/forum-3.html)
+--- Forum: Tools & Technology (/forum-10.html)
+--- Thread: Retrieving user name with carbon. (/thread-2651.html)



Retrieving user name with carbon. - operator - May 2, 2008 05:57 AM

Hi!

I'm a bit new on Mac development.
I would like to know if there is a way to retrieve the current user name with Carbon?

I'm porting an application from Windows to Mac, and so far I couldn't find a way to do it.

Thanks!


Retrieving user name with carbon. - FlamingHairball - May 2, 2008 06:46 AM

In the words of the wise OneSadCookie: Carbon is all but dead. Use Cocoa instead.


Retrieving user name with carbon. - ThemsAllTook - May 2, 2008 06:51 AM

Are you looking for the user's short name, or their full name? If you just want short name, this appears to work:
Code:
#include <pwd.h>
#include <stdio.h>
#include <stdlib.h>

int main() {
    struct passwd * passwd;
    
    passwd = getpwuid(getuid());
    printf("Name: %s\n", passwd->pw_name);
    
    return EXIT_SUCCESS;
}

See also whoami() in http://www.opensource.apple.com/darwinsource/10.5/shell_cmds-116/who/who.c (requires ADC login).


Retrieving user name with carbon. - OneSadCookie - May 2, 2008 04:00 PM

NSUserName() / NSFullUserName() may also be useful. I thought there were CF equivalents but I can't see them :/


Retrieving user name with carbon. - operator - May 3, 2008 07:24 AM

Thanks.
Now I'll have to code and try.


Retrieving user name with carbon. - KittyMac - May 3, 2008 05:48 PM

And in the interest of completeness:

http://developer.apple.com/qa/qa2001/qa1078.html


Retrieving user name with carbon. - operator - May 5, 2008 06:55 PM

completeness, yeah, I would say ready for copy and paste ... thanks!


Retrieving user name with carbon. - Myrdred - May 10, 2008 07:16 PM

Here's the CF way:

Quote:char buffer[256];
Boolean success = CFStringGetCString(CSCopyUserName(true), buffer, sizeof(buffer), kCFStringEncodingUTF8);