PDA

View Full Version : Retrieving Username in C++




GenghisKhan
Jan 29, 2009, 01:49 PM
Hi there, I'm working on a small XCode project to get my C++ skills up to par again.

I need to retrieve the username to use with a path command in "int execpl(char *path, char *arg, ...)". Since this app is going to run on somebody else's Mac and I don't know his username, I need a smart way to retrieve it.

I could get it from the environment variable $USER but I don't know how to do this in C++.

Anyone?



GenghisKhan
Jan 29, 2009, 01:54 PM
You can use "stdlib.h" apparantly, to use char *getenv(const char *name);

Hi there, I'm working on a small XCode project to get my C++ skills up to par again.

I need to retrieve the username to use with a path command in "int execpl(char *path, char *arg, ...)". Since this app is going to run on somebody else's Mac and I don't know his username, I need a smart way to retrieve it.

I could get it from the environment variable $USER but I don't know how to do this in C++.

Anyone?

kainjow
Jan 29, 2009, 11:15 PM
It might be better to use CSCopyUserName() from the CoreServices framework, but that's not cross-platform if you're going that route.