简体   繁体   中英

Programmatically find username in c++?

I'm creating little application which make easy some task in MS Word. Application have to be imported in Word as macro, that's mean that have to be stored in some template folder which is under user. So I wan't to find out how to know what is the name of user , and what is version of windows , cause my username and folder location is not same as someone else. Is there any little bat code, or some function in c++ that can easy just take those two information and store it in variable, that I can easy use it when installing application?

OS: win7, vista,xp

To get the user name you use the GetUserName() function.

However, this is not the best way to determine the current user's folder locations. For that use something like SHGetSpecialFolderPath() or SHGetFolderPath() instead which can give the path of a special folder (such as the user's application folder, their desktop etc)

You can get the user's profile directory by calling SHGetFolderPath(CSIDL_PROFILE) (Win2K and later) or SHGetKnownFolderPath(FOLDERID_Profile) (Vista and later).

You can get a direct path to the templates folder using SHGetFolderPath(CSIDL_TEMPLATES) (Win2K and later) or SHGetKnownFolderPath(FOLDERID_Templates) (Vista and later).

I guess that once you've got the templates folder you don't need the user name or OS version.

Use this :

void UserName(string *x){
char username[UNLEN + 1];
DWORD size = UNLEN + 1;
GetUserName(username, &size);
string transition(username);
*x=transition;}
//use this syntax in main : string username;UserName(&username);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM