如何获得MinGW的用户名? [英] How to get the username in MinGW?
本文介绍了如何获得MinGW的用户名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
要在Windows获取用户名,使用MinGW的,我应当使用unistd.h中的函数getlogin(),或Windows函数GetUserName?
To get the user name from Windows, using MinGW, should I use the function getlogin() from unistd.h, or the Windows function GetUserName?
感谢您。
推荐答案
您可以检查 USERNAME
变量:
char *name = getenv("USERNAME"); // Get environmentvariable for Username
if( name == NULL )
return -1; // Username not found ...
else
printf("%s\n", name); // Output Username
如果你完全在Windows下你可以使用它的API( GetUserName()
)太:
If you are fully on Windows you can use its API (GetUserName()
) too:
#include <windows.h>
#include <Lmcons.h>
// ...
TCHAR name [ UNLEN + 1 ];
DWORD size = UNLEN + 1;
if( GetUserName((TCHAR*) name, &size) )
printf("%s\n", name); // Output Username
else
return -1; // Username not found ...
一般
- 使用
getlogin()
如果您在的Linux / Unix ,因为它的不在MinGW的可用 - 使用
GetUserName()
如果您在窗口 - 使用的两个(适用条件组preprocessor)你想留下来的平台影响无关
- use
getlogin()
if you are on linux / unix since it's not available in MinGW - use
GetUserName()
if you are on windows - use both (conditional group preprocessor) with you want to stay platform independend
In general:
这篇关于如何获得MinGW的用户名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文