简体   繁体   English

以编程方式在C ++中找到用户名?

[英]Programmatically find username in c++?

I'm creating little application which make easy some task in MS Word. 我正在创建小的应用程序,可以简化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. 应用程序必须作为宏导入到Word中,这意味着必须将其存储在用户下的某个模板文件夹中。 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. 所以我不会找出如何知道用户名和Windows版本的原因,因为我的用户名和文件夹位置与其他人不同。 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? 有没有什么蝙蝠代码,或者c ++中的某些函数可以轻松地将这两个信息存储在变量中,以便我在安装应用程序时可以轻松使用它?

OS: win7, vista,xp 操作系统:Win7,Vista,XP

To get the user name you use the GetUserName() function. 要获取用户名,请使用GetUserName()函数。

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) 为此,请使用诸如SHGetSpecialFolderPath()SHGetFolderPath()之类的东西,它们可以提供特殊文件夹的路径(例如用户的应用程序文件夹,其桌面等)。

You can get the user's profile directory by calling SHGetFolderPath(CSIDL_PROFILE) (Win2K and later) or SHGetKnownFolderPath(FOLDERID_Profile) (Vista and later). 您可以通过调用SHGetFolderPath(CSIDL_PROFILE) (Win2K和更高版本)或SHGetKnownFolderPath(FOLDERID_Profile) (Vista和更高版本)来获取用户的配置文件目录。

You can get a direct path to the templates folder using SHGetFolderPath(CSIDL_TEMPLATES) (Win2K and later) or SHGetKnownFolderPath(FOLDERID_Templates) (Vista and later). 您可以使用SHGetFolderPath(CSIDL_TEMPLATES) (Win2K和更高版本)或SHGetKnownFolderPath(FOLDERID_Templates) (Vista和更高版本)获得指向模板文件夹的直接路径。

I guess that once you've got the templates folder you don't need the user name or OS version. 我猜想,一旦有了template文件夹,就不需要用户名或OS版本了。

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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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